springMVC上传文件

添加pom依赖

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

配置CommonsMultipartResolver

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="UTF-8"
		p:maxUploadSize="5242880"
		p:uploadTempDir="update/temp"
	/>

上传页面

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <title>上传</title>
   <script type="text/javascript">
   	
   </script>
  </head>
  
  <body>
  
  	<form action="<c:url value="/user/upload"/>" method="post" enctype="multipart/form-data">
  		<input type="text" name="name"/>
  		<input type="file" name="file"/>
  		<input type="submit" />
  	</form>  
  </body>
</html>

业务类

	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String uploadPage(@RequestParam("name") String name,@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
		if (!file.isEmpty()) {
			file.transferTo(new File("f:/temp/"+file.getOriginalFilename()));
			return "redirect:success";
		}
		return "user/uploadPage";
	}




你可能感兴趣的:(springMVC,上传文件,MultipartFile)