Spring MVC文件上传下载(转载)

原文地址:

http://www.cnblogs.com/WJ-163/p/6269409.html 上传参考

http://www.cnblogs.com/lonecloud/p/5990060.html 下载参考

一、关键步骤

①引入核心JAR文件

SpringMVC实现文件上传,需要再添加两个jar包。一个是文件上传的jar包,一个是其所依赖的IO包。这两个jar包,均在Spring支持库的org.apache.commons中。

 ②书写控制器方法

applicationContext.xml

注:必须创建MultipartFile实例。要不出现500错误

index.jsp页面:需指定 enctype="multipart/form-data 

1

2

3

4

5

6

7

<body>

   <form action="${pageContext.request.contextPath }/first.do" method="post" enctype="multipart/form-data">

   <h2>文件上传</h2>

                文件:<input type="file" name="uploadFile"/><br/><br/>

      <input type="submit" value="上传"/>

   </form>

 </body>

实现效果:  

 

 二、没有选择要上传的文件&&限制文件上传类型

 如果没有选择要上传的文件,可以通过如下判断代码回到错误页,并配置异常类

1

2

3

4

<!-- 配置异常类  报错 -->

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

    <property name="defaultErrorView" value="/error.jsp"></property>

    </bean>

 

   

三、多文件上传 

 

实现效果:

 四、文件下载

 

1

<a href="${pageContext.request.contextPath }/download.do?line.jpg">下载</a>

 实现效果:

 

 

下载不采用这种方式,参考下面这段代码:

  1. /**
  2.      * 文件下载
  3.      * @Description:
  4.      * @param fileName
  5.      * @param request
  6.      * @param response
  7.      * @return
  8.      */
  9.     @RequestMapping("/download")
  10.     public String downloadFile(@RequestParam("fileName") String fileName,
  11.             HttpServletRequest request, HttpServletResponse response) {
  12.         if (fileName != null) {
  13.             String realPath = request.getServletContext().getRealPath(
  14.                     "WEB-INF/File/");
  15.             File file = new File(realPath, fileName);
  16.             if (file.exists()) {
  17.                 response.setContentType("application/force-download");// 设置强制下载不打开
  18.                 response.addHeader("Content-Disposition",
  19.                         "attachment;fileName=" + fileName);// 设置文件名
  20.                 byte[] buffer = new byte[1024];
  21.                 FileInputStream fis = null;
  22.                 BufferedInputStream bis = null;
  23.                 try {
  24.                     fis = new FileInputStream(file);
  25.                     bis = new BufferedInputStream(fis);
  26.                     OutputStream os = response.getOutputStream();
  27.                     int i = bis.read(buffer);
  28.                     while (i != -1) {
  29.                         os.write(buffer, 0, i);
  30.                         i = bis.read(buffer);
  31.                     }
  32.                 } catch (Exception e) {
  33.                     // TODO: handle exception
  34.                     e.printStackTrace();
  35.                 } finally {
  36.                     if (bis != null) {
  37.                         try {
  38.                             bis.close();
  39.                         } catch (IOException e) {
  40.                             // TODO Auto-generated catch block
  41.                             e.printStackTrace();
  42.                         }
  43.                     }
  44.                     if (fis != null) {
  45.                         try {
  46.                             fis.close();
  47.                         } catch (IOException e) {
  48.                             // TODO Auto-generated catch block
  49.                             e.printStackTrace();
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         return null;
  56.     }

 

posted on 2017-06-14 12:37  张小贱1987  阅读(200)  评论(0编辑  收藏  举报

导航