zoukankan      html  css  js  c++  java
  • 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.     }
  • 相关阅读:
    Web Site 与 Web Application 的区别
    Jquery获取text,areatext,radio,checkbox,select值
    C#怎么样操作world文档中的文字型窗体域?
    DataFormatString 用法
    overload和override的区别
    Apollo安装
    工控机基础
    CAN总线技术基础
    dd命令_Linux dd命令:复制(拷贝)文件,并对原文件进行转换
    Unity2021零基础入门教程
  • 原文地址:https://www.cnblogs.com/xiaolang8762400/p/7008202.html
Copyright © 2011-2022 走看看