标签:new csdn and 千万 turn 功能 rtc 1.7 ras
https://blog.csdn.net/linzhiqiang0316/article/details/77016997
****************************************************************
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>
后台接收代码:
/** * 文件上传具体实现方法; * * @param file * @return */ @RequestMapping("/upload") @ResponseBody public String handleFileUpload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { /* * 这段代码执行完毕之后,图片上传到了工程的跟路径; 大家自己扩散下思维,如果我们想把图片上传到 * d:/files大家是否能实现呢? 等等; * 这里只是简单一个例子,请自行参考,融入到实际中可能需要大家自己做一些思考,比如: 1、文件路径; 2、文件名; * 3、文件格式; 4、文件大小的限制; */ 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>
后台接收代码:
@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"; }
如何用SpringBoot框架来接收multipart/form-data文件
标签:new csdn and 千万 turn 功能 rtc 1.7 ras
原文地址:https://www.cnblogs.com/zhao1949/p/9681701.html