想利用MultipartRequest的getFileName方法來一次獲取多個上傳的文件名字時,得到的不是文件的名字,而是 input 的name屬性
最后找到了答案,解決方法,參照http://stackoverflow.com/questions/13946859/retrieve-the-file-name-while-using-file-type-input
原來的代碼
@RequestMapping(value = {"multipleFileUpload"}, method = {RequestMethod.GET, RequestMethod.POST}) public String multipleFileUpload( ModelMap modelMap, MultipartHttpServletRequest request, HttpServletResponse response) throws IOException { Iterator<String> itr = request.getFileNames(); while(itr.hasNext()){ String str = itr.next(); //這個文件並不是原來的文件名 multipartFile = (CommonsMultipartFile)request.getFile(str); MultipartFile mpf = request.getFile(str); InputStream is = mpf.getInputStream(); byte[] bytes = IOUtils.toByteArray(is); CompressWorker worker = new CompressWorker(statusMap, bytes, str, compressSize, jobId); worker.start(); } modelMap.addAttribute("json", new ReturnMap("線程已經啟動")); return "json"; }
修改后的代碼
@RequestMapping(value = {"multipleFileUpload"}, method = {RequestMethod.GET, RequestMethod.POST}) public String multipleFileUpload( ModelMap modelMap, MultipartHttpServletRequest request, HttpServletResponse response) throws IOException { CommonsMultipartFile multipartFile = null; Iterator<String> itr = request.getFileNames(); while(itr.hasNext()){ String str = itr.next(); multipartFile = (CommonsMultipartFile)request.getFile(str); String fileName = multipartFile.getOriginalFilename(); //原文件名 MultipartFile mpf = request.getFile(str); InputStream is = mpf.getInputStream(); byte[] bytes = IOUtils.toByteArray(is); CompressWorker worker = new CompressWorker(statusMap, bytes, fileName, compressSize, jobId); worker.start(); } modelMap.addAttribute("json", new ReturnMap("線程已經啟動")); return "json"; }