GOORM-DEEP DIVE 백엔드 3회차 회고 DAY16

Cori1304·2025년 3월 19일
0

GOORM-DEEPDIVE

목록 보기
15/19

MultipartFile이용한 파일 올리기

( 수업에서 service class를 사용하지 않고 진행했기에 회고 동일하게 )

FileUploadController

@Controller
public class FileUploadController {

    @PostMapping("/single-file")
    public String singleFileUpload(@RequestParam String singleFileDescription,
                                   @RequestParam MultipartFile singleFile,
                                   Model model) {

        System.out.println("singleFileDescription = " + singleFileDescription);
        System.out.println("singleFile = " + singleFile);

        /* 서버로 전달 된 File을 서버에서 설정하는 경로에 지정한다. */
        String root = "src/main/resources/static";
        String filePath = root + "/uploadFiles";
        File dir = new File(filePath);
        System.out.println(dir.getAbsoluteFile());

        if(!dir.exists()) {
            dir.mkdirs();
        }

        /* 파일명 변경 처리 */
        String originFileName = singleFile.getOriginalFilename();
        String ext = originFileName.substring(originFileName.lastIndexOf("."));
        System.out.println("ext = " + ext);

        String savedName = UUID.randomUUID() + ext;
        System.out.println("savedName = " + savedName);

        /* 파일 저장 */
        try {
            singleFile.transferTo(new File(filePath + "/" + savedName));
            model.addAttribute("message", "파일 업로드 완료!");
        } catch (IOException e) {
            model.addAttribute("message", "파일 업로드 실패!");
        }
        return "result";
    }
}

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>파일 업로드 하기</h1>

    <h3>single file 업로드하기</h3>
    <form action="single-file" method="post" enctype="multipart/form-data">
        파일 : <input type="file" name="singleFile"><br>
        파일 설명 : <input type="text" name="singleFileDescription"><br>
        <input type="submit">
    </form>
</body>
</html>

result.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${ message }"></h1>
</body>
</html>

REST API

웹의 모든 자원에 고유한 ID인 HTTP URL을 부여하고, HTTP Method (GET, POST, PUT, DELETE)를 통해 해당 자원에 대한 CRUD 연산을 적용한다. 즉, REST는 자원 기반 구조 (ROA : Resource Oriented Architecture) 설계의 중심에 Resource가 있고 HTTP Method를 통해 Resource를 처리하도록 설계된 아키텍처를 의미한다.

REST API 설계 규칙

API는 동사가 아니라 명사를 사용하여 리소스를 식별해야 한다.
예를 들어, 유저 정보를 가져오기 위한 API는 GET /users/{id}로 설계한다.
❌ 잘못된 예: GET /getUser, POST /createUser
✅ 올바른 예: GET /users/{id}, POST /users

URI 설계 규칙 참고

URI는 리소스를 표현하는 명사를 사용해야 한다.
복수형을 사용하여 리소스를 명확히 표현한다.
❌ GET /user/1
✅ GET /users/1
URL에 동사를 포함하지 않는다.
❌ POST /users/create
✅ POST /users
계층적인 구조를 사용하여 관계를 표현한다.
❌ GET /users?id=1&posts=true
✅ GET /users/1/posts

HTTP 메서드기능예제
GET리소스 조회GET /users/{id}
POST새로운 리소스 생성POST /users
PUT기존 리소스 전체 업데이트PUT /users/{id}
PATCH기존 리소스 일부 업데이트PATCH /users/{id}
DELETE리소스 삭제DELETE /users/{id}

참고자료

profile
개발 공부 기록

0개의 댓글

관련 채용 정보