Kaynağa Gözat

修改下载文件(打压缩包)

LiFei 6 yıl önce
ebeveyn
işleme
66a870abfd

+ 5 - 0
pom.xml

@@ -867,6 +867,11 @@
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>2.6.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.ant</groupId>
+            <artifactId>ant</artifactId>
+            <version>1.8.1</version>
+        </dependency>
         <!-- UTILS end -->
     </dependencies>
 

+ 33 - 0
src/main/java/com/jeeplus/modules/hpresume/util/ZipUtil.java

@@ -0,0 +1,33 @@
+package com.jeeplus.modules.hpresume.util;
+
+import java.io.File;
+
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.taskdefs.Zip;
+import org.apache.tools.ant.types.FileSet;
+
+public class ZipUtil {
+    private File zipFile;
+
+    public ZipUtil(String pathName) {
+        zipFile = new File(pathName);
+    }
+
+    public void compress(String srcPathName) {
+        File srcdir = new File(srcPathName);
+        if (!srcdir.exists())
+            throw new RuntimeException(srcPathName + "不存在!");
+
+        Project prj = new Project();
+        Zip zip = new Zip();
+        zip.setProject(prj);
+        zip.setDestFile(zipFile);
+        FileSet fileSet = new FileSet();
+        fileSet.setProject(prj);
+        fileSet.setDir(srcdir);
+        //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");
+        //fileSet.setExcludes(...); 排除哪些文件或文件夹
+        zip.addFileset(fileSet);
+        zip.execute();
+    }
+}

+ 128 - 72
src/main/java/com/jeeplus/modules/hpresume/web/HpResumeBankController.java

@@ -13,6 +13,8 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.ConstraintViolationException;
 
+import com.jeeplus.common.utils.io.FileUtil;
+import com.jeeplus.modules.hpresume.util.ZipUtil;
 import com.jeeplus.modules.hpuser.entity.HpResumeWorkExperience;
 import com.jeeplus.modules.hpuser.service.HpResumeWorkExperienceService;
 import com.jeeplus.modules.sys.utils.DictUtils;
@@ -175,44 +177,103 @@ public class HpResumeBankController extends BaseController {
     public AjaxJson downLoad(String ids, HttpServletRequest request, HttpServletResponse response) {
         AjaxJson j = new AjaxJson();
         String idArray[] = ids.split(",");
+        String fileDir = "E:/home";
         for (String id : idArray) {
-          HpResumeBank hpResumeBank= hpResumeBankService.get(id);
+            HpResumeBank hpResumeBank = hpResumeBankService.get(id);
             if (StringUtils.isNotBlank(hpResumeBank.getId())) {
                 List<HpResumeWorkExperience> hpResumeWorkExperienceList = hpResumeWorkExperienceService.findListByResumeId(hpResumeBank.getId());
                 hpResumeBank.setHpResumeWorkExperienceList(hpResumeWorkExperienceList);
             }
-            String fileName = "简历" +id+ ".doc";
-            try {
-                XWPFDocument wb=createWord(fileName,hpResumeBank,request,response);
-                setResponseHeader(response, fileName);
-                setResponseHeader(response, fileName);
-                OutputStream os = response.getOutputStream();
-                wb.write(os);
-                os.flush();
-                os.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+            String fileName = "简历" + id + DateUtils.getDate("yyyyMMddHHmmss") + ".doc";
+
+            createWord(fileName, fileDir,hpResumeBank, request, response);
+        }
+        ZipUtil zc = new  ZipUtil("E:/file/file.zip");
+        //需要打包的文件路径
+        zc.compress(fileDir+"/");
+        String contentType = "application/octet-stream";
+        try {
+            //导出压缩包
+            download(request, response, "file/file.zip", contentType,encodeChineseDownloadFileName(request, "file.zip"));
+        } catch (Exception e) {
+            request.getSession().setAttribute("msg", "暂无内容");
+        }
+        //如果原压缩包存在,则删除
+        File file=new File("E:/file/file.zip");
+        if(file.exists()){
+            file.delete();
         }
         j.setMsg("下载成功");
         return j;
     }
 
 
-       //生成word
-    public XWPFDocument createWord(String fileName, HpResumeBank hpResumeBank, HttpServletRequest request, HttpServletResponse response){
+    /**
+     * 下载文件
+     */
+    public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception {
+        response.setContentType("text/html;charset=UTF-8");
+        request.setCharacterEncoding("UTF-8");
+        BufferedInputStream bis = null;
+        BufferedOutputStream bos = null;
+
+        String ctxPath ="E:/";
+        String downLoadPath = ctxPath + storeName;
+
+        long fileLength = new File(downLoadPath).length();
+
+        response.setContentType(contentType);
+        response.setHeader("Content-disposition", "attachment; filename="
+                + new String(realName.getBytes("utf-8"), "ISO8859-1"));
+        response.setHeader("Content-Length", String.valueOf(fileLength));
+
+        bis = new BufferedInputStream(new FileInputStream(downLoadPath));
+        bos = new BufferedOutputStream(response.getOutputStream());
+        byte[] buff = new byte[2048];
+        int bytesRead;
+        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
+            bos.write(buff, 0, bytesRead);
+        }
+        bis.close();
+        bos.close();
+    }
+
+    /**
+     * 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性
+     */
+    public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) throws UnsupportedEncodingException {
+        String filename = null;
+        String agent = request.getHeader("USER-AGENT");
+        if (null != agent){
+            if (-1 != agent.indexOf("Firefox")) {//Firefox
+                filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8"))))+ "?=";
+            }else if (-1 != agent.indexOf("Chrome")) {//Chrome
+                filename = new String(pFileName.getBytes(), "ISO8859-1");
+            } else {//IE7+
+                filename = java.net.URLEncoder.encode(pFileName, "UTF-8");
+                filename = StringUtils.replace(filename, "+", "%20");//替换空格
+            }
+        } else {
+            filename = pFileName;
+        }
+        return filename;
+    }
+
+
+
+    //生成word
+    public void createWord(String fileName,String fileDir, HpResumeBank hpResumeBank, HttpServletRequest request, HttpServletResponse response) {
         XWPFDocument document = new XWPFDocument();
-//        OutputStream stream = null;
-//        BufferedOutputStream bufferStream = null;
-//        String savePath = request.getServletContext().getRealPath("/uploadword");
-//        String fileDir= savePath + "/home";
+        OutputStream stream = null;
+        BufferedOutputStream bufferStream = null;
+
         try {
-//            File dir = new File(fileDir);
-//            if (!dir.exists()) {
-//                dir.mkdirs();
-//            }
-//            stream = new FileOutputStream(new File(fileDir+"/"+fileName));
-//            bufferStream = new BufferedOutputStream(stream, 1024);
+            File dir = new File(fileDir);
+            if (!dir.exists()) {
+                dir.mkdirs();
+            }
+            stream = new FileOutputStream(new File(fileDir + "/" + fileName));
+            bufferStream = new BufferedOutputStream(stream, 1024);
             XWPFParagraph p = document.createParagraph();
             // 设置居中
             p.setAlignment(ParagraphAlignment.CENTER);
@@ -241,7 +302,7 @@ public class HpResumeBankController extends BaseController {
             // 创建一个段落
             XWPFParagraph p1 = document.createParagraph();
             XWPFRun r1 = p1.createRun();
-            r1.setText("姓名:"+hpResumeBank.getRealName());
+            r1.setText("姓名:" + hpResumeBank.getRealName());
             // 与下一行的距离
             r1.setTextPosition(10);
             // 字体大小
@@ -249,7 +310,7 @@ public class HpResumeBankController extends BaseController {
             // 创建第二个段落
             XWPFParagraph p2 = document.createParagraph();
             XWPFRun r2 = p2.createRun();
-            r2.setText("联系电话:"+hpResumeBank.getPhone());
+            r2.setText("联系电话:" + hpResumeBank.getPhone());
             // 与下一行的距离
             r2.setTextPosition(10);
             // 字体大小
@@ -257,7 +318,7 @@ public class HpResumeBankController extends BaseController {
             // 创建第三个段落
             XWPFParagraph p3 = document.createParagraph();
             XWPFRun r3 = p3.createRun();
-            r3.setText("性别:"+DictUtils.getDictLabels(hpResumeBank.getGender(),"sex", "未填"));
+            r3.setText("性别:" + DictUtils.getDictLabels(hpResumeBank.getGender(), "sex", "未填"));
             // 与下一行的距离
             r3.setTextPosition(10);
             // 字体大小
@@ -266,11 +327,11 @@ public class HpResumeBankController extends BaseController {
             XWPFParagraph p4 = document.createParagraph();
             XWPFRun r4 = p4.createRun();
             if (StringUtils.isNotBlank(hpResumeBank.getBirthYear())) {
-                String birthYear=hpResumeBank.getBirthYear();
+                String birthYear = hpResumeBank.getBirthYear();
                 Calendar cale = Calendar.getInstance();
                 int year = cale.get(Calendar.YEAR);
-                birthYear = String.valueOf(year - Integer.parseInt(hpResumeBank.getBirthYear())+1);
-                r4.setText("年龄:"+birthYear);
+                birthYear = String.valueOf(year - Integer.parseInt(hpResumeBank.getBirthYear()) + 1);
+                r4.setText("年龄:" + birthYear);
             }
             // 与下一行的距离
             r4.setTextPosition(10);
@@ -279,7 +340,7 @@ public class HpResumeBankController extends BaseController {
             // 创建第五个段落
             XWPFParagraph p5 = document.createParagraph();
             XWPFRun r5 = p5.createRun();
-            r5.setText("学历:"+DictUtils.getDictLabels(hpResumeBank.getHighestQualification(),"qualification", "未填"));
+            r5.setText("学历:" + DictUtils.getDictLabels(hpResumeBank.getHighestQualification(), "qualification", "未填"));
             // 与下一行的距离
             r5.setTextPosition(10);
             // 字体大小
@@ -298,18 +359,18 @@ public class HpResumeBankController extends BaseController {
             // 创建第六个段落
             XWPFParagraph p6 = document.createParagraph();
             XWPFRun r6 = p6.createRun();
-            String expectationPosition="";
-            if(StringUtils.isNotBlank(hpResumeBank.getExpectationPosition())){
-                String[] arr=hpResumeBank.getExpectationPosition().split(",");
-                for (int j = 0; j <arr.length ; j++) {
-                    if(j==0){
-                        expectationPosition=DictUtils.getDictLabels(arr[j],"expectation_position", "");
-                    }else {
-                        expectationPosition=expectationPosition+","+DictUtils.getDictLabels(arr[j],"expectation_position", "");
+            String expectationPosition = "";
+            if (StringUtils.isNotBlank(hpResumeBank.getExpectationPosition())) {
+                String[] arr = hpResumeBank.getExpectationPosition().split(",");
+                for (int j = 0; j < arr.length; j++) {
+                    if (j == 0) {
+                        expectationPosition = DictUtils.getDictLabels(arr[j], "expectation_position", "");
+                    } else {
+                        expectationPosition = expectationPosition + "," + DictUtils.getDictLabels(arr[j], "expectation_position", "");
                     }
                 }
             }
-            r6.setText("期望岗位:"+expectationPosition);
+            r6.setText("期望岗位:" + expectationPosition);
             // 与下一行的距离
             r6.setTextPosition(10);
             // 字体大小
@@ -317,7 +378,7 @@ public class HpResumeBankController extends BaseController {
             // 创建第七个段落
             XWPFParagraph p7 = document.createParagraph();
             XWPFRun r7 = p7.createRun();
-            r7.setText("期望薪资:"+DictUtils.getDictLabels(hpResumeBank.getExpectationSalary(),"expectation_salary", "未填"));
+            r7.setText("期望薪资:" + DictUtils.getDictLabels(hpResumeBank.getExpectationSalary(), "expectation_salary", "未填"));
             // 与下一行的距离
             r7.setTextPosition(10);
             // 字体大小
@@ -325,13 +386,13 @@ public class HpResumeBankController extends BaseController {
             // 创建第八个段落
             XWPFParagraph p8 = document.createParagraph();
             XWPFRun r8 = p8.createRun();
-            r8.setText("期望地点:"+hpResumeBank.getExpectationAddress());
+            r8.setText("期望地点:" + hpResumeBank.getExpectationAddress());
             // 与下一行的距离
             r8.setTextPosition(10);
             // 字体大小
             r8.setFontSize(12);// 字体大小
-            List<HpResumeWorkExperience>  hpResumeWorkExperienceList = hpResumeBank.getHpResumeWorkExperienceList();
-            if(hpResumeWorkExperienceList.size()>0){
+            List<HpResumeWorkExperience> hpResumeWorkExperienceList = hpResumeBank.getHpResumeWorkExperienceList();
+            if (hpResumeWorkExperienceList.size() > 0) {
                 // 创建工作经验模块
                 XWPFParagraph p1111 = document.createParagraph();
                 XWPFRun r1111 = p1111.createRun();
@@ -342,17 +403,17 @@ public class HpResumeBankController extends BaseController {
                 r1111.setTextPosition(10);
                 // 字体大小
                 r1111.setFontSize(12);// 字体大小
-                for (int i = 0; i <hpResumeWorkExperienceList.size() ; i++) {
+                for (int i = 0; i < hpResumeWorkExperienceList.size(); i++) {
                     XWPFParagraph q = document.createParagraph();
                     XWPFRun qq = q.createRun();
-                    qq.setText("企业名称:"+hpResumeWorkExperienceList.get(i).getWorkCompany());
+                    qq.setText("企业名称:" + hpResumeWorkExperienceList.get(i).getWorkCompany());
                     // 与下一行的距离
                     qq.setTextPosition(10);
                     // 字体大小
                     qq.setFontSize(12);// 字体大小
                     XWPFParagraph q1 = document.createParagraph();
                     XWPFRun qq1 = q1.createRun();
-                    qq1.setText("工作岗位:"+hpResumeWorkExperienceList.get(i).getWorkPosition());
+                    qq1.setText("工作岗位:" + hpResumeWorkExperienceList.get(i).getWorkPosition());
                     // 与下一行的距离
                     qq1.setTextPosition(10);
                     // 字体大小
@@ -375,12 +436,11 @@ public class HpResumeBankController extends BaseController {
                     }else {
                         qq3.setText("结束工作时间:"+hpResumeWorkExperienceList.get(i).getWorkEndDate());
                     }
-
                     // 与下一行的距离
                     qq3.setTextPosition(10);
                     // 字体大小
                     qq3.setFontSize(12);// 字体大小
-                    if(i <hpResumeWorkExperienceList.size()-1){
+                    if (i < hpResumeWorkExperienceList.size() - 1) {
                         // 增加换行
                         qq3.addCarriageReturn();
                     }
@@ -388,35 +448,31 @@ public class HpResumeBankController extends BaseController {
                 }
 
             }
-//            document.write(stream);
-//            stream.close();
-//            bufferStream.close();
+            document.write(stream);
+            stream.close();
+            bufferStream.close();
         } catch (Exception ex) {
             ex.printStackTrace();
         } finally {
-//            if (stream != null) {
-//                try {
-//                    stream.close();
-//                } catch (IOException e) {
-//                    e.printStackTrace();
-//                }
-//            }
-//            if (bufferStream != null) {
-//                try {
-//                    bufferStream.close();
-//                } catch (IOException e) {
-//                    e.printStackTrace();
-//                }
-//            }
+            if (stream != null) {
+                try {
+                    stream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (bufferStream != null) {
+                try {
+                    bufferStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 //        String path="/uploadword/home/"+fileName;
-        return document;
     }
 
 
-
-
-
     /**
      * 导出excel文件
      */
@@ -437,7 +493,7 @@ public class HpResumeBankController extends BaseController {
                     }
                 }
             }
-            if(page.getList().size()>0){
+            if (page.getList().size() > 0) {
                 HSSFWorkbook wb = hpResumeBankService.getHSSFWorkbook("简历", page.getList(), null);
                 setResponseHeader(response, fileName);
                 OutputStream os = response.getOutputStream();