浏览代码

feature(模板下载修改): 模板下载修改

模板下载修改
mazq 4 年之前
父节点
当前提交
2a3c9e07d4

+ 8 - 9
src/main/java/com/dragoninfo/dcuc/authweb/restcontroller/download/DownloadController.java

@@ -16,6 +16,7 @@ import com.dragonsoft.duceap.commons.util.string.StringUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.system.ApplicationHome;
@@ -129,14 +130,12 @@ public class DownloadController {
     }
 
     private HttpServletResponse down(HttpServletResponse response, String path) {
+        InputStream fis = null;
+        OutputStream out = null;
         try {
             // 以流的形式下载文件。
             Resource resource = new ClassPathResource(path);
-            resource.getInputStream();
-            InputStream fis = resource.getInputStream();
-            byte[] buffer = new byte[fis.available()];
-            fis.read(buffer);
-            fis.close();
+            fis = resource.getInputStream();
             // 清空response
             response.reset();
             // 设置response的Header
@@ -168,13 +167,13 @@ public class DownloadController {
             }
             response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
             response.addHeader("Content-Length", "" + resource.contentLength());
-            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
+            out = new BufferedOutputStream(response.getOutputStream());
             response.setContentType("application/octet-stream");
-            outputStream.write(buffer);
-            outputStream.flush();
-            outputStream.close();
+            IOUtils.copy(fis,out);
         } catch (IOException ex) {
             ex.printStackTrace();
+        } finally {
+            IOUtils.closeQuietly(fis, out);
         }
         return response;
     }