DEV/spring

단일 파일 업로드를 위한 설정 / 파일 1탄

purple 2022. 2. 3. 05:47

https://purple-j.tistory.com/213

 

파일 저장을 위한 설정

https://purple-j.tistory.com/212 main > webapp > WEB-INF > spring > servlet-context.xml에 아래 코드를 넣어준다 class="org.springframework.." data-og-host="purple-j.tistory.com" data-og-source-url="h..

purple-j.tistory.com

https://purple-j.tistory.com/214

 

다중 파일을 받아오기 위한 설정 / 파일 3탄

https://purple-j.tistory.com/213 webapp > WEB-INF > spring > servlet-context.xml에 아래 코드를 넣어준다 class="org.springframework.." data-og-host="purple-j.tistory.com" data-og-source-url="h.." dat..

purple-j.tistory.com

 

위 포스팅들과 이어지는 파일 포스팅 1탄

 

- spring web mvc 제작중

- sts4 

- tomcat 9 

- jdk 1.8

 

1. servlet-context.xml 설정

 

 

아래단 src > main > webapp > WEB-INF > spring > servlet-context.xml에 아래 코드를 넣어준다

 

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size -->
<!-- 300*1024*1024(1kb) = 300mb-->
<property name="maxUploadSize" value="314572800" />
</bean>

 

- maxUploadSize는 파일의 최대 크기 지정 위의 314572800은 300mb이다.

 

 

2. pom.xml 설정

 

pom.xml에 아래 코드를 넣어준다.

 
 
    <!-- 파일 업로드를 위함  -->
    <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

 

3. html / jsp 

- form의 enctype을 multipart/form-data로 적어준다.

 
<form action="reg" method="post" enctype="multipart/form-data">
    <tr>
        <th>첨부파일</th>
        <td colspan="3" class="text-align-left text-indent"><input type="file"
                name="file" /> </td>
    </tr>
</form>

 

4. controller

 

- 파라미터를 MultipartFile file로 받는다. 

- file.getOriginalFilename() : 등록한 파일의 이름을 얻어올 수 있다

- file.getSize() : 파일 사이즈를 얻어올 수 있다.

 

* 코드는 길지만 사실상 다 필요 없고 파라미터를 MultipartFile로 받는 부분만 적어주면 됨

 

- 붙여넣기용 코드 

@RequestMapping(value="reg", method= {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String reg(String title, String content, MultipartFile file, String category, String[] foods, String food) {
   
    long size = file.getSize();
    String fileName = file.getOriginalFilename();
    System.out.printf("fileName:%s, fileSize:%d\n", fileName, size);
   
    for(String f : foods)
        System.out.println(f);
   
    return String.format("title:%s<br>content:%s<br>category:%s", title, content, category);

}

 

 

 

 

# 참고자료

1. 파일 업로드 공부용

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/commons/CommonsFileUploadSupport.html

 

CommonsFileUploadSupport (Spring Framework 5.3.15 API)

Base class for multipart resolvers that use Apache Commons FileUpload 1.2 or above. Provides common configuration properties and parsing functionality for multipart requests, using a Map of Spring CommonsMultipartFile instances as representation of uploade

docs.spring.io

 

2. 인코딩 타입

https://www.w3schools.com/tags/att_form_enctype.asp

 

HTML form enctype Attribute

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com