SpringMVC文件上传

  

<强> SpringMVC上传文件演示

,,,, SpringMVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver实现的.SpringMVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。因此SpringMVC的文件上传需要依赖ApacheCommons FileUpload的组件(commons-io-2.5.jar以及commons-fileupload-1.3.2.jar)。

,,,, 1。编写jsp

,,,,需要注意的点:表单的方法必须为发布方式,负责上传文件的表单编码类型必须是“多部分/格式”。

& lt; % @  page 语言=癹ava”, contentType=" text/html;, charset=utf - 8”   ,,,pageEncoding=皍tf - 8”%比;   & lt; ! DOCTYPE  html>   & lt; html>   & lt; head>   & lt; meta  http-equiv=" - type ",内容=" text/html;, charset=utf - 8”比;   & lt; title> springMVC文件上传& lt;/title>   & lt;/head>   & lt; body>   & lt; h2>文件上传& lt;/h2>   & lt; form 行动="上传",enctype=岸嗖糠?格式”,方法=皃ost”比;   & lt; table>   & lt; tr>   & lt; td>文件描述:& lt;/td>   & lt; td> & lt; input 类型=拔谋尽?name="描述"祝辞& lt;/td>   & lt;/tr>   & lt; tr>   & lt; td>请选择文件& lt;/td>   & lt; td> & lt; input  type="文件",name="文件"/祝辞& lt;/td>   & lt;/tr>   & lt; tr>   & lt; td>   & lt; input 类型=疤峤弧?value=" https://www.yisu.com/zixun/上传”/比;   & lt;/td>   & lt;/tr>   & lt;/table>      & lt;/form>   & lt;/body>   & lt;/html>

,,, 2。编写控制器


package  com.cn.controller;      import  java.io.File;   import  javax.servlet.http.HttpServletRequest;   import  org.springframework.stereotype.Controller;   import  org.springframework.web.bind.annotation.RequestMapping;   import  org.springframework.web.bind.annotation.RequestMethod;   import  org.springframework.web.bind.annotation.RequestParam;   import  org.springframework.web.multipart.MultipartFile;      @ controller   public  class  FileUploadController  {   @RequestMapping (value=" https://www.yisu.com/zixun/uploadForm ")   public  String  uploadForm (), {   return “uploadForm”;   }   @RequestMapping (value=" https://www.yisu.com/zixun/upload ",方法=RequestMethod.POST)   public  String 上传(HttpServletRequest 请求,,@RequestParam(“描述”),String 描述,   @RequestParam(“文件”),MultipartFile 文件),throws 异常{   System.out.println(描述);   如果(! file.isEmpty ()), {   ,,,,,,,//上传文件的路径   String  path =, request.getServletContext () .getRealPath (“/p_w_picpaths/?,,,,,,,,,,,,,,,,,,,,,,,,//上传文件名   String  fileName =, file.getOriginalFilename ();   File  filePath =, new 文件(路径,文件名);   如果(! filePath.getParentFile () .exists ()), {   .mkdirs filePath.getParentFile () ();   }//将文件保存到目标文件中   file.transferTo (new 文件(时间+ path  File.separator  +,文件名));   return “成功”;   其他}{   return “错误”;   }         }   }

,,, 3。配置springmvc-config。xml

,,,,这里需要配置MultipartResolver。需要注意的是:请求的编码格式必须和jsp中的pageEncoding属性一致,以便正确读取表单的内容,默认值为“iso8859-1”。

& lt; ? xml  version=" 1.0 "   ,编码=" utf - 8 " ?比;   ,& lt; beans  xmlns=" http://www.springframework.org/schema/beans "   xmlns: mvc=" http://www.springframework.org/schema/mvc "   xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance "   xmlns:上下文=" http://www.springframework.org/schema/context "   xmlns: aop=" http://www.springframework.org/schema/aop "   xsi: schemaLocation="   http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.2.xsd   http://www.springframework.org/schema/aop ,,,,   ,,,,http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ,,,   ,,,,http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd   “在,   & lt; !——, Spring 可以自动去扫描基本包下面的包或者子包下面的java文件,——比;   & lt; !——,如果扫描到春天相关的注解类,将其注册为春天的bean ——比;   & lt;上下文:component-scan 基础包=癱om.cn.controller”,/比;   & lt; bean 类=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/比;   & lt; bean 类=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/比;   & lt; bean 类=皁rg.springframework.web.servlet.view.InternalResourceViewResolver”比;   & lt; property  name=扒白骸北?   & lt; value>/web - inf/内容/& lt;/value>   & lt;/property>   & lt; property  name=昂笞骸北?   & lt; value> .jsp

SpringMVC文件上传