https://blog.csdn.net/linzhiqiang0316/article/details/77016997html
****************************************************************前端
boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判斷是不是表單文件類型 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(factory); List items = sfu.parseRequest(request);//從request獲得全部上傳域的列表 for(Iterator iter = items.iterator();iter.hasNext();){ FileItem fileitem =(FileItem) iter.next(); if(!fileitem.isFormField()&&fileitem!=null){//判讀不是普通表單域便是file System.out.println("name:"+fileitem.getName()); } }
<html> <body> <form action="/upload" method="POST" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="Upload"/> </form> </body> </html>
後臺接收代碼:spring
/** * 文件上傳具體實現方法; * * @param file * @return */ @RequestMapping("/upload") @ResponseBody public String handleFileUpload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { /* * 這段代碼執行完畢以後,圖片上傳到了工程的跟路徑; 你們本身擴散下思惟,若是咱們想把圖片上傳到 * d:/files你們是否能實現呢? 等等; * 這裏只是簡單一個例子,請自行參考,融入到實際中可能須要你們本身作一些思考,好比: 一、文件路徑; 二、文件名; * 三、文件格式; 四、文件大小的限制; */ BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File( file.getOriginalFilename()))); System.out.println(file.getName()); out.write(file.getBytes()); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } return "上傳成功"; } else { return "上傳失敗,由於文件是空的."; } }
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <form method="POST" enctype="multipart/form-data" action="/batch/upload"> <p>文件1:<input type="text" name="id" /></p> <p>文件2:<input type="text" name="name" /></p> <p>文件3:<input type="file" name="file" /></p> <p><input type="submit" value="上傳" /></p> </form> </body> </html>
後臺接收代碼:app
@RequestMapping(value = "/batch/upload", method = RequestMethod.POST) @ResponseBody public String handleFileUpload(HttpServletRequest request) { MultipartHttpServletRequest params=((MultipartHttpServletRequest) request); List<MultipartFile> files = ((MultipartHttpServletRequest) request) .getFiles("file"); String name=params.getParameter("name"); System.out.println("name:"+name); String id=params.getParameter("id"); System.out.println("id:"+id); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream( new File(file.getOriginalFilename()))); stream.write(bytes); stream.close(); } catch (Exception e) { stream = null; return "You failed to upload " + i + " => " + e.getMessage(); } } else { return "You failed to upload " + i + " because the file was empty."; } } return "upload successful"; }