zoukankan      html  css  js  c++  java
  • spring mvc -文件下载

    1、下载一个E盘存在jpg文件   

    【1】因为是spring-mvc 而且是文件上传  ,所以需要导入以下包(可能会有多余,但是绝对够用),核心jar包是(commons-io和commons-fileupload)

    【2】编写大配置文件applicationContext.xml

      <context:component-scan base-package="cn.happy.test"></context:component-scan>

    【3】编写上传文件类

    package cn.happy.test;
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.servlet.http.HttpSession;
    
    import org.apache.commons.io.FileUtils;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    public class FileUp {
        
    @RequestMapping("/download.do")  
    public ResponseEntity<byte[]> download(String filename) throws IOException {  
        HttpHeaders headers = new HttpHeaders();  
        String file="E:\"+filename;
        File file2 = new File(file);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
        System.out.println(filename);
        String charset=new String(filename.getBytes("utf-8"),"iso-8859-1");
        headers.setContentDispositionFormData("file", charset);  
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file2),  
                                          headers, HttpStatus.CREATED);  
    }  
        
    }

     在前台写一个a标签

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

    web.xml配置

    <!--编码过滤器  -->
      <filter>
          <filter-name>characterEncoding</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          
          <init-param>
              <param-name>encoding</param-name>
              <param-value>utf-8</param-value>
          </init-param>
      </filter>
      
      <filter-mapping>
          <filter-name>characterEncoding</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      
      <!-- 配置中英调度器 -->
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:applicationContext.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>

    直接拷贝就行

  • 相关阅读:
    编码问题:python写入文件
    [ExtJS5学习笔记]第三十二节 sencha extjs 5与struts2的ajax交互配置
    PS 滤镜——Skewing
    [struts2学习笔记] 第六节 struts2依赖的jar包还有Could not find action or result 错误解决
    [linux RedHat]windows下使用putty远程连接linux 下载JDK和tomcat
    一个有趣的花环图案生成函数
    【翻译】针对多种设备定制Ext JS 5应用程序
    【翻译】Ext JS最新技巧——2015-1-2
    [ExtJS5学习笔记]第三十一节 sencha extjs 5使用cmd生成的工程部署到tomcat服务器
    PhotoShop 图像处理 算法 汇总
  • 原文地址:https://www.cnblogs.com/myhome-1/p/6270301.html
Copyright © 2011-2022 走看看