1、文件配置
配置文件解析
上传文件处理的核心方法
// uploadOneFile.jsp, uploadMultiFile.jsp submit to. @RequestMapping(value = "/doUpload", method = RequestMethod.POST) public String uploadFileHandler(HttpServletRequest request, Model model, @RequestParam("file") MultipartFile[] files) { // Root Directory. String uploadRootPath = request.getServletContext().getRealPath( "upload"); System.out.println("uploadRootPath=" + uploadRootPath); File uploadRootDir = new File(uploadRootPath); // Create directory if it not exists. if (!uploadRootDir.exists()) { uploadRootDir.mkdirs(); } // 创建 List<File> uploadedFiles = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { MultipartFile file = files[i]; // Client File Name String name = file.getOriginalFilename(); System.out.println("Client File Name = " + name); if (name != null && name.length() > 0) { try { byte[] bytes = file.getBytes(); // Create the file on server File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name); // Stream to write data to file in server. BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); uploadedFiles.add(serverFile); System.out.println("Write file: " + serverFile); } catch (Exception e) { System.out.println("Error Write file: " + name); } } } model.addAttribute("uploadedFiles", uploadedFiles); return "uploadResult"; }
也可参见一个很不错的api连接:
https://www.yiibai.com/spring_mvc/spring-mvc-file-upload-tutorial.html