zoukankan      html  css  js  c++  java
  • SpringMVC文件下载

    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 java.io.File;
    import java.io.IOException;
    
    /**
     * Created by Administrator on 2018/4/3.
     */
    @Controller
    public class Filedownload {
    
            @RequestMapping("/download")
            public ResponseEntity<byte[]> download() throws IOException {
                //下载文件路径
                String path="D:\测试.xls";
                File file=new File(path);
                //Http请求头
                HttpHeaders headers = new HttpHeaders();
                //为了解决中文名称乱码问题
                String fileName=new String("测试.xls".getBytes("UTF-8"),"iso-8859-1");
                //设置请求标题
                headers.setContentDispositionFormData("attachment", fileName);
                //按照标题中的指定设置正文的媒体类型
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                        headers, HttpStatus.CREATED);
            }
    
    }
    

      

  • 相关阅读:
    javaSE基础(六)--IO流
    javaSE基础(五)--JDBC
    javaSE基础(四)--Map集合
    javaSE基础(三)--List集合
    javaSE基础(二)
    javaSE基础(一)
    eclipse快捷键大全
    mybatis学习-基础
    工厂模式
    GC日志和jvm内存的分代
  • 原文地址:https://www.cnblogs.com/xuchangqi1/p/8709751.html
Copyright © 2011-2022 走看看