2.經過FileItem對象能夠輕鬆得到CommonsMultipartFile對象,轉換成MultiPartFile對象便可使用。1.首先獲取FileItem對象: public FileItem createFileItem(String filePath) { FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "textField"; int num = filePath.lastIndexOf("."); String extFile = filePath.substring(num); FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName"); File newfile = new File(filePath); int bytesRead = 0; byte[] buffer = new byte[8192]; try { FileInputStream fis = new FileInputStream(newfile); OutputStream os = item.getOutputStream(); while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } return item; }
參考: http://www.javashuo.com/article/p-mqrxxvku-cd.htmlMultipartFile mfile = new CommonsMultipartFile(fileItem)