Solve the problem of downloading directly to open

Downloading is also a common topic. Sometimes we download the pdf and open it directly, or the downloaded ofd is a compressed package. There is a situation facing this problem today that will cause this situation.

  • Pseudo code for direct opening

  StringBuilder fileName = new StringBuilder();//模拟文件名称
  fileName.append(queryByInvoiceId.getInvoiceCode()).append("_").append(queryByInvoiceId.getInvoiceNum()).append(suf);
  OSSClient ossClient = null;
  InputStream  inputStream = null;
  String key = null;
  ServletOutputStream outputStream = null;
  ossClient = AliyunOSSClientUtil.getOSSClient();
  String key = "aliyunkey";
  inputStream = GetInputStream(ossClient, key);
  try {
    
    
   outputStream = response.getOutputStream();
   int len = 0;
   byte[] buffer = new byte[4096];
   while ((len = inputStream.read(buffer)) != -1) {
    
    
    outputStream.write(buffer, 0, len);
    outputStream.flush();
   }
   response.setCharacterEncoding("UTF-8");
   response.setContentType("application/x-msdownload");//设置下载类型
   response.addHeader("Content-Disposition",
     "attachment;filename=" + fileName);
  } catch (Exception e) {
    
    
   // TODO Auto-generated catch block
   logger.error(e.getMessage(),e);
   
  }finally {
    
    
   try {
    
    
    if(inputStream != null) {
    
    
     inputStream.close();
    }
    if(outputStream != null) {
    
    
     outputStream.close();
    }
   } catch (IOException e) {
    
    
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  • problem lies in

The problem : In fact, it is not difficult to find that the code seems to be no problem, but when setting the response attribute, outputStream = response.getOutputStream(); has not been set when the output stream is obtained, resulting in the attribute function not being reflected in the output stream. Knowing the problem, we know that our settings should be set before getting the output stream.

  • Final pseudo code

  StringBuilder fileName = new StringBuilder();
  OSSClient ossClient = null;
  InputStream  inputStream = null;
  String key = null;
  ServletOutputStream outputStream = null;
  ossClient = AliyunOSSClientUtil.getOSSClient();
  String key = "aliyunKey";
  inputStream = GetInputStream(ossClient, key);
  try {
    
    
   response.setCharacterEncoding("UTF-8");
   response.setContentType("application/x-msdownload");
   response.addHeader("Content-Disposition",
     "attachment;filename=" + fileName);
   outputStream = response.getOutputStream();
   int len = 0;
   byte[] buffer = new byte[4096];
   while ((len = inputStream.read(buffer)) != -1) {
    
    
    outputStream.write(buffer, 0, len);
    outputStream.flush();
   }
  } catch (Exception e) {
    
    
   // TODO Auto-generated catch block
   logger.error(e.getMessage(),e);
  }finally {
    
    
   try {
    
    
    if(inputStream != null) {
    
    
     inputStream.close();
    }
    if(outputStream != null) {
    
    
     outputStream.close();
    }
   } catch (IOException e) {
    
    
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

Guess you like

Origin blog.csdn.net/qq_29897369/article/details/108313131