Notice
Recent Posts
Recent Comments
Link
관리 메뉴

데브마우스

[Spring] 파일 업로드 개요 정리 본문

Spring/Spring: 정리

[Spring] 파일 업로드 개요 정리

데브마우스 2024. 1. 23. 00:21

스프링 파일 업로드란 무엇인가요?

웹 브라우저를 통해 모두 사진이나 파일을 업로드 해본 경험이 있을것입니다. 그렇다면 스프링 프레임워크에서 파일을 업로드 하기 위해서는 어떻게 해야할까요? 이번 포스팅에서 알아보도록 하겠습니다.

 

스프링 파일 업로드를 하기 위해서는 무엇이 필요한가요?

파일 업로드를 위해서는 2가지 준비가 필요합니다.

 

pom.xml 파일에 의존 라이브러리 등록하기

		<!-- 파일 업로드를 위한 의존 라이브러리-->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.11.0</version>
		</dependency>

 

servlet-context.xml 파일에 시큐리티 필터 등록하기

	<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<beans:property name="maxUploadSize" value="100000" />
		<beans:property name="defaultEncoding" value="utf-8" />
		<beans:property name="uploadTempDir" ref="uploadDirResource"/>
	</beans:bean>
	<beans:bean id="uploadDirResource" class="org.springframework.core.io.FileSystemResource">
		<beans:constructor-arg value="c:/upload/"/>
	</beans:bean>

 

파일 업로드를 위한 폼 태그 양식

웹 브라우저에서 서버로 파일을 전송하기 위해서는 JSP 페이지와 JSP 페이지에 form 태그가 필요합니다. form 태그 형식은 아래와 같습니다.

<form method="POST" enctype="multipart/form-data">
	<input type="file" name="요청 매개변수 이름">
</form>