From fa9284fe8b0bc5eb1542d26774fd29b864288750 Mon Sep 17 00:00:00 2001 From: "396572590@qq.com" Date: Sat, 16 Jul 2022 00:23:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20There=20is=20no=20way=20the,=20=20?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WorkFlowController.java | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/adc-da-activiti/src/main/java/com/adc/da/workFlow/controller/WorkFlowController.java b/adc-da-activiti/src/main/java/com/adc/da/workFlow/controller/WorkFlowController.java index 0f548117..b41f3c0b 100644 --- a/adc-da-activiti/src/main/java/com/adc/da/workFlow/controller/WorkFlowController.java +++ b/adc-da-activiti/src/main/java/com/adc/da/workFlow/controller/WorkFlowController.java @@ -38,6 +38,7 @@ import sun.misc.BASE64Encoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -77,23 +78,59 @@ public class WorkFlowController { // @RequiresPermissions("sys:file:downloadFileForSar") public void downloadFileForSar(String fileId, String oldFileName, HttpServletResponse response, HttpServletRequest request) throws Exception { BusProcessModelHis modelHisOne = workFlowFeignClient.processEditFile(fileId); - InputStream is = null; OutputStream os = null; + // fileUrl为http或https链接 + InputStream inputStream = null; + HttpURLConnection conn = null; response.reset(); - try { - String fileOldName = fileNameEncoding(oldFileName,request); - response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\""); - response.setContentType("application/octet-stream"); - File file = new File(downloadUrl+modelHisOne.getEditFilePath()); - is = new FileInputStream(file); - os = response.getOutputStream(); - org.apache.commons.io.IOUtils.copy(is, os); - os.flush(); - } catch (IOException e) { - logger.error(e.getMessage(), e); - } finally { - org.apache.commons.io.IOUtils.closeQuietly(is); - org.apache.commons.io.IOUtils.closeQuietly(os); + File file = new File(downloadUrl+modelHisOne.getEditFilePath()); + + if(file.exists()){ + try { + String fileOldName = fileNameEncoding(oldFileName,request); + response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\""); + response.setContentType("application/octet-stream"); + inputStream = new FileInputStream(file); + os = response.getOutputStream(); + org.apache.commons.io.IOUtils.copy(inputStream, os); + os.flush(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } finally { + org.apache.commons.io.IOUtils.closeQuietly(inputStream); + org.apache.commons.io.IOUtils.closeQuietly(os); + } + }else { + URL url; + try { + String fileOldName = fileNameEncoding(oldFileName,request); + response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\""); + response.setContentType("application/octet-stream"); + url = new URL(downloadUrl+modelHisOne.getEditFilePath()); + conn = (HttpURLConnection) url.openConnection(); + // 设置超时间为5秒 + conn.setConnectTimeout(3 * 1000); + // 得到输入流 + inputStream = conn.getInputStream(); + os = response.getOutputStream(); + IOUtils.copy(inputStream, os); + os.flush(); + //生成本地文件之类的省略了 + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + if (conn != null) { + conn.disconnect(); + } + try { + if (inputStream != null) { + inputStream.close(); + } + IOUtils.closeQuietly(os); + } catch (IOException e) { + e.printStackTrace(); + } + } } }