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>

    直接拷贝就行

  • 相关阅读:
    自然语言交流系统 phxnet团队 创新实训 项目博客 (十一)
    install ubuntu on Android mobile phone
    Mac OS, Mac OSX 与Darwin
    About darwin OS
    自然语言交流系统 phxnet团队 创新实训 项目博客 (十)
    Linux下编译安装qemu和libvirt
    libvirt(virsh命令总结)
    深入浅出 kvm qemu libvirt
    自然语言交流系统 phxnet团队 创新实训 项目博客 (九)
    自然语言交流系统 phxnet团队 创新实训 项目博客 (八)
  • 原文地址:https://www.cnblogs.com/myhome-1/p/6270301.html
Copyright © 2011-2022 走看看