Spring MVC 之文件上传

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC 之文件上传相关的知识,希望对你有一定的参考价值。

SpringMVC同样使用了apache的文件上传组件。所以需要引入以下包:

apache-commons-fileupload.jar

apache-commons-io.jar

在springAnnotation-servlet.xml中配置

1 <!-- 定义文件上传解析器 -->
2     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3         <property name="maxUploadSize" value="-1"/><!-- -1代表不限制文件大小,单位是B -->
4         <property name="defaultEncoding"  value="utf-8"/><!-- 设置字符集编码 -->
5         <property name="maxInMemorySize" value="1024"/><!-- 内存中最大内存空间,单位是B NO KB -->
6     </bean>

控制器:

 1 package com.cy.springannotation.controller;
 2 
 3 import java.io.File;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.log4j.Logger;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.multipart.commons.CommonsMultipartFile;
13 
14 @Controller
15 public class FileUploadController {
16     private Logger log = Logger.getLogger(this.getClass());
17     
18     
19     /**
20      * CommonsMultipartFile file
21      * @param file
22      * @return
23      */
24     @RequestMapping(value="/upload.do",method=RequestMethod.POST)
25     public String upload(@RequestParam("fileName") CommonsMultipartFile file,HttpServletRequest req){
26         //获取原始文件名
27         String fileName = file.getOriginalFilename();
28         log.info(fileName);
29         String path = req.getSession().getServletContext().getRealPath("upload");
30         try {
31             file.getFileItem().write(new File(path + File.separator + fileName));
32         } catch (Exception e) {
33             
34             log.error(e);
35         }
36         return "success";
37     }
38     
39 }

 上传文件页面:

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>文件上传</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26    <form action="upload.do" method="post" enctype="multipart/form-data">
27         <table>
28             <tr>
29                 <td><input type="file" name="fileName"/></td>
30             </tr>
31             <tr>
32                 <td><input type="submit" value="提交"/></td>
33             </tr>
34         </table>
35     </form>
36     
37   </body>
38 </html>

 

上传文件就可以了!

 

以上是关于Spring MVC 之文件上传的主要内容,如果未能解决你的问题,请参考以下文章

Spring Mvc 笔记二之异常和文件上传

Spring mvc ajax文件上传导致415(不支持的媒体类型)

Spring MVC文件上传

在 Angular 2 和 Spring MVC 中上传带有其他表单字段的文件

java spring mvc 客户端上传文件到服务器端

Spring MVC 文件上传功能详解