zoukankan      html  css  js  c++  java
  • Spring MVC实现文件下载

     下载文件①

    下载文件需要将byte数组还原成文件。

             首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式)。然后使用OutputStream将文件输入

    1. @RequestMapping(value = "downPhotoById")  
    2. public void downPhotoByStudentId(String id, final HttpServletResponse response){  
    3.     PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);  
    4.     byte[] data = entity.getPhotoData();  
    5.     String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();  
    6.     fileName = URLEncoder.encode(fileName, "UTF-8");  
    7.     response.reset();  
    8.     response.setHeader("Content-Disposition", "attachment; filename="" + fileName + """);  
    9.     response.addHeader("Content-Length", "" + data.length);  
    10.     response.setContentType("application/octet-stream;charset=UTF-8");  
    11.     OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
    12.     outputStream.write(data);  
    13.     outputStream.flush();  
    14.     outputStream.close();  
    15. }  
    1. <a href="<%=request.getContextPath() %>/downPhotoById.do?id=8000001">下载照片</a>  

     下载文件②

    /** * @Description 下载文件

    * @author jxldjsn

    * @date 2015年12月11日 下午6:11:33

    * @param fileName

    * @param file

    * @return

    * @throws IOException

    */

    public ResponseEntity<byte[]> download(String fileName, File file) throws IOException {

    String dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1");

    HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", dfileName);

    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }

     下载文件③

    //文件下载 主要方法
      public static void download(HttpServletRequest request,  
          HttpServletResponse response, String storeName, String contentType
           ) throws Exception {  
        
        request.setCharacterEncoding("UTF-8");  
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
      
        //获取项目根目录
        String ctxPath = request.getSession().getServletContext()  
            .getRealPath("");  
        
        //获取下载文件露肩
        String downLoadPath = ctxPath+"/uploadFile/"+ storeName;  
      
        //获取文件的长度
        long fileLength = new File(downLoadPath).length();  
    
        //设置文件输出类型
        response.setContentType("application/octet-stream");  
        response.setHeader("Content-disposition", "attachment; filename="  
            + new String(storeName.getBytes("utf-8"), "ISO8859-1")); 
        //设置输出长度
        response.setHeader("Content-Length", String.valueOf(fileLength));  
        //获取输入流
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
        //输出流
        bos = new BufferedOutputStream(response.getOutputStream());  
        byte[] buff = new byte[2048];  
        int bytesRead;  
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
          bos.write(buff, 0, bytesRead);  
        }  
        //关闭流
        bis.close();  
        bos.close();  
      }  
       
    }


    下载直接访问控制器如:http:\localhost:8080/springmvc/download.do


     下载文件④

    1. @RequestMapping("/export")  
    2.     public ResponseEntity<byte[]> export() throws IOException {  
    3.         HttpHeaders headers = new HttpHeaders();    
    4.         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);    
    5.         headers.setContentDispositionFormData("attachment", "dict.txt");    
    6.         return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("C:/Users/Administrator/Desktop/a.txt")),    
    7.                                               headers, HttpStatus.CREATED);    
    8.     }  
     
     


     

  • 相关阅读:
    前路漫长
    OEL6 /boot分区删除恢复
    区块链:剖析工作量证明
    【转】区块链:Schnoor签名究竟是什么
    使用 Hyperledger Fabric 开展私密交易
    Sovrin:技术落地可期,但推广难度较高
    白硕:区块链技术与数据隐私(附视频)
    从入门到精通零知识证明与zkSNARK
    区块链安全:匿名性以及隐私性
    朋友去面试Python工程师,又带回来几道基础题,Python面试题No10
  • 原文地址:https://www.cnblogs.com/jxldjsn/p/5671578.html
Copyright © 2011-2022 走看看