#How to create an upload file (any types of file) service in Java?

51 messages · Page 1 of 1 (latest)

ashen sedge
#

I'm creating this business app card using Java for my project and now I'm stuck at creating the Service to upload a file in Java. So far I did my research on the internet but mainly they only show how to do it through Controller and I'm afraid if I build the Controller first it will ruin the architecture. Do you guys have experience of how to do it or maybe can refer to a project or any resources to learn about it?

weak jettyBOT
#

<@&987246841693360200> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

obsidian shell
ashen sedge
obsidian shell
#

genuinely it will sabotage the mechanisms by which you learn and retain info

ashen sedge
obsidian shell
#

so lets take a step back

#

what is the project

#

and what are your requirements

ashen sedge
#

most of the example I get from the internet is only discussing about the Controller class

obsidian shell
#

who are you doing this program for

ashen sedge
#

For my personal project. I am shifting a career toward softwate engineering

obsidian shell
#

describe in detail what you know as of right now

ashen sedge
#

I have created the dto and the entity also injected the dependecies required. I have created the mapper as well to translates the data so it can be saved into the database and processed within the service. So far these things what I did

obsidian shell
#

that is what you did

#

that is not what you know

#

if you dont know why you are doing the things you have done we need to take a step back

ashen sedge
#

Can you explain more about what do I know?

obsidian shell
#

like, what that means?

ashen sedge
#

I still don't understand. Mind to elaborate or give an example?

obsidian shell
#

okay there is a difference between knowledge and understanding

#

if you "know" to do something you don't need to understand why you do it, just to do it

#

if you understand why you do something you would know when you wouldn't do it

#

and it sounds to me like you followed a tutorial that just said "write this file. do this. this is the pattern"

#

and while that can be useful in small doses, it sabotages your ability to truly learn

ashen sedge
#

So what should I do for handling this?

obsidian shell
#

can you share your code so far

#

and what is your native language?

ashen sedge
#

package org.example.bizcardapp.Entity;

import org.example.bizcardapp.dto.CardDto;
import org.springframework.stereotype.Component;

@Component
public class CardMapper {

public Card toEntity (CardDto dto, User owner){

    if (dto != null){

        return Card.builder()
                .fullName(dto.name())
                .title(dto.title())
                .company(dto.company())
                .address(dto.address())
                .email(dto.email())
                .phone(dto.phone())
                .website(dto.website())
                .tagline(dto.tagline())
                .logoDesc(dto.logoDesc())
                .ocrRawText(dto.ocrRawText())
                .user(owner)
                .build();


    } else {
        return null;
    }

}

public CardDto toDto(Card card){

    if (card == null){
        return null;
    }
    return new CardDto(
            card.getFullName(),
            card.getTitle(),
            card.getCompany(),
            card.getEmail(),
            card.getAddress(),
            card.getPhone(),
            card.getWebsite(),
            card.getTagline(),
            card.getLogoDesc(),
            card.getOcrRawText()
    );
}

}

#

this is the mapper

#

package org.example.bizcardapp.Entity;

import jakarta.persistence.;
import lombok.
;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@Table
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Setter
public class Card {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;


private String fullName;
private String email;
private String title;
private String phone;
private String company;
private String website;
private String address;
private String tagline;
private String logoDesc;
private String ocrRawText;
private LocalDate dateCreated;
private LocalDateTime timeStamp;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

}

#

this is the entity

#

package org.example.bizcardapp.dto;

public record CardDto (
String name,
String title,
String company,
String email,
String phone,
String website,
String address,
String tagline,
String logoDesc,
String ocrRawText

) {

}

#

this is the dto

#

This is the whole code

obsidian shell
#

and when you were learning java, did you jump straight into spring?

#

how long have you been at this?

ashen sedge
#

no I did not, I learn the basics as well

#

around 3 months already

obsidian shell
#

okay

#

can you make a new project, make it a maven project and call it BizCardAppDetour

#

or

#

well okay i want to bring you through a longer tutorial but i dont really have the time do give it

#

so for now just do file uploads in the controller

#

whatever thing you saw online explaining that is "right" in how to do it

ashen sedge
#

For now I will do the controller

#

If you can reach out to this message again tomorrow I'd be happy to hear from you