ludc
2023-11-24 9f9f7637f0cfd99497d2a5457089c7e92951a426
Source/UBCS/ubcs-ops-api/ubcs-resource-api/src/main/java/com/vci/ubcs/resource/utils/FileDownloadUtil.java
@@ -21,39 +21,39 @@
@Slf4j
public class FileDownloadUtil {
    /**
     * 下载文件
     * @param response 响应对象
     * @param fileObjectBO 文件的信息,包含文件的输入流
     * @throws IOException 下载异常会抛出
     */
    public static void downloadFile(HttpServletResponse response, FileObjectBO fileObjectBO) throws IOException {
        downloadFile(response,fileObjectBO,true);
    }
   /**
    * 下载文件
    * @param response 响应对象
    * @param fileObjectBO 文件的信息,包含文件的输入流
    * @throws IOException 下载异常会抛出
    */
   public static void downloadFile(HttpServletResponse response, FileObjectBO fileObjectBO) throws IOException {
      downloadFile(response,fileObjectBO,true);
   }
    /**
     * 下载文件
     * @param response 响应对象
     * @param fileObjectBO 文件的信息,包含文件的输入流
     * @param closeInputStream 是否关闭流
     * @throws IOException 下载出错的时候抛出异常
     */
    public static void downloadFile(HttpServletResponse response, FileObjectBO fileObjectBO,boolean closeInputStream) throws IOException {
        MediaType mediaType = MediaTypeFactory.getMediaType(fileObjectBO.getBucketName() + "." + fileObjectBO.getFileExtension()).orElse(MediaType.APPLICATION_OCTET_STREAM);
        // 设置强制下载不打开
        response.setContentType(mediaType.toString()+";application/force-download;charset=UTF-8");
        try{
            String fileName = URLEncoder.encode(fileObjectBO.getName() + "." + fileObjectBO.getFileExtension(), "UTF8");
            response.addHeader("Content-Disposition", "attachment;filename="+ fileName+ ";filename*=utf-8''");
        }catch(Exception e){
            if(log.isErrorEnabled()){
                log.error("设置文件的名称到响应流的时候出错",e);
            }
        }
        response.setCharacterEncoding("UTF-8");
        Cookie cookie = new Cookie("fileDownload", "true");
        cookie.setPath("/");
        response.addCookie(cookie);
   /**
    * 下载文件
    * @param response 响应对象
    * @param fileObjectBO 文件的信息,包含文件的输入流
    * @param closeInputStream 是否关闭流
    * @throws IOException 下载出错的时候抛出异常
    */
   public static void downloadFile(HttpServletResponse response, FileObjectBO fileObjectBO,boolean closeInputStream) throws IOException {
      MediaType mediaType = MediaTypeFactory.getMediaType(fileObjectBO.getBucketName() + "." + fileObjectBO.getFileExtension()).orElse(MediaType.APPLICATION_OCTET_STREAM);
      // 设置强制下载不打开
      response.setContentType(mediaType.toString()+";application/force-download;charset=UTF-8");
      try{
         String fileName = URLEncoder.encode(fileObjectBO.getName() + "." + fileObjectBO.getFileExtension(), "UTF8");
         response.addHeader("Content-Disposition", "attachment;filename="+ fileName+ ";filename*=utf-8''");
      }catch(Exception e){
         if(log.isErrorEnabled()){
            log.error("设置文件的名称到响应流的时候出错",e);
         }
      }
      response.setCharacterEncoding("UTF-8");
      Cookie cookie = new Cookie("fileDownload", "true");
      cookie.setPath("/");
      response.addCookie(cookie);
      if(closeInputStream) {
         try (InputStream ins = (fileObjectBO.getInputStream() != null ? fileObjectBO.getInputStream() : new FileInputStream(fileObjectBO.getFileLocalPath()))) {
            IOUtils.copy(ins, response.getOutputStream());
@@ -76,5 +76,53 @@
            throw e;
         }
      }
    }
   }
   /**
    * 下载文件
    * @param response 响应对象
    * @param fileObjectBO 文件的信息,包含文件的输入流
    * @param closeInputStream 是否关闭流
    * @throws IOException 下载出错的时候抛出异常
    */
   public static void downloadFileLocal(HttpServletResponse response, FileObjectBO fileObjectBO,boolean closeInputStream) throws IOException {
      MediaType mediaType = MediaTypeFactory.getMediaType(fileObjectBO.getFileExtension()).orElse(MediaType.APPLICATION_OCTET_STREAM);
      // 设置强制下载不打开
      response.setContentType(mediaType.toString()+";application/force-download;charset=UTF-8");
      try{
         String fileName = URLEncoder.encode(fileObjectBO.getName(), "UTF8");
         response.addHeader("Content-Disposition", "attachment;filename="+ fileName+ ";filename*=utf-8''");
      }catch(Exception e){
         if(log.isErrorEnabled()){
            log.error("设置文件的名称到响应流的时候出错",e);
         }
      }
      response.setCharacterEncoding("UTF-8");
      Cookie cookie = new Cookie("fileDownload", "true");
      cookie.setPath("/");
      response.addCookie(cookie);
      if(closeInputStream) {
         try (InputStream ins = (fileObjectBO.getInputStream() != null ? fileObjectBO.getInputStream() : new FileInputStream(fileObjectBO.getFileLocalPath()))) {
            IOUtils.copy(ins, response.getOutputStream());
         } catch (IOException e) {
            //有可能客户端的链接
            if (log.isErrorEnabled()) {
               log.error("写入文件到响应流出错", e);
            }
            throw e;
         }
      }else{
         try {
            InputStream ins = (fileObjectBO.getInputStream() != null ? fileObjectBO.getInputStream() : new FileInputStream(fileObjectBO.getFileLocalPath()));
            IOUtils.copy(ins, response.getOutputStream());
         } catch (IOException e) {
            //有可能客户端的链接
            if (log.isErrorEnabled()) {
               log.error("写入文件到响应流出错", e);
            }
            throw e;
         }
      }
   }
}