티스토리 뷰

Framework/SPRING BOOT

file upload

xoo | 수진 2023. 9. 25. 17:31

스프링 프레임워크

 

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
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함