手机网站用什么做,wordpress 页面静态化,福田小货车,wordpress插件video playe文章目录 Spring MVC 中文件上传利用 commons-fileupload 文件上传使用 Servlet 3.1 内置的文件上传功能 Spring MVC 中文件下载 Spring MVC 中文件上传 为了能上传文件#xff0c;必须将 from 表单的 method 设置为 POST#xff0c;并将 enctype 设置为 multipart/form-data… 文章目录 Spring MVC 中文件上传利用 commons-fileupload 文件上传使用 Servlet 3.1 内置的文件上传功能 Spring MVC 中文件下载 Spring MVC 中文件上传 为了能上传文件必须将 from 表单的 method 设置为 POST并将 enctype 设置为 multipart/form-data 。 实现文件上传的 “底层” 方案有 2 种 使用 Apache Commons FileUpload 包 使用 Servlet 3.1 内置的文件上传功能
无论你的底层是使用上述的哪种方案Spring MVC 都对它们作出了『包装』让 Spring MVC 中的上传文件的代码简化而统一提供一个 MultipartResolver并将 input type“file” … 类型的请求参数绑定到请求处理方法的 MultipartFile 类型的参数上。两者具体的类型有所不同 这需要提前说明以下Spring MVC 利用 Servlet 3.1 内置的文件上传功能上传文件时有个小问题。有人发现无法将它所用到的 StandardMultipartResolver 的编码从默认的 iso-8859-1 改为 UTF-8 。也有人分析在使用 tomcat 7 时会出现这个问题并认为这是 tomcat 7 的 bug 。所有简单起见建议优先考虑 commons-fileupload 方案。 具体讨论可参见stakoverflow 。 利用 commons-fileupload 文件上传
利用 commons-fileupload 文件上传需要利用引入 commons-fileupload 包它依赖于 commons-io 包
dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.5/version
/dependencySpring MVC 是通过 MultipartResolver 的 JavaBean 提供、支持文件上传功能。commons-fileupload 中该接口的实现类是 CommonsMultipartResolver 。
简单来说CommonsMultipartResolver 是 Spring MVC 去利用 commons-fileupload 实现上传功能的『桥梁』。
在 spring-web.xml 中添加如下配置让 Spring 负责创建并初始化该 Bean 。
bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolverproperty namemaxUploadSize value104857600 /property namemaxInMemorySize value4096 /property namedefaultEncoding valueUTF-8/!-- 更多配置根据具体需求进一步再学习/使用 --
/bean至此配置结束。在 Spring MVC 的 Controller 中Spring MVC 就可以将用户上传的数据绑定到 CommonsMultipartFile 类型的参数上。 注意 此处的 RequestParam() 不能省略即便是 name 与 name 一致。 RequestMapping(/upload.do)
public String upload(String username, String password,RequestParam(uploadfile) CommonsMultipartFile uploadfile) throws IOException {log.info({}, uploadfile.getName());log.info({}, uploadfile.getOriginalFilename());String path D:/ new Date().getTime() uploadfile.getOriginalFilename();uploadfile.transferTo(new File(path));return ;
}CommonsMultipartFile 支持如下功能
方法说明byte[] getBytes()以字节数组的形式返回文件的内容String getContentType()返回文件的内容类型InputStream getInputStream()返回一个 InputStream可以从中去读文件内容String getName()返回请求参数的 nameString getOriginalFilename()返回文件原本的文件名long getSize()返回文件大小单位字节boolean isEmpty()判断上传的文件是否为空void transferTo(File destination)将上传的文件保存到指定位置
如果从页面上同时上传多个文件那么页面上的 file 可以使用同一个 name而代码中则使用 CommonsMultipartFile 的数组类型的参数接受。数组中的每一个 MultipartFile 就代表着一个上传的文件。
pinput typefile namefiles/p
pinput typefile namefiles/p
pinput typefile namefiles/pRequestParam(files) CommonsMultipartFile[] files使用 Servlet 3.1 内置的文件上传功能 补充其实 Servlet 3.0 就已经开始提供内置的上传功能只不过该功能在 Servlet 3.1 中进一步增强/改进/完成。因此一般的说法是 Servlet 3.1 支持内置的文件上传功能。 利用 Servlet 3.1 实现文件上传的概念和使用过程和利用 commons-fileupload 本质上并无太大区别。只不过有几处小区别
提供文件上传功能的是 StandardServletMultipartResolver 不再是 CommonsMultipartResolver 。
spring-web.xml
bean idmultipartResolver classorg.springframework.web.multipart.support.StandardServletMultipartResolver/对上传过程中的相关配置是配置在 web.xml 中的 DispacherServlet 下而非 spring-web.xml 中的 MultipartResolver 下。
web.xml
servletservlet-namehello/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-param.../init-paramload-on-startup.../load-on-startupmultipart-configlocationd://location !-- 临时文件的目录。该目录必须存在 --max-file-size2097152/max-file-size !-- 一次请求上传的单个文件最大2M --max-request-size4194304/max-request-size !-- 一次请求上传的多个文件整体大小不超过4M --/multipart-config
/servletController 代码中使用的注解是 RequestPart(“file”)而非 RequestParam绑定的参数类型是 MultipartFile而不是 CommonsMultipartFile 。
RequestMapping(/upload.do)
public String upload(String username, String password,RequestPart(files) MultipartFile[] files) MultipartFile 对象的功能与 CommonsMultipartFile 基本类似。
Spring MVC 中文件下载
Spring MVC 提供了一个 ResponseEntity 类型使用它可以很方便地定义返回 HttpHeaders 和 HttpStatus 以实现下载功能。
RequestMapping(/download)
public ResponseEntitybyte[] download(HttpServletRequest req,RequestParam(filename) String filename, Model model) throws Exception {// 1. 准备一个字节数组字节数组的内容来源于一个文件。// 这个字节数组就是在本次 HTTP 请求中 Tomcat 要回给客户端浏览器的内容、数据。String path req.getServletContext().getRealPath(upload);File file new File(path File.separator filename);byte[] bytes FileUtils.readFileToByteArray(file);// 2. 创建一个 ResponseEntity 对象。它代表着一个 HTTP 响应。// 而一个 HTTP 响应又有行-头-体。其中『体』里存放的就是上述的代表文件内容的字节数组HttpHeaders headers new HttpHeaders();// 解决文件名乱码问题// String downloadFileName new String(filename.getBytes(UTF-8), iso-8859-1);// 第一个放在 header 中的键值对 attachmentxxxheaders.setContentDispositionFormData(attachment, filename);// 第二个放在 header 中的键值对 media-typexxxxheaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);ResponseEntitybyte[] responseEntity new ResponseEntity(bytes, // 响应体中携带的数据headers, // 响应头HttpStatus.OK); // 响应行中的状态码return responseEntity;
}
文章转载自: http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.nggry.cn.gov.cn.nggry.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.mttck.cn.gov.cn.mttck.cn