FileUpload Multipart 설정 (Tomcat)

Heejun Byun·2022년 11월 4일
0

개념정리

목록 보기
1/4
post-thumbnail

✔ Multipart 파일 업로드를 위한 프로젝트 설정 및 Tomcat 설정

1. maven dependency 주입

  • pom.xml

    <!-- File upload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>
    
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>

2. MultipartFilter 선언 및 url-pattern 지정

  • 프로젝트/src/webapp/WEB-INF/web.xml
<filter>
	<filter-name>MultipartFilter</filter-name>
	<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>MultipartFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

3. MultipartResolver bean 주입

  • dispatcher-servlet.xml
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	    <!-- <property name="maxUploadSize" value="100000000" />
		<property name="maxInMemorySize" value="100000000" /> -->
	</bean>

※ 파일 업로드 시 오류 발생

  • 오류내용
		org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: 어떤 multi-part 설정도 제공되지 않았기 때문에, part들을 처리할 수 없습니다.
			at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112)
			at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86)
			at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:80)
			at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:112)
			at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
			at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
			at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
			at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
			at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
			at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
			at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
			at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
			at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
			at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
			at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
			at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
			at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
			at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:880)
			at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1601)
			at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
			at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
			at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
			at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
			at java.lang.Thread.run(Unknown Source)
		Caused by: java.lang.IllegalStateException: 어떤 multi-part 설정도 제공되지 않았기 때문에, part들을 처리할 수 없습니다.
			at org.apache.catalina.connector.Request.parseParts(Request.java:2803)
			at org.apache.catalina.connector.Request.getParts(Request.java:2771)
			at org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1098)
			at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:93)
			... 23 more코드를 입력하세요
  • 오류가 난 이유

    Tomcat이 multipart/form-data 요청 본문을 분석하지 못하여 경로를 찾지 못함

4. tomcat - context.xml

<Context allowCasualMultipartParsing="true" path="/">
	<Resources cachingAllowed="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>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>

5. tomcat - web.xml

    <servlet>
		...
		<multipart-config>
		    <!-- 50MB max -->
			<max-file-size>52428800</max-file-size>
			<max-request-size>52428800</max-request-size>
			<file-size-threshold>0</file-size-threshold>
		</multipart-config>
    </servlet>

6. tomcat - server.xml

<Connector ... maxPostSize="52428800" ... />
  • Post 전송 시 데이터 크기 최대 사이즈 지정

    maxPostSize="52428800" // 50MB
    maxPostSize="-1" // 디폴트값 해제

profile
반갑습니다. 개발자 변희준입니다.

0개의 댓글