Spring - 파일 업로드

Programming/Java 2017.06.18 10:39 Posted by 파란크리스마스

출처 : [Spring, Tomcat] Could not parse multipart servlet request Unable to process parts as no multi-part configuration has been provided 해결 [소림사의 홍반장!]
[Apache/Tomcat] Context.xml 수정을 통한 문제 해결 - Cache, CSRF(Upload) [PostIT]
[Spring] Multipart를 사용한 페이지에서 Filter 적용하기 : 네이버 블로그

pom.xml

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.2</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.5</version>
		</dependency>

MultipartFilter 적용(web.xml)

	<!-- MultipartFilter 적용 -->
	<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>

MultipartResolver 등록 (spring bean 등록)

	<!--  이미지 드래그 앤 드롭 업로드 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- max upload size in bytes -->
		<property name="maxUploadSize" value="209715200" /> <!-- 20MB -->
		<!-- max size of file in memory (in bytes) -->
		<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
	</bean>

allowCasualMultipartParsing 설정 (context.xml)

방법1 %TOMCAT%/conf/context.xml - Tomcat 전체 적용

<Context allowCasualMultipartParsing="true">

	<WatchedResource>WEB-INF/web.xml</WatchedResource>
	<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

	<!-- 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="" /> -->

	<!-- 케시문제 해결 -->
	<Resources cachingAllowed="true" cacheMaxSize="100000"></Resources>

</Context>

방법2 WebContent\META-INF\context.xml - Eclipse 사용시 프로젝트에 적용

<?xml version="1.0" encoding="UTF-8"?>
<Context allowCasualMultipartParsing="true">
</Context>

form의 enctype

<form action="<c:url value="/insertCompleteForProblem.do"/>" method="post" enctype="multipart/form-data">
	    <tr>
	        <th scope="row">file</th>
	        <td><input type="file" size="20"  name="file1"></td>
	    </tr>

controller

	@RequestMapping(value = "/insertCompleteForProblem.do", method = RequestMethod.POST, headers = ("content-type=multipart/*"))
	public String joinComplete(
			@RequestParam("seq") int seq, 
			@RequestParam("quiz_name") String quiz_name, 
			@RequestParam("user_id") String user_id, 
			@RequestParam("price") String price,
			@RequestParam(value = "file1", required = false) MultipartFile mediaFile, 
			HttpServletRequest request, 
			ModelMap modelMap) throws Exception 
	{

		System.out.println("seq : " + seq);
		System.out.println("quiz_name : " + quiz_name);
		System.out.println("user_id : " + user_id);
		System.out.println("price : " + price);

		if (mediaFile.isEmpty() == false) {
			System.out.println("------------- file start -------------");
			System.out.println("name : " + mediaFile.getName());
			System.out.println("filename : " + mediaFile.getOriginalFilename());
			System.out.println("size : " + mediaFile.getSize());
			System.out.println("-------------- file end --------------\n");

			String originalFilename = mediaFile.getOriginalFilename(); // 파일명

			// 파일 확장자 추출
			String fileExt = FileUtils.getFileExt(originalFilename);

			UUID uuid = UUID.randomUUID();
			String targetFilename = uuid.toString() + "." + fileExt.toLowerCase();

			File file = new File(getDestinationLocation() + targetFilename);
			mediaFile.transferTo(file);
		}

		return "redirect:/main.do";
	}

	private String getDestinationLocation() {
		// return "/home/hosting_users/terecal/www/intel_images/";
		return "c:\\test\\";
	}


저작자 표시
신고


 

티스토리 툴바