스프링 프레임워크
1. 2개의 의존성 설정 필요
commons-fileupload, connons-io
2. jsp
<form action=”upload” method=”post” enctype=”multipart/from-data”>
comment:<input type="text" name="theText"><br>
file:<input type="file" name="theFile"><br>
3. DTO
public class UploadDTO {
String theText;
MultipartFile theFile;
4. servlet-context.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
5. Controller
@PostMapping("/upload")
public String upload(UploadDTO dto) {
String theText = dto.getTheText();
CommonsMultipartFile theFile = dto.getTheFile();
String originalFilename = theFile.getOriginalFilename();
File f = new File("c:\\upload", originalFilename) ;
theFile.transferTo(f);
}
스프링 부트
1. 2개의 의존성 설정 필요 없음
왜? spring-boot-starter-web 에 포함됨
2. JSP
<form action=”upload” method=”post” enctype=”multipart/from-data”>
comment:<input type="text" name="theText"><br>
file:<input type="file" name="theFile"><br>
3. DTO
public class UploadDTO {
String theText;
MultipartFile theFile;
}
4. application.properties 에서 파일 업로드 관련 설정
# multipart (파일 업로드 관련)
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=11MB
5. Controller
@PostMapping("/upload")
public String upload(UploadDTO dto) {
String theText = dto.getTheText();
MultipartFile theFile = dto.getTheFile();
File f = new File("c:\\upload", originalFilename);
theFile.transferTo(f);
'Framework > SPRING BOOT' 카테고리의 다른 글
springboot에서 설정 정보 파일 (0) | 2023.09.25 |
---|---|
HandlerInterceptor (0) | 2023.09.25 |
@RestController (0) | 2023.09.25 |
JSON 처리(@RequestBody) (0) | 2023.09.25 |
JSON 처리(@ResponseBody) (0) | 2023.09.25 |