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>

    直接拷贝就行

  • 相关阅读:
    [BZOJ1666][Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
    [BZOJ1692][Usaco2007 Dec]队列变换
    [BZOJ1599][Usaco2008 Oct]笨重的石子
    [BZOJ1603][Usaco2008 Oct]打谷机
    [BZOJ1614][Usaco2007 Jan]Telephone Lines架设电话线
    [BZOJ1617][Usaco2008 Mar]River Crossing渡河问题
    [BZOJ1612][Usaco2008 Jan]Cow Contest奶牛的比赛
    [BZOJ1600][Usaco2008 Oct]建造栅栏
    [BZOJ1230][Usaco2008 Nov]lites 开关灯
    hdu 2199 Can you solve this equation? 二分
  • 原文地址:https://www.cnblogs.com/myhome-1/p/6270301.html
Copyright © 2011-2022 走看看