[Spring] 파일 업로드 setting

Whatever·2022년 2월 10일
0

Spring(스프링)

목록 보기
15/29

1. 파일업로드를 위한 라이브러리 다운로드

commons-fileupload


imgscalr-lib


Jackson Databind

thumbnailator

2. pom.xml에 붙여넣기

		<!-- json 데이터 바인딩을 위한 의존 라이브러리 -->
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.13.1</version>
		</dependency>

3. web.xml 설정하기

1) 서블릿을 3.1버전으로 변경

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

2) servlet안에 넣어주기

		<!-- 파일업로드 설정 시작 -->
		<multipart-config>
			<location>C:\\upload</location><!-- 업로드 되는 파일을 저장할 공간 -->
			<max-file-size>20971520</max-file-size><!-- 최대크기 1MB * 20 -->
			<max-request-size>41943040</max-request-size><!-- 한번에 올릴 수 있는 최대크기 1MB * 40 -->
			<file-size-threshold>20971520</file-size-threshold><!-- 메모리 사용 1MB * 20 -->
		</multipart-config>
		<!-- 파일업로드 설정 끝 -->

web.xml 전체 코드

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">


	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<!-- web.xml 설정 : Tomcat 자체설정 -->
		<!-- 파일업로드 설정 시작 -->
		<multipart-config>
			<location>C:\\upload</location><!-- 업로드 되는 파일을 저장할 공간 -->
			<max-file-size>20971520</max-file-size><!-- 최대크기 1MB * 20 -->
			<max-request-size>41943040</max-request-size><!-- 한번에 올릴 수 있는 최대크기 1MB * 40 -->
			<file-size-threshold>20971520</file-size-threshold><!-- 메모리 사용 1MB * 20 -->
		</multipart-config>
		<!-- 파일업로드 설정 끝 -->
		
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 한글설정 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 한글설정 END -->



</web-app>

4. servlet-context 설정하기

	<!-- 첨부파일을 처리하는 빈 설정 -->
	<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
	</beans:bean>

위 코드 추가하기

5. context.xml 설정하기

1) Servers > context.xml 접속

2) context.xml 설정

context에 allowCasualMultipartParsing="true" path="/" 추가
Resources의 cashingAllowed="true" cacheMaxSize="100000" 추가

<Context allowCasualMultipartParsing="true" path="/">
	<Resources cashingAllowed="true" cacheMaxSize="100000" />
    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>

0개의 댓글