Explorar el Código

登记簿下载代码修改

jingyuanchao hace 1 año
padre
commit
c3f5d38c9a

+ 14 - 27
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/registerbook/service/impl/CoreRegisterBookPdfServiceImpl.java

@@ -283,24 +283,17 @@ public class CoreRegisterBookPdfServiceImpl extends ServiceImpl<CoreRegisterBook
     @Override
     public void downLoad(JSONObject tempVo, HttpServletResponse response) {
         final String localPathPrefix = fileService.getLocalPathPrefix().getData();
-        ServletOutputStream out = null;
-        ByteArrayOutputStream baos = null;
         String localFileName = tempVo.get("localFileName").toString();
-        File file = FileUtils.getFile(localFileName);
+        File file = new File(localFileName);
+
         if (!file.exists()) {
             response.setStatus(HttpStatus.NOT_FOUND.value());
             return;
         }
-        try {
-            out = response.getOutputStream();
-            baos = new ByteArrayOutputStream();
-            InputStream in = Files.newInputStream(file.toPath());
-            byte[] buffer = new byte[1024];
-            int len = 0;
-            while ((len = in.read(buffer)) > -1) {
-                baos.write(buffer, 0, len);
-            }
-            baos.flush();
+
+        try (InputStream in = new FileInputStream(file);
+             ServletOutputStream out = response.getOutputStream()) {
+
             response.setContentType("application/octet-stream");
             localFileName = localFileName.replace(localPathPrefix + File.separator, "");
             final BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(URLDecoder.decode(localFileName, "UTF-8"));
@@ -310,22 +303,16 @@ public class CoreRegisterBookPdfServiceImpl extends ServiceImpl<CoreRegisterBook
             if (header.matches("[a-zA-Z0-9._\\-]+")) {
                 response.setHeader(HttpHeaders.CONTENT_DISPOSITION, header);
             }
-            response.setContentLength(baos.size());
-            baos.writeTo(out);
+
+            byte[] buffer = new byte[1024];
+            int len;
+            while ((len = in.read(buffer)) != -1) {
+                out.write(buffer, 0, len);
+            }
             out.flush();
+
         } catch (IOException e) {
-            throw new RuntimeException(e);
-        } finally {
-            try {
-                if (out != null) {
-                    out.close();
-                }
-                if (baos != null) {
-                    baos.close();
-                }
-            } catch (IOException e) {
-                throw new RuntimeException(e);
-            }
+            throw new RuntimeException("Error during file download", e);
         }
     }