1. 引入Jar包
commons-fileupload-1.2.2.jar
commons-io-2.1.jar
2.user-servlet.xml中配置文件上传
3. UserController.java
@Controller@RequestMapping("/user")public class UserController { // 使用map模拟数据库 private Map4. upload.jspuserMap = new HashMap (); 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"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>文件上传
版权声明:本文博客原创文章,博客,未经同意,不得转载。