//单个文件 input 中的name="xx",MultipartFile xx必须一致 @RequestMapping("upload") public String upload(HttpServletRequest request,@RequestParam MultipartFile name,Model model) throws IllegalStateException, IOException { System.out.println("------------------开始上传文件---------------------"); String path = request.getSession().getServletContext().getRealPath("upload"); //获得文件名字 String filename = name.getOriginalFilename(); if(null != filename && filename.length() != 0){ //获得上传文件.gpj; String suffix = filename.substring(filename.lastIndexOf("."),filename.length()); UUID uuid = UUID.randomUUID(); String newFileName = uuid+suffix; File targetFile = new File(path,newFileName); if(!targetFile.exists()){ targetFile.mkdirs(); } name.transferTo(targetFile); model.addAttribute("path", path+File.separator+newFileName); } return "show"; } //多个文件 input name 不同 的多文件上传 @RequestMapping("uploads") public String uploads(HttpServletRequest request,Model model) throws IllegalStateException, IOException { MultipartHttpServletRequest msr = (MultipartHttpServletRequest)request; List name = msr.getFiles("name"); List file = msr.getFiles("file"); List jpg = msr.getFiles("jpg"); System.out.println("------------------开始上传文件---------------------"); String path = request.getSession().getServletContext().getRealPath("upload"); for(int i = 0; i < name.size(); i++){ if(null != name && !name.isEmpty()){ if(!name.get(i).isEmpty()){ //获得上传文件.gpj; String filename = name.get(i).getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf("."),filename.length()); UUID uuid = UUID.randomUUID(); String newFileName = uuid+suffix; File targetFile = new File(path,newFileName); if(!targetFile.exists()){ targetFile.mkdirs(); } name.get(i).transferTo(targetFile); } } } return "show"; } //多个文件 input 中的name="xx",MultipartFile xx必须一致 name 相同 @RequestMapping("uploads/name") public String uploadsName(HttpServletRequest request,@RequestParam MultipartFile[] name,Model model) throws IllegalStateException, IOException { System.out.println("------------------开始上传文件---------------------"); String path = request.getSession().getServletContext().getRealPath("upload"); //获得文件名字 int len = name.length; if(len > 0 ){ for(int i = 0 ; i < len; i++){ String filename = name[i].getOriginalFilename(); if(null != filename && filename.length() != 0){ //获得上传文件.gpj; String suffix = filename.substring(filename.lastIndexOf("."),filename.length()); UUID uuid = UUID.randomUUID(); String newFileName = uuid+suffix; File targetFile = new File(path,newFileName); if(!targetFile.exists()){ targetFile.mkdirs(); } name[i].transferTo(targetFile); model.addAttribute("path", path+File.separator+newFileName); } } } return "show"; } //原生的上传文件 @RequestMapping(value="/upload",method=RequestMethod.POST) public String upload(HttpServletRequest req) throws Exception { MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req; MultipartFile file = mreq.getFile("file"); String fileName = file.getOriginalFilename(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+ "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.'))); fos.write(file.getBytes()); fos.flush(); fos.close(); return "hello"; }