- 文件上传的前端设置
A form 表单的 enctype 取值必须是: multipart/form-data(默认值是:application/x-www-form-urlencoded) enctype:是表单请求正文的类型
B method 属性取值必须是 Post
C 提供一个文件选择域<input type=”file” /> - 借助第三方组件实现文件上传
- 编写前端代码
<form action="/fileUpload" method="post" enctype="multipart/form-data"> 名称: <input type="text" name="picname"/><br/> 图片: <input type="file" name="uploadFile"/><br/> <input type="submit" value="上传"/> </form>
- 编写后端代码
@RequestMapping("/fileupload")
public String fileupload(HttpServletRequest request, MultipartFile upload) throws IOException {
// 上传位置
String path = request.getSession().getServletContext().getRealPath("/uploads/");
// 判断该路径是否存在
File file = new File(path);
if (!file.exists()) {
// 创建该文件夹
file.mkdirs();
}
// 获取上传文件的名称
String filename = upload.getOriginalFilename();
// 设置文件名为唯一值
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
// 完成文件上传
upload.transferTo(new File(path, filename));
return "success";
} - 编写文件解析器(在springmvc.xml中)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为 5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean> - 跨服务器上传文件
A:导入需要依赖<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.18.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version> </dependency>
B:
@RequestMapping("/fileupload") public String fileupload(HttpServletRequest request, MultipartFile upload) throws IOException { // 定义上传服务器的路径 String path = "http://localhost:9090/uploads/"; // 获取上传文件的名称 String filename = upload.getOriginalFilename(); // 设置文件名为唯一值 String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid + "_" + filename; // 创建客服端的对象 Client client = Client.create(); // 和上传服务器进行连接 WebResource webResource = client.resource(path + filename); // 上传文件 webResource.put(upload.getBytes()); return "success"; }
- 原始上传文件
1 @Controller 2 @RequestMapping("/file") 3 public class FileUploadController { 4 5 /** 6 * 原始文件上传 7 * @param request 8 * @return 9 */ 10 @RequestMapping("/fileupload") 11 public String fileupload(HttpServletRequest request) { 12 // 使用fileupload组件上传文件 13 // 上传位置 14 String path = request.getSession().getServletContext().getRealPath("/uploads/"); 15 // 判断该路径是否存在 16 File file = new File(path); 17 if (!file.exists()) { 18 // 创建该文件夹 19 file.mkdirs(); 20 } 21 22 // 解析request对象,获取上传文件项 23 DiskFileItemFactory factory = new DiskFileItemFactory(); 24 ServletFileUpload upload = new ServletFileUpload(factory); 25 // 解析request 26 List<FileItem> items = null; 27 try { 28 items = upload.parseRequest(request); 29 // 遍历 30 for (FileItem item : items) { 31 // 进行判断,当前item对象是否是文件项 32 if (item.isFormField()) { 33 // 说明是普通表单项 34 } else { 35 // 说明是上传文件项 36 // 获取上传文件的名称 37 String filename = item.getName(); 38 // 设置文件名为唯一值 39 String uuid = UUID.randomUUID().toString().replace("-", ""); 40 filename = uuid + "_" + filename; 41 // 完成文件上传 42 item.write(new File(path, filename)); 43 // 删除临时文件 44 item.delete(); 45 } 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 51 return "success"; 52 } 53 54 }