瀏覽代碼

Merge branch 'master' of http://47.100.37.243:10080/ZHOU/yunsu

# Conflicts:
#	cloud-model/src/main/java/com/hssx/cloudmodel/service/impl/MouldFileServiceImpl.java
5 年之前
父節點
當前提交
851816b8f3

+ 1 - 0
cloud-model/src/main/java/com/hssx/cloudmodel/controller/MouldFileController.java

@@ -238,6 +238,7 @@ public class MouldFileController {
      */
      */
     @ApiOperation("文档勾选批量下载")
     @ApiOperation("文档勾选批量下载")
     @RequestMapping(value = "/downloadfileList")
     @RequestMapping(value = "/downloadfileList")
+    @ResponseBody
     public HttpRespMsg downloadfileList(UserVO userVO, HttpServletRequest request, HttpServletResponse response) {
     public HttpRespMsg downloadfileList(UserVO userVO, HttpServletRequest request, HttpServletResponse response) {
         HttpRespMsg msg = new HttpRespMsg();
         HttpRespMsg msg = new HttpRespMsg();
         try {
         try {

+ 42 - 56
cloud-model/src/main/java/com/hssx/cloudmodel/service/impl/MouldFileServiceImpl.java

@@ -554,7 +554,9 @@ public class MouldFileServiceImpl extends ServiceImpl<MouldFileMapper, MouldFile
                 }else{
                 }else{
                      mouldFiles = mouldFileMapper.selectList(new QueryWrapper<MouldFile>().eq("model_id", id).eq("state", 3).eq("blong_type",3));
                      mouldFiles = mouldFileMapper.selectList(new QueryWrapper<MouldFile>().eq("model_id", id).eq("state", 3).eq("blong_type",3));
                 }
                 }
-                this.download(request, response, downloadPath, mould, mouldFiles, path);
+//                feedBackDirectMultiDownload(request, response, downloadPath, mould, mouldFiles, path);
+                String filePath =  this.download(downloadPath, mould, mouldFiles, path);
+                msg.data = filePath;
             }
             }
         }
         }
         return msg;
         return msg;
@@ -735,67 +737,51 @@ public class MouldFileServiceImpl extends ServiceImpl<MouldFileMapper, MouldFile
         return msg;
         return msg;
     }
     }
 
 
-    public String download(HttpServletRequest request, HttpServletResponse response, String downloadPath, Mould vo, List<MouldFile> mouldFiles, String oldFilePath){
-        //响应头的设置
-        response.reset();
-        response.setCharacterEncoding("utf-8");
-//        response.setContentType("multipart/form-data");
-        response.setContentType("application/x-download");
-        //设置压缩包的名字
-        //解决不同浏览器压缩包名字含有中文时乱码的问题
-        String downloadName = vo.getModelNo() +".zip";
-        String agent = request.getHeader("USER-AGENT");
-        try {
-            if (agent.contains("MSIE")||agent.contains("Trident")) {
-                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
-            } else {
-                downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
-        //设置压缩流:直接写入response,实现边压缩边下载
-        ZipOutputStream zipos = null;
-        try {
-            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
-            zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
 
 
-        //循环将文件写入压缩流
-        DataOutputStream os = null;
-        if(mouldFiles.size()>0){
-            for(int i = 0; i < mouldFiles.size(); i++ ){
-                File file = new File(mouldFiles.get(i).getFileUrl());
-                try {
-                    //添加ZipEntry,并ZipEntry中写入文件流
-                    //这里,加上i是防止要下载的文件有重名的导致下载失败
-                    zipos.putNextEntry(new ZipEntry(mouldFiles.get(i).getFileName()));
-                    os = new DataOutputStream(zipos);
-                    InputStream is = new FileInputStream(file);
-                    byte[] b = new byte[100];
-                    int length = 0;
-                    while((length = is.read(b))!= -1){
-                        os.write(b, 0, length);
+    public String download(String downloadPath, Mould vo, List<MouldFile> mouldFiles, String oldFilePath){
+        //需要压缩的文件--包括文件地址和文件名
+        // 要生成的压缩文件地址和文件名称
+        String desPath = oldFilePath+vo.getModelNo() + vo.getModelName()+".zip";
+        File zipFile = new File(desPath);
+        ZipOutputStream zipStream = null;
+        FileInputStream zipSource = null;
+        BufferedInputStream bufferStream = null;
+        try {
+            //构造最终压缩包的输出流
+            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
+            if(mouldFiles.size()>0){
+                for(int i =0;i<mouldFiles.size();i++){
+                    File file = new File(oldFilePath.substring(0, oldFilePath.length() - "/upload/".length())+mouldFiles.get(i).getFileUrl());
+                    //将需要压缩的文件格式化为输入流
+                    zipSource = new FileInputStream(file);
+                    //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
+                    ZipEntry zipEntry = new ZipEntry(mouldFiles.get(i).getFileName());
+                    //定位该压缩条目位置,开始写入文件到压缩包中
+                    zipStream.putNextEntry(zipEntry);
+                    //输入缓冲流
+                    bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
+                    int read = 0;
+                    //创建读写缓冲区
+                    byte[] buf = new byte[1024 * 10];
+                    while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
+                    {
+                        zipStream.write(buf, 0, read);
                     }
                     }
-                    is.close();
-                    zipos.closeEntry();
-                } catch (IOException e) {
-                    e.printStackTrace();
                 }
                 }
             }
             }
-        }
-        //关闭流
-        try {
-            os.flush();
-            os.close();
-            zipos.close();
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
+        } finally {
+            //关闭流
+            try {
+                if(null != bufferStream) bufferStream.close();
+                if(null != zipStream) zipStream.close();
+                if(null != zipSource) zipSource.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
         }
-        return null;
+        return "/upload/"+vo.getModelNo() + vo.getModelName()+".zip";
     }
     }
 
 
     public Map<String, Object> feedBackDirectMultiDownload(HttpServletRequest request, HttpServletResponse response, String downloadPath, Mould vo, List<MouldFile> mouldFiles, String oldFilePath) throws IOException {
     public Map<String, Object> feedBackDirectMultiDownload(HttpServletRequest request, HttpServletResponse response, String downloadPath, Mould vo, List<MouldFile> mouldFiles, String oldFilePath) throws IOException {