springMvc的上传与下载

来源:互联网 发布:中国网络日报 编辑:程序博客网 时间:2024/05/22 14:40

springMvc的配置文件的代码

 使用springMVC包装的解析器(CommonsMultipartResolver)进行文件上传控制 需要引入 apache的 common-fileupload组件包加上common-io

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans  
  4.     xmlns="http://www.springframework.org/schema/beans"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xmlns:p="http://www.springframework.org/schema/p"  
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.       
  9.     xsi:schemaLocation="  
  10.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  11.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  12.     ">  
  13.     <!-- 配置扫描 -->  
  14.     <context:component-scan base-package="cn.et"></context:component-scan>  
  15.     <!-- 配置上传的解析器 id是写死的不能改变 -->  
[html] view plain copy
  1.  <span style="white-space:pre">    </span><!-- 名称必须使用  multipartResolver 因为spring容器使用名称注入 文件上传解析器-->  
  2.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <!-- 上传的大小限制 -->  
  4.            <property name="maxUploadSize" value="1048576"></property>  
  5.    <span style="white-space:pre">   </span></bean>  
  6. </beans>  

action的代码


[java] view plain copy
  1. @Controller  
  2. @RequestMapping(value="/day0601")  
  3. public class UploadingDownloadAction {  
  4.     //上传的默认路径  
  5.     private final String FILEPATH="E:/upload";  
  6.     //下载的默认路径  
  7.     private final String DOWNFILEPATH="E:/mydownload";  
  8.     /** 
  9.      * 文件上传 
  10.      * @param multipartFile 
  11.      * @return 
  12.      * @throws IllegalStateException 
  13.      * @throws IOException 
  14.      */  
  15.     @RequestMapping(value="/uploading.action")  
  16.     public String uploading(@RequestParam(name="uploadingFile") MultipartFile multipartFile) throws IllegalStateException, IOException{  
  17.         //获取上传的文件名  
  18.         String fileName=multipartFile.getOriginalFilename();  
  19.         //上传文件的存储路径  
  20.         String file=FILEPATH+"/"+fileName;  
  21.         //调用这个方法直接上传  
  22.         multipartFile.transferTo(new File(file));  
  23.         return "/day20170601/success.jsp";  
  24.     }  
  25.     /**  
  26.      * 文件下载  
[java] view plain copy
  1. <span style="white-space:pre">  </span>SpringMVC使用消息转换器 ByteArrayHttpMessageConverter 设置文件内容为响应体 通过设置响应头ContentDisposition 通知浏览器下载的附件名称  
  2.      * @return  
  3.      * @throws Exception  
  4.      */  
  5.     @RequestMapping(value="/download.action")  
  6.     public ResponseEntity<byte[]> fileDowload(String downloadFile) throws Exception{  
  7.             //下载文件的路径  
  8.             String fileName=DOWNFILEPATH+"/"+downloadFile;  
  9.             //创建这个文件  
  10.             File file=new File(fileName);  
  11.           //设置响应头  
  12.            HttpHeaders hh=new HttpHeaders();  
  13.            //设置下载的文件的名称  
  14.            hh.setContentDispositionFormData("attachment", URLEncoder.encode(downloadFile, "UTF-8"));  
  15.            //读取目标文件为二进制数组  
  16.            byte[] fileByte=FileCopyUtils.copyToByteArray(file);  
  17.            //构建ResponseEntity对象  
  18.            ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(fileByte, hh, HttpStatus.CREATED);  
  19.            return re;  
  20.   
  21.     }  
  22. }  


jsp的代码

编写jsp文件跳转的动作只能是form表单,并且添加属性enctype="multipart/form-data"必需要是post提交

[html] view plain copy
  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>My JSP 'uplodDownl.jsp' starting page</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="${pageContext.request.contextPath}/day0601/uploading.action" method="post" enctype="multipart/form-data">  
  27.         <input type="file" name="uploadingFile"/>  
  28.         <input type="submit" value="上传"/>  
  29.         <a href="${pageContext.request.contextPath}/day0601/download.action?downloadFile=A.txt">下载</a>  
  30.     </form>  
  31.   </body>  
  32. </html>  



web.xml配置

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     <!-- spring自带的解决乱码的过滤器 -->  
  8.         <filter>  
  9.         <filter-name>utf</filter-name>  
  10.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  11.         <init-param>  
  12.             <param-name>encoding</param-name>  
  13.             <param-value>UTF-8</param-value>  
  14.         </init-param>  
  15.     </filter>  
  16.     <filter-mapping>  
  17.         <filter-name>utf</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
  20.     <filter>  
  21.     <!-- 配置这个selevlet来加载sprinmvc的配置文件 -->  
  22.     <filter-name>hidden</filter-name>  
  23.     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  24.   </filter>      
  25.   <filter-mapping>  
  26.     <filter-name>hidden</filter-name>  
  27.     <url-pattern>/*</url-pattern>  
  28.   </filter-mapping>  
  29.     <servlet>  
  30.         <servlet-name>springmvc</servlet-name>  
  31.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  32.         <!-- 通过这个参数去找配置文件 不加这个参数默认在 /WEB-INF/找spservlet-name-servlet.xml这个文件-->  
  33.         <init-param>  
  34.             <param-name>contextConfigLocation</param-name>  
  35.             <param-value>classpath:/springmvc.xml</param-value>  
  36.         </init-param>  
  37.         <!-- 启动tomcat的时候就加载 -->  
  38.         <load-on-startup>0</load-on-startup>  
  39.     </servlet>  
  40.     <servlet-mapping>  
  41.         <servlet-name>springmvc</servlet-name>  
  42.         <!-- /拦截所有以.action结尾的 -->  
  43.         <url-pattern>*.action</url-pattern>  
  44.         <!-- /拦截所有servlet -->  
  45.         <url-pattern>/</url-pattern>  
  46.     </servlet-mapping>  
  47.   <welcome-file-list>  
  48.     <welcome-file>index.jsp</welcome-file>  
  49.   </welcome-file-list>  
  50. </web-app>
原创粉丝点击