Today I Learned
QnA ๋ต๊ธ
- Reply
File Upload
- Client ์ปดํจํฐ์ ์ ์ฅ๋์ด ์๋ ํ์ผ(์ด๋ฏธ์ง, ๋์์, ๋ฌธ์ ๋ฑ)์ Server ์ปดํจํฐ๋ก ์ ์ก
1. ์ค๋น์ฌํญ
1) Library ๋ค์ด(pom.xml)
- commons-fileupload
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
2) form ํ๊ทธ์ method ํ์์ post, enctype="multipart/form-data"๋ก ๋ณ๊ฒฝ
- ์ฒจ๋ถํ์ผ์ด ์๋ ํผ์ enctype์ ๊ธฐ๋ณธ์ผ๋ก ์ค์
3) Server์์ ์ฌ๋ฌ ๊ฐ์ ํ์ผ ์กฐ๊ฐ๋ค์ ํ๋๋ก ํฉ์ณ์ฃผ๋ ์ญํ ์ ํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑ
- ***-context.xml
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- ์ ์ฒด Uploadํ๋ ํ์ผ์ ์ด ํฌ๊ธฐ ์ ํ ๋จ์ byte -->
<property name="maxUploadSize" value="10485760"></property>
<!-- uploadํ๋ ํ์ผ๋น ํฌ๊ธฐ ์ ํ ๋จ์ byte -->
<property name="maxUploadSizePerFile" value="5242880"></property>
</bean>
4) form ํ๊ทธ์ ํ์ผ ํ๊ทธ ์ถ๊ฐ
5) Controller์ ๋ฉ์๋
- ๋งค๊ฐ๋ณ์๋ก MultipartFile ์ ์ธ
- ๋ณ์๋ช
์ ํ๋ผ๋ฏธํฐ ์ด๋ฆ๊ณผ ๋์ผ
- ๊ฐ์ ๋ณ์๋ช
(ํ๋ผ๋ฏธํฐ)๋ก ์ฌ๋ฌ๊ฐ์ ํ์ผ์ ์
๋ก๋ํ ๋๋ multipart [] ์ ์ธ
728x90