개발 무지렁이

[Spring Boot] 때에 따라 업로드 개수가 다를 경우, MultipartRequest 본문

Backend/스프링부트

[Spring Boot] 때에 따라 업로드 개수가 다를 경우, MultipartRequest

Gaejirang-e 2022. 12. 26. 16:57

[article/write.html]

<!DOCTYPE html>
<html lang="ko"
      xmlns:th="http://thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/layout.html}"
>
<head>
    <title>글쓰기</title>
</head>

<body>
<section layout:fragment="content">
    <div class="container">
        <h1>글쓰기</h1>

        <form th:action method="post" enctype="multipart/form-data" onsubmit="WriteForm__submit(this); return false;">
            <div>
                <span>제목</span>
                <input type="text" name="subject">
            </div>
            <div>
                <span>내용</span>
                <textarea name="content"></textarea>
            </div>
            <div>
                <span>본문이미지</span>
                <input type="file" name="bodyImg__1" accept="image/png, image/gif, image/jpeg">
                <input type="file" name="bodyImg__2" accept="image/png, image/gif, image/jpeg">
            </div>
            <div>
                <input type="submit" value="작성">
            </div>
        </form>
    </div>
</section>
</body>
</html>

[MemberController.java]

@Controller
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @PreAuthorize("isAuthenticated()")
    @GetMapping("/write")
    public String showWrite() {
        return "article/write";
    }

    @PreAuthorize("isAuthenticated()")
    @PostMapping("/write")
    @ResponseBody
    public String write(ArticleForm articleForm, MultipartRequest multipartRequest) {
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

        log.debug("fileMap : " + fileMap);

        return "작업중";
    }
}    
fileMap: 
{
   bodyImg__1=org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@16013bbf, 
   bodyImg__2=org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@4a6fe95d
}
Comments