Jelajahi Sumber

完成导出分页导出耳标数据到文本文件

zhuoning 3 tahun lalu
induk
melakukan
48c4d69dfb

+ 5 - 4
huimv-manage/src/main/java/com/huimv/manage/eartag/controller/EarmarkController.java

@@ -156,11 +156,12 @@ public class EarmarkController {
         log.info("printState="+printState);
         log.info("quantity="+quantity);
         //导出数据到excel格式
-//        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response);
+        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response,request);
         //导出数据到文本文件中
 //        earmarkService.exportEarmarkByApplyToTxt(applyId,printState,quantity,response);
         //test1
-        earmarkService.exportTest1(applyId,printState,quantity,response,request);
+//        earmarkService.exportTest1(applyId,printState,quantity,response,request);
+//        earmarkService.export1(applyId,printState,quantity,response,request);
         //test2
 //        earmarkService.exportTest2(applyId,printState,quantity,response,request);
     }
@@ -179,12 +180,12 @@ public class EarmarkController {
     public void exportPageEarmarkByApply(@RequestParam(value = "applyId",required = true) Integer applyId,
                                      @RequestParam(value = "printState",required = false) Integer printState,
                                      @RequestParam(value = "quantity",required = true) Integer quantity,
-                                     HttpServletResponse response) throws IOException {
+                                     HttpServletResponse response,HttpServletRequest request) throws IOException, ArchiveException {
         log.info("applyId="+applyId);
         log.info("printState="+printState);
         log.info("quantity="+quantity);
         //导出数据到excel格式
-        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response);
+        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response,request);
     }
 
     /**

+ 3 - 1
huimv-manage/src/main/java/com/huimv/manage/eartag/service/IEarmarkService.java

@@ -31,7 +31,7 @@ public interface IEarmarkService {
     Result setPrintState(String ids, int state);
 
     // 通过任务号导出耳标数据
-    void exportEarmarkByApply(Integer applyId, Integer printState,Integer quantity, HttpServletResponse response) throws IOException;
+    void exportEarmarkByApply(Integer applyId, Integer printState,Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException, ArchiveException;
 
     // 通过耳标号导出耳标数据
     void exportEarmarkByEarmark(String ids,Integer printState, Integer quantity, HttpServletResponse response) throws IOException;
@@ -42,4 +42,6 @@ public interface IEarmarkService {
     void exportTest1(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException, ArchiveException;
 
     void exportTest2(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException;
+
+    void export1(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException, ArchiveException;
 }

+ 201 - 96
huimv-manage/src/main/java/com/huimv/manage/eartag/service/impl/EarmarkServiceImpl.java

@@ -65,22 +65,24 @@ public class EarmarkServiceImpl implements IEarmarkService {
     private ExcelUtil excelUtil;
     @Autowired
     private DataUtil dateUtil;
+    @Autowired
+    private TxtUtil txtUtil;
+
 
     /**
-     * @Method      : listEarmark
+     * @Method : listEarmark
      * @Description : 展示列表
-     * @Params      : [pageSize]
-     * @Return      : com.huimv.manage.util.Result
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/29       
-     * @Time        : 21:14
+     * @Params : [pageSize]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/29
+     * @Time : 21:14
      */
     @Override
     public Result listEarmark(int pageSize) {
         //
         List<EtEarmarkEntity> earmarkEntityList = earmarkRepo.listEarmark(pageSize);
-        return new Result(ResultCode.SUCCESS,earmarkEntityList);
+        return new Result(ResultCode.SUCCESS, earmarkEntityList);
     }
 
     @Override
@@ -125,19 +127,18 @@ public class EarmarkServiceImpl implements IEarmarkService {
             return criteriaQuery.orderBy(orders).getRestriction();
         };
         //分页
-        Pageable pageable = PageRequest.of( pageNo - 1, pageSize);
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
         return new Result(ResultCode.SUCCESS, earmarkRepo.findAll(sf, pageable));
     }
 
     /**
-     * @Method      : getEarmark
+     * @Method : getEarmark
      * @Description : 下载耳标号
-     * @Params      : [applyId]
-     * @Return      : com.huimv.manage.util.Result
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/29       
-     * @Time        : 21:14
+     * @Params : [applyId]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/29
+     * @Time : 21:14
      */
     @Override
     public Result getEarmark(int applyId) throws IOException {
@@ -146,17 +147,17 @@ public class EarmarkServiceImpl implements IEarmarkService {
 //        System.out.println("requestXML="+requestXML);
 
         // 调用接口返回结果
-        String earMarkReponseXML = soap.callSoap(requestXML,webServiceUrl);
+        String earMarkReponseXML = soap.callSoap(requestXML, webServiceUrl);
 //        System.out.println("earMarkReponseXML="+earMarkReponseXML);
 
         // 解析结果
         JSONArray downloadEarmarkJa = earmarkTask.parseEarMarkReponseXML(earMarkReponseXML);
         // 耳标入库
         JSONArray newEarmarkJa = filterEarmark(downloadEarmarkJa);
-        if(newEarmarkJa.size() == 0){
-            return new Result(Const.CODE_NO_NEW_EARMARK,Const.NO_NEW_EARMARK,false);
+        if (newEarmarkJa.size() == 0) {
+            return new Result(Const.CODE_NO_NEW_EARMARK, Const.NO_NEW_EARMARK, false);
         }
-        for(int a=0;a<newEarmarkJa.size();a++){
+        for (int a = 0; a < newEarmarkJa.size(); a++) {
             JSONObject earmarkJo = newEarmarkJa.getJSONObject(a);
             EtEarmarkEntity earmarkEntity = new EtEarmarkEntity();
             earmarkEntity.setEarmarkId(earmarkJo.getString("earmarkId"));
@@ -170,18 +171,17 @@ public class EarmarkServiceImpl implements IEarmarkService {
             earmarkEntity.setSetUploadState(0);
             earmarkRepo.saveAndFlush(earmarkEntity);
         }
-        return new Result(Const.CODE_OK,"本次下载"+newEarmarkJa.size()+"个新耳标号数据.",false);
+        return new Result(Const.CODE_OK, "本次下载" + newEarmarkJa.size() + "个新耳标号数据.", false);
     }
 
     /**
-     * @Method      : setUpdateDownState
+     * @Method : setUpdateDownState
      * @Description : 设置耳标已下载状态
-     * @Params      : [earmarkIDs]
-     * @Return      : com.huimv.manage.util.Result
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/29       
-     * @Time        : 23:00
+     * @Params : [earmarkIDs]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/29
+     * @Time : 23:00
      */
     @Override
     public Result setUpdateDownState(String earmarkIDs) throws UnsupportedEncodingException {
@@ -189,31 +189,31 @@ public class EarmarkServiceImpl implements IEarmarkService {
         String requestXML = earmarkTask.getUpdateDownStateRequestXML(earmarkIDs);
         //远程调用返回结果
         String reponseXML = "";
-        reponseXML = soap.callSoap(requestXML,webServiceUrl);
+        reponseXML = soap.callSoap(requestXML, webServiceUrl);
         // test
 //        reponseXML = earmarkTask.TestParseUpdateDownStateXML();
         //解析
         String updateDownState = earmarkTask.parseUpdateDownStateXML(reponseXML);
         // 入库
-        if(updateDownState.trim().equalsIgnoreCase("false")){
-            return new Result(Const.CODE_UPDATE_EARMARK_DOWN_STATE_FAIL,Const.UPDATE_EARMARK_DOWN_STATE_FAIL,false);
-        }else{
+        if (updateDownState.trim().equalsIgnoreCase("false")) {
+            return new Result(Const.CODE_UPDATE_EARMARK_DOWN_STATE_FAIL, Const.UPDATE_EARMARK_DOWN_STATE_FAIL, false);
+        } else {
             String[] earmarkIDArray = earmarkIDs.split(",");
-            for(int a=0;a<earmarkIDArray.length;a++){
+            for (int a = 0; a < earmarkIDArray.length; a++) {
                 EtEarmarkEntity earmarkEntity = new EtEarmarkEntity();
                 earmarkEntity.setEarmarkId(earmarkIDArray[a]);
                 ExampleMatcher matcher = ExampleMatcher.matching()
-                        .withMatcher("earmark_id" ,ExampleMatcher.GenericPropertyMatchers.contains());
-                Example example = Example.of(earmarkEntity,matcher);
-                Optional optional= earmarkRepo.findOne(example);
-                if (optional.isPresent()){
-                    EtEarmarkEntity earmarkEntity1  = (EtEarmarkEntity) optional.get();
+                        .withMatcher("earmark_id", ExampleMatcher.GenericPropertyMatchers.contains());
+                Example example = Example.of(earmarkEntity, matcher);
+                Optional optional = earmarkRepo.findOne(example);
+                if (optional.isPresent()) {
+                    EtEarmarkEntity earmarkEntity1 = (EtEarmarkEntity) optional.get();
                     earmarkEntity1.setSetDownState(1);
 //                    earmarkEntity1.setSetDownDate(new Timestamp(new Date().getTime()));
                     earmarkEntity1.setSetDownDate(new Timestamp(System.currentTimeMillis()));
                     earmarkRepo.saveAndFlush(earmarkEntity1);
-                }else{
-                    return new Result(Const.CODE_NO_RECORD,Const.NO_RECORD,false);
+                } else {
+                    return new Result(Const.CODE_NO_RECORD, Const.NO_RECORD, false);
                 }
             }
             return new Result(ResultCode.SUCCESS);
@@ -221,19 +221,18 @@ public class EarmarkServiceImpl implements IEarmarkService {
     }
 
     /**
-     * @Method      : setProduceState
+     * @Method : setProduceState
      * @Description : 设置生产状态
-     * @Params      : [ids]
-     * @Return      : com.huimv.manage.util.Result
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/29       
-     * @Time        : 23:56
+     * @Params : [ids]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/29
+     * @Time : 23:56
      */
     @Override
-    public Result setProduceState(String ids,int state) {
+    public Result setProduceState(String ids, int state) {
         String[] idArray = ids.split(",");
-        for(int a=0;a<idArray.length;a++){
+        for (int a = 0; a < idArray.length; a++) {
             Optional optional = earmarkRepo.findById(Integer.parseInt(idArray[a]));
             EtEarmarkEntity earmarkEntity = (EtEarmarkEntity) optional.get();
             earmarkEntity.setSetWriteState(state);
@@ -244,19 +243,18 @@ public class EarmarkServiceImpl implements IEarmarkService {
     }
 
     /**
-     * @Method      : setWriteState
+     * @Method : setWriteState
      * @Description : 设置写入时间
-     * @Params      : [ids, state]
-     * @Return      : com.huimv.manage.util.Result
-     *
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/30
-     * @Time        : 0:02
+     * @Params : [ids, state]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/30
+     * @Time : 0:02
      */
     @Override
     public Result setWriteState(String ids, int state) {
         String[] idArray = ids.split(",");
-        for(int a=0;a<idArray.length;a++){
+        for (int a = 0; a < idArray.length; a++) {
             Optional optional = earmarkRepo.findById(Integer.parseInt(idArray[a]));
             EtEarmarkEntity earmarkEntity = (EtEarmarkEntity) optional.get();
             earmarkEntity.setSetWriteState(state);
@@ -267,19 +265,18 @@ public class EarmarkServiceImpl implements IEarmarkService {
     }
 
     /**
-     * @Method      : setPrintState
+     * @Method : setPrintState
      * @Description : 设置打码状态
-     * @Params      : [ids, state]
-     * @Return      : com.huimv.manage.util.Result
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/10/30       
-     * @Time        : 0:04
+     * @Params : [ids, state]
+     * @Return : com.huimv.manage.util.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/10/30
+     * @Time : 0:04
      */
     @Override
     public Result setPrintState(String ids, int state) {
         String[] idArray = ids.split(",");
-        for(int a=0;a<idArray.length;a++){
+        for (int a = 0; a < idArray.length; a++) {
             Optional optional = earmarkRepo.findById(Integer.parseInt(idArray[a]));
             EtEarmarkEntity earmarkEntity = (EtEarmarkEntity) optional.get();
             earmarkEntity.setSetPrintState(state);
@@ -293,16 +290,16 @@ public class EarmarkServiceImpl implements IEarmarkService {
     //过滤已经存在的耳标号
     private JSONArray filterEarmark(JSONArray downloadEarmarkJa) {
         JSONArray newEarmarkJa = new JSONArray();
-        for(int a=0;a<downloadEarmarkJa.size();a++){
+        for (int a = 0; a < downloadEarmarkJa.size(); a++) {
             JSONObject downloadEarmarkJo = downloadEarmarkJa.getJSONObject(a);
             String earmarkId = downloadEarmarkJo.getString("earmarkId");
 
             EtEarmarkEntity earmarkEntity = new EtEarmarkEntity();
             earmarkEntity.setEarmarkId(earmarkId);
             ExampleMatcher matcher = ExampleMatcher.matching()
-                    .withMatcher("earmark_id" ,ExampleMatcher.GenericPropertyMatchers.contains());
-            Example example = Example.of(earmarkEntity,matcher);
-            if(!earmarkRepo.exists(example)){
+                    .withMatcher("earmark_id", ExampleMatcher.GenericPropertyMatchers.contains());
+            Example example = Example.of(earmarkEntity, matcher);
+            if (!earmarkRepo.exists(example)) {
                 newEarmarkJa.add(downloadEarmarkJo);
             }
         }
@@ -310,17 +307,51 @@ public class EarmarkServiceImpl implements IEarmarkService {
     }
 
     /**
-     * @Method      : exportEarmarkByApply
+     * @Method : exportEarmarkByApply
      * @Description : 通过任务号导出耳标号
-     * @Params      : [applyId]
-     * @Return      : void
-     *
-     * @Author      : ZhuoNing
-     * @Date        : 2021/11/9
-     * @Time        : 14:11
+     * @Params : [applyId]
+     * @Return : void
+     * @Author : ZhuoNing
+     * @Date : 2021/11/9
+     * @Time : 14:11
      */
     @Override
-    public void exportEarmarkByApply(Integer applyId,Integer printState,Integer quantity, HttpServletResponse response) throws IOException {
+    public void exportEarmarkByApply(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException, ArchiveException {
+        Specification<EtEarmarkEntity> specific = (Specification<EtEarmarkEntity>) (root, criteriaQuery, criteriaBuilder) -> {
+            //
+            List<Predicate> predList = new ArrayList<>();
+            if (null != applyId) {
+                predList.add(criteriaBuilder.equal(root.get("applyId").as(Integer.class), applyId));
+            }
+            if (null != printState && printState != 0) {
+                predList.add(criteriaBuilder.equal(root.get("printState").as(Integer.class), printState));
+            }
+            //
+            Predicate[] pred = new Predicate[predList.size()];
+            Predicate and = criteriaBuilder.and(predList.toArray(pred));
+            criteriaQuery.where(and);
+            //
+            List<Order> orders = new ArrayList<>();
+            orders.add(criteriaBuilder.asc(root.get("id")));
+            return criteriaQuery.orderBy(orders).getRestriction();
+        };
+        //
+        List<EtEarmarkEntity> dataList = earmarkRepo.findAll(specific);
+        log.info("导出数据数量=" + dataList.size());
+        //
+//        excelUtil.exportEarmarkExcel(response, Const.EXPORT_EARMARK_EXCEL, String.valueOf(applyId), dataList, quantity);
+        // test
+//        excelUtil.exportEarmarkExcel(String.valueOf(applyId),dataList,quantity);
+        if(dataList.size()>0){
+            //导出耳标数据到文本文件
+            txtUtil.exportEarmarkToTxt(response,String.valueOf(applyId), dataList, quantity,request);
+        }else{
+            //导出空数据到文本文件
+            txtUtil.exportEmptyEarmarkToTxt(response,String.valueOf(applyId),request);
+        }
+    }
+
+    public void exportEarmarkByApply_old(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response) throws IOException {
         Specification<EtEarmarkEntity> specific = (Specification<EtEarmarkEntity>) (root, criteriaQuery, criteriaBuilder) -> {
             //
             List<Predicate> predList = new ArrayList<>();
@@ -341,23 +372,23 @@ public class EarmarkServiceImpl implements IEarmarkService {
         };
         //
         List<EtEarmarkEntity> dataList = earmarkRepo.findAll(specific);
-        log.info("导出数据数量="+dataList.size());
+        log.info("导出数据数量=" + dataList.size());
         //
-        excelUtil.exportEarmarkExcel(response,Const.EXPORT_EARMARK_EXCEL,String.valueOf(applyId),dataList,quantity);
+        excelUtil.exportEarmarkExcel(response, Const.EXPORT_EARMARK_EXCEL, String.valueOf(applyId), dataList, quantity);
         // test
 //        excelUtil.exportEarmarkExcel(String.valueOf(applyId),dataList,quantity);
     }
 
     @Override
-    public void exportEarmarkByEarmark(String ids, Integer printState,Integer quantity, HttpServletResponse response) throws IOException {
+    public void exportEarmarkByEarmark(String ids, Integer printState, Integer quantity, HttpServletResponse response) throws IOException {
         Specification<EtEarmarkEntity> specific = (Specification<EtEarmarkEntity>) (root, criteriaQuery, criteriaBuilder) -> {
             //
             List<Predicate> predList = new ArrayList<>();
             if (null != ids && !ids.trim().equalsIgnoreCase("null")) {
-                String[] idsArray =  ids.split(",");
+                String[] idsArray = ids.split(",");
                 Integer[] idArrInt = new Integer[idsArray.length];
-                for(int a=0;a<idsArray.length;a++){
-                    idArrInt[a]=Integer.parseInt(idsArray[a]);
+                for (int a = 0; a < idsArray.length; a++) {
+                    idArrInt[a] = Integer.parseInt(idsArray[a]);
                 }
 //                predList.add(criteriaBuilder.equal(root.get("id").as(Integer.class), applyId));
                 predList.add(root.get("id").as(Integer.class).in(idArrInt));
@@ -373,14 +404,14 @@ public class EarmarkServiceImpl implements IEarmarkService {
         };
         //
         List<EtEarmarkEntity> dataList = earmarkRepo.findAll(specific);
-        log.info("导出数据数量:"+dataList.size());
+        log.info("导出数据数量:" + dataList.size());
         String applyId = null;
-        if(dataList.size() > 0){
+        if (dataList.size() > 0) {
             EtEarmarkEntity earmarkEntity = dataList.get(0);
             applyId = earmarkEntity.getApplyId();
         }
         //
-        excelUtil.exportEarmarkExcel(response,Const.EXPORT_EARMARK_EXCEL,applyId,dataList,quantity);
+        excelUtil.exportEarmarkExcel(response, Const.EXPORT_EARMARK_EXCEL, applyId, dataList, quantity);
         // test
 //        excelUtil.exportEarmarkExcel(applyId,dataList,quantity);
     }
@@ -390,7 +421,6 @@ public class EarmarkServiceImpl implements IEarmarkService {
     public void exportEarmarkByApplyToTxt(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response) {
 
 
-
     }
 
     @Override
@@ -424,10 +454,11 @@ public class EarmarkServiceImpl implements IEarmarkService {
         //Windows
 //        String rootPath = "D:\\";
         String basePath = rootPath + "temp_download";
-        System.out.println("basePath>>"+basePath);
+        System.out.println("basePath>>" + basePath);
         //创建文件
         File file = new File(basePath);
         //判断文件是否存在,如果不存在,则创建此文件夹
+        System.out.println("file.exists() 11.>>" + file.exists());
         if (!file.exists()) {
             file.mkdir();
         }
@@ -437,7 +468,7 @@ public class EarmarkServiceImpl implements IEarmarkService {
 //        String studentResourcePath = new File(basePath, "student/student.txt").getAbsolutePath();
 //        File txtFile1 = new File(basePath, "eartag\\earmark_1.txt");
         File txtFile1 = new File(basePath, "eartag/earmark_1.txt");
-        System.out.println("txtFile1.exists()>>"+txtFile1.exists());
+        System.out.println("txtFile1.exists()>>" + txtFile1.exists());
 //        String studentResourcePath = txtFile1.getAbsolutePath();
 //        System.out.println("studentResourcePath = " + studentResourcePath);
         // 保证目录一定存在
@@ -470,7 +501,7 @@ public class EarmarkServiceImpl implements IEarmarkService {
 //        String fileName = "耳标数据下载" + "_" + dateUtil.formatLongTimeForTime(new Date().getTime());
         String fileName = "耳标数据下载";
         String zipFileName = fileName + ".zip";
-        System.out.println("zipFileName>>"+zipFileName);
+        System.out.println("zipFileName>>" + zipFileName);
 
 //        File filex = new File(basePath + "\\newFile.txt");
 //        System.out.println("###### filex.exists>>"+filex.exists());
@@ -485,9 +516,9 @@ public class EarmarkServiceImpl implements IEarmarkService {
 //        File zipFile = null;
 //        String zipPath = basePath + "\\eartag\\" + zipFileName;
         String zipPath = basePath + "/eartag/" + zipFileName;
-        System.out.println("zipPath>>"+zipPath);
+        System.out.println("zipPath>>" + zipPath);
         File zipFile = new File(zipPath);
-        System.out.println("zipFileexists.exists>>"+zipFile.exists());
+        System.out.println("zipFileexists.exists>>" + zipFile.exists());
         FileOutputStream zipFos = new FileOutputStream(zipFile);
         ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
         if (archOut instanceof ZipArchiveOutputStream) {
@@ -520,7 +551,7 @@ public class EarmarkServiceImpl implements IEarmarkService {
     @Override
     public void exportTest2(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException {
         String basePath = getResourceBasePath(request);
-        System.out.println("basePath>>"+basePath);
+        System.out.println("basePath>>" + basePath);
 //        String studentResourcePath = new File(basePath, "student/student.txt").getAbsolutePath();
         File txtFile = new File(basePath, "eartag/earmark_1.txt");
         String studentResourcePath = txtFile.getAbsolutePath();
@@ -551,6 +582,81 @@ public class EarmarkServiceImpl implements IEarmarkService {
         ZipUtils.downloadZip(response.getOutputStream(), txtFile);
     }
 
+    @Override
+    public void export1(Integer applyId, Integer printState, Integer quantity, HttpServletResponse response, HttpServletRequest request) throws IOException, ArchiveException {
+        //定义根路径
+        //Linux
+        String rootPath = request.getRealPath("/");
+        //Windows
+//        String rootPath = "D:\\";
+        String basePath = rootPath + "temp_download";
+        System.out.println("basePath>>" + basePath);
+        //创建文件
+        File file = new File(basePath);
+        //判断文件是否存在,如果不存在,则创建此文件夹
+        System.out.println("file.exists() 11.>>" + file.exists());
+        if (!file.exists()) {
+            file.mkdir();
+        }
+        File t1File = new File(basePath + "/t1.txt");
+        if (!t1File.exists()) {
+            // t1File.delete();
+            t1File.createNewFile();
+        }
+        System.out.println("t1File.exists()>>"+t1File.exists());
+        BufferedWriter bw = new BufferedWriter(new FileWriter(t1File));
+        String ss = "测试数据";
+        bw.write(ss);
+        bw.newLine();  //换行用
+        //关闭流
+        bw.close();
+        System.out.println("写入成功");
+
+        List<File> txtList = new ArrayList();
+        txtList.add(t1File);
+        for(int a=0;a<txtList.size();a++){
+            System.out.println(">>"+txtList.get(a));
+        }
+        ///////////////////////////////////////////////////////////////////////////////////////////////////////
+        String name = "耳标文件压缩包下载";
+        String fileName = name + "_" + dateUtil.formatLongTimeForTime(new Date().getTime());
+        String zipFileName = fileName + ".zip";
+        File zipFile = null;
+//        String path = rootPath + "temp_download";
+        // 打成压缩包
+        zipFile = new File(basePath + "/" + zipFileName);
+        FileOutputStream zipFos = new FileOutputStream(zipFile);
+        ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
+        if (archOut instanceof ZipArchiveOutputStream) {
+            ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
+            for (File txtFile : txtList) {
+                ZipArchiveEntry zipEntry = new ZipArchiveEntry(txtFile, txtFile.getName());
+                zos.putArchiveEntry(zipEntry);
+                zos.write(FileUtils.readFileToByteArray(txtFile));
+                zos.closeArchiveEntry();
+                zos.flush();
+            }
+            zos.close();
+        }
+        // 输出到客户端
+        OutputStream out = null;
+        out = response.getOutputStream();
+        response.reset();
+        response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
+        response.setContentType("application/octet-stream; charset=utf-8");
+        response.setCharacterEncoding("UTF-8");
+        out.write(FileUtils.readFileToByteArray(zipFile));
+        out.flush();
+        out.close();
+
+        System.out.println("zipFile.exists()>>"+zipFile.exists());
+        // 输出客户端结束后,删除压缩包
+//        if (zipFile.exists()) {
+//            zipFile.delete();
+//        }
+
+    }
+
     /**
      * 获取项目根路径
      *
@@ -581,15 +687,14 @@ public class EarmarkServiceImpl implements IEarmarkService {
             file.mkdir();
         }
         String pathStr = file.getAbsolutePath();
-        System.out.println("pathStr>>"+pathStr);
+        System.out.println("pathStr>>" + pathStr);
         return pathStr;
     }
 
     /**
      * 保证拷贝的文件的目录一定要存在
      *
-     * @param filePath
-     *            文件路径
+     * @param filePath 文件路径
      */
     public static void ensureDirectory(String filePath) {
         if (StringUtils.isEmpty(filePath)) {

+ 193 - 0
huimv-manage/src/main/java/com/huimv/manage/util/TxtUtil.java

@@ -0,0 +1,193 @@
+package com.huimv.manage.util;
+
+import com.huimv.manage.eartag.dao.entity.EtEarmarkEntity;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.commons.io.FileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class TxtUtil {
+    @Autowired
+    private DataUtil dateUtil;
+
+    public void exportEarmarkToTxt(HttpServletResponse response, String applyId, List<EtEarmarkEntity> dataList, Integer quantity, HttpServletRequest request) throws IOException, ArchiveException {
+        //定义根路径
+        String rootPath = request.getRealPath("/");
+        String basePath = rootPath + "temp_download";
+        System.out.println("basePath>>" + basePath);
+        //创建文件
+        File file = new File(basePath);
+        //判断文件是否存在,如果不存在,则创建此文件夹
+        System.out.println("file.exists() 11.>>" + file.exists());
+        if (!file.exists()) {
+            file.mkdir();
+        }
+        //
+        List rootList = new ArrayList();
+        List newList = new ArrayList();
+        for(int a=0;a<dataList.size();a++) {
+            if(a % quantity == 0){
+                newList = new ArrayList();
+                rootList.add(newList);
+                EtEarmarkEntity EtEarmarkEntity = dataList.get(a);
+                String earmark = EtEarmarkEntity.getEarmarkNumber()+" "+EtEarmarkEntity.getEarmarkData();
+                newList.add(earmark);
+            }else{
+                EtEarmarkEntity EtEarmarkEntity = dataList.get(a);
+                String earmark = EtEarmarkEntity.getEarmarkNumber()+" "+EtEarmarkEntity.getEarmarkData();
+                newList.add(earmark);
+            }
+        }
+        System.out.println("rootList.size>>"+rootList.size());
+        //写入文件
+        List<File> txtList = new ArrayList();
+        for(int a=0;a<rootList.size();a++){
+            List childDataList = (ArrayList) rootList.get(a);
+            //生成新文件并写入耳标数据
+            File txtFile = createFileAndWriteText(childDataList,quantity,a+1,basePath);
+            txtList.add(txtFile);
+        }
+        ///////////////////////////////////////////////////////////////////////////////////////////////////////
+        String name = "耳标文件压缩包下载";
+        String fileName = name + "_" + dateUtil.formatLongTimeForTime(new Date().getTime());
+        String zipFileName = fileName + ".zip";
+//        File zipFile = null;
+//        String path = rootPath + "temp_download";
+        // 打成压缩包
+        File zipFile = new File(basePath + "/" + zipFileName);
+        System.out.println("zipFile.getAbsolutePath>>"+zipFile.getAbsolutePath());
+        FileOutputStream zipFos = new FileOutputStream(zipFile);
+        ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
+        if (archOut instanceof ZipArchiveOutputStream) {
+            ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
+            for (File txtFile : txtList) {
+                ZipArchiveEntry zipEntry = new ZipArchiveEntry(txtFile, txtFile.getName());
+                zos.putArchiveEntry(zipEntry);
+                zos.write(FileUtils.readFileToByteArray(txtFile));
+                zos.closeArchiveEntry();
+                zos.flush();
+            }
+            zos.close();
+        }
+        // 输出到客户端
+        OutputStream out = null;
+        out = response.getOutputStream();
+        response.reset();
+        response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
+        response.setContentType("application/octet-stream; charset=utf-8");
+        response.setCharacterEncoding("UTF-8");
+        out.write(FileUtils.readFileToByteArray(zipFile));
+        out.flush();
+        out.close();
+
+        System.out.println("zipFile.exists()>>"+zipFile.exists());
+        // 输出客户端结束后,删除压缩包
+//        if (zipFile.exists()) {
+//            zipFile.delete();
+//        }
+    }
+
+    //创建文件并写入耳标数据
+    private File createFileAndWriteText(List childDataList,Integer quantity,Integer pageNo,String basePath) throws IOException {
+        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 新建文件 >>"+pageNo);
+        File txtFile = new File(basePath + "/耳标数据第"+pageNo+"页.txt");
+        if (!txtFile.exists()) {
+            // t1File.delete();
+            txtFile.createNewFile();
+        }
+        System.out.println("txtFile.exists()>>"+txtFile.exists());
+        BufferedWriter bw = new BufferedWriter(new FileWriter(txtFile));
+        for(int a=0;a<childDataList.size();a++){
+            bw.write(childDataList.get(a).toString());
+            //换行
+            bw.newLine();
+        }
+        //关闭流
+        bw.close();
+        System.out.println("第"+pageNo+"页已经写入记录数>>"+childDataList.size());
+        return txtFile;
+    }
+
+    //导出空数据文件
+    public void exportEmptyEarmarkToTxt(HttpServletResponse response, String valueOf, HttpServletRequest request) throws IOException, ArchiveException {
+        //定义根路径
+        //Linux
+        String rootPath = request.getRealPath("/");
+        String basePath = rootPath + "temp_download";
+        System.out.println("basePath>>" + basePath);
+        //创建文件
+        File file = new File(basePath);
+        //判断文件是否存在,如果不存在,则创建此文件夹
+        if (!file.exists()) {
+            file.mkdir();
+        }
+        File t1File = new File(basePath + "/t1.txt");
+        if (!t1File.exists()) {
+            // t1File.delete();
+            t1File.createNewFile();
+        }
+        BufferedWriter bw = new BufferedWriter(new FileWriter(t1File));
+        String text = "暂无任何耳标数据";
+        bw.write(text);
+        bw.newLine();  //换行用
+        //关闭流
+        bw.close();
+        List<File> txtList = new ArrayList();
+        txtList.add(t1File);
+        String name = "耳标文件压缩包下载";
+        String fileName = name + "_" + dateUtil.formatLongTimeForTime(new Date().getTime());
+        String zipFileName = fileName + ".zip";
+        File zipFile = null;
+        // 打成压缩包
+        zipFile = new File(basePath + "/" + zipFileName);
+        FileOutputStream zipFos = new FileOutputStream(zipFile);
+        ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
+        if (archOut instanceof ZipArchiveOutputStream) {
+            ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
+            for (File txtFile : txtList) {
+                ZipArchiveEntry zipEntry = new ZipArchiveEntry(txtFile, txtFile.getName());
+                zos.putArchiveEntry(zipEntry);
+                zos.write(FileUtils.readFileToByteArray(txtFile));
+                zos.closeArchiveEntry();
+                zos.flush();
+            }
+            zos.close();
+        }
+        // 输出到客户端
+        OutputStream out = null;
+        out = response.getOutputStream();
+        response.reset();
+        response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
+        response.setContentType("application/octet-stream; charset=utf-8");
+        response.setCharacterEncoding("UTF-8");
+        out.write(FileUtils.readFileToByteArray(zipFile));
+        out.flush();
+        out.close();
+
+        System.out.println("zipFile.exists()>>"+zipFile.exists());
+        // 输出客户端结束后,删除压缩包
+        if (zipFile.exists()) {
+            zipFile.delete();
+        }
+    }
+}

+ 56 - 0
huimv-manage/src/test/java/com/huimv/manage/eartag/controller/ApplyTest.java

@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -34,4 +35,59 @@ public class ApplyTest {
     public void testGetNewApply(){
 //        Result result = applyService();
     }
+
+    @Test
+    public void testPagesize(){
+        int total = 100;
+        int a = total/10;
+        int b = total%10;
+        System.out.println("a>>"+a);
+        System.out.println("b>>"+b);
+    }
+
+    @Test
+    public void testList(){
+        List dataList = new ArrayList();
+        for(int a=0;a<100;a++){
+            dataList.add(a+1);
+        }
+
+        System.out.println("dataList.size 11>>"+dataList.size());
+//        for(int a=0;a<dataList.size();a++){
+//            Object dataJo = dataList.get(a);
+//            System.out.println(a+"="+dataJo.toString());
+//            dataList.remove(a);
+//            if(a==20)
+//                break;
+//        }
+        List outList = new ArrayList();
+        List aList = new ArrayList();
+        for(int a=0;a<dataList.size();a++){
+            if(a%20 == 0){
+                aList = new ArrayList();
+                aList.add(dataList.get(a));
+                outList.add(aList);
+            }else{
+                aList.add(dataList.get(a));
+            }
+        }
+
+        for(int a=0;a<outList.size();a++)
+        {
+            List bList = (ArrayList)outList.get(a);
+            for(int b=0;b<bList.size();b++){
+                System.out.println(""+bList.get(b).toString());
+            }
+            System.out.println("=================================================");
+
+        }
+
+        System.out.println("outList.size>>"+outList.size());
+
+        System.out.println("dataList.size 22>>"+dataList.size());
+//        for(int a=0;a<dataList.size();a++){
+//            Object dataJo = dataList.get(a);
+//            System.out.println(a+" -- "+dataJo.toString());
+//        }
+    }
 }