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(); + } + } } }