文件下载模板

文件下载模板

public void downloadFile(HttpServletResponse response){
  InputStream inputStream  = null;
  OutputStream outputStream  = null;
      try{
        // 读到流中
        inputStream  = ;
        //自定义文件名称
        String fileName = ;
        String fileName = new String(filename.getBytes("GBK"),"ISO8859-1");
       // 设置输出的格式
        response.reset();
        response.addHeader("Content-Disposition", String.format("attachment;filename=\"%s\"",fileName));
        response.setContentType("application/octet-stream");
        outputStream = new BufferOutputStream(response.getOutputStream);
        // 循环取出流中的数据
        byte[] chuf = new byte[1024];
        int len;
        while ((len = inStream.read(chuf)) > 0){
          outputStream.write(chuf, 0, len);
          }
      
          outputStream.flush();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
               try{
                    if (inputStream  != null)
                           inputStream  .close();
               } catch (IOException e) {
                    e.printStackTrace();
              }
                 
                try{
                    if (outputStream != null)
                           outputStream .close();
               } catch (IOException e) {
                    e.printStackTrace();
              }
         }
 
     }                                                    

 

posted @ 2021-10-21 17:53  精进的浩然兄  阅读(33)  评论(0编辑  收藏  举报