springMVC之文件上传

本文介绍如何在SpringMVC框架中实现多文件上传功能,包括所需Jar包的引入、配置文件设置、控制器方法编写及JSP页面设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



来自:http://blog.youkuaiyun.com/zdp072/article/details/38693003

1. 引入Jar包

commons-fileupload-1.2.2.jar

commons-io-2.1.jar


2.user-servlet.xml中配置文件上传

  1. <!-- 配置文件上传 -->  
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <property name="maxUploadSize" value="5000000"></property>  
  4. </bean>  
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="5000000"></property>
</bean>
3. UserController.java

  1. @Controller  
  2. @RequestMapping("/user")  
  3. public class UserController {  
  4.     // 使用map模拟数据库   
  5.     private Map<String, User> userMap = new HashMap<String, User>();  
  6.   
  7.     public UserController() {  
  8.         userMap.put("zhangsan"new User("zhangsan""123"));  
  9.         userMap.put("lishimin"new User("lishimin""456"));  
  10.     }  
  11.       
  12.     // 文件上传  
  13.     // 访问方法: http://localhost/springmvc_user/upload.jsp  
  14.     @RequestMapping(value="/upload",method=RequestMethod.POST)  
  15.     public String upload(@RequestParam("attachs")MultipartFile[] attachs,HttpServletRequest req) throws IOException {  
  16.         String realpath = req.getSession().getServletContext().getRealPath("/resources/upload");  
  17.         System.out.println(realpath);  
  18.         for(MultipartFile attach:attachs) {  
  19.             if(attach.isEmpty()){  
  20.                 continue;  
  21.             }  
  22.             File file = new File(realpath + "/" + attach.getOriginalFilename());   
  23.             FileUtils.copyInputStreamToFile(attach.getInputStream(),file);  
  24.         }  
  25.         return "success";  
  26.     }  
  27.       
  28. }  
@Controller
@RequestMapping("/user")
public class UserController {
	// 使用map模拟数据库 
	private Map<String, User> userMap = new HashMap<String, User>();

	public UserController() {
		userMap.put("zhangsan", new User("zhangsan", "123"));
		userMap.put("lishimin", new User("lishimin", "456"));
	}
	
	// 文件上传
	// 访问方法: http://localhost/springmvc_user/upload.jsp
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(@RequestParam("attachs")MultipartFile[] attachs,HttpServletRequest req) throws IOException {
		String realpath = req.getSession().getServletContext().getRealPath("/resources/upload");
		System.out.println(realpath);
		for(MultipartFile attach:attachs) {
			if(attach.isEmpty()){
				continue;
			}
			File file = new File(realpath + "/" + attach.getOriginalFilename()); 
			FileUtils.copyInputStreamToFile(attach.getInputStream(),file);
		}
		return "success";
	}
	
}
4. upload.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>文件上传</title>  
  7. </head>  
  8. <body>  
  9. <form method="post" action="user/upload" enctype="multipart/form-data">  
  10.     Attach:<br/>  
  11.     <input type="file" name="attachs"/><br/>      
  12.     <input type="file" name="attachs"/><br/>      
  13.     <input type="file" name="attachs"/><br/>      
  14.     <input type="submit" value="上传文件"/>  
  15. </form>  
  16. </body>  
  17. </html>  
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<form method="post" action="user/upload" enctype="multipart/form-data">
	Attach:<br/>
    <input type="file" name="attachs"/><br/>	
    <input type="file" name="attachs"/><br/>	
    <input type="file" name="attachs"/><br/>	
    <input type="submit" value="上传文件"/>
</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值