浏览代码

导入开发

lwhhszx 2 年之前
父节点
当前提交
93753bb732

+ 0 - 1
PAS/src/main/java/cn/cslg/pas/common/utils/ExcelUtils.java

@@ -36,7 +36,6 @@ public class ExcelUtils {
         try {
             //2003版本的excel,用.xls结尾
             wookbook = new HSSFWorkbook(fis);//得到工作簿
-
         } catch (Exception ex) {
             //ex.printStackTrace();
             try {

+ 52 - 6
PAS/src/main/java/cn/cslg/pas/common/utils/ReadExcelUtils.java

@@ -20,18 +20,22 @@ import java.util.Map;
  * @Date 2023/5/30
  */
 public class ReadExcelUtils {
-
+    //每次读取Excel一行
     public static Map ReadExcelOneRow(String filePath, Integer row) {
         Map<Object, Object> map = new HashMap<>();
-        //判断传入文件路径不为空
-        if(filePath == null && filePath == ""){
-            ThrowException.throwXiaoShiException("传入文件路径不存在");
-        }
         File file = new File(filePath);
         try {
             InputStream inputStream = new FileInputStream(file);
             //XSSFWorkbook可以处理xlsx文件
-            Workbook workbook = new XSSFWorkbook(inputStream);
+            Workbook workbook =null;
+            //当文件以.xls结尾时
+            if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
+                workbook   = new HSSFWorkbook(inputStream);
+            }
+            else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
+            {
+                workbook   = new XSSFWorkbook(inputStream);
+            }
             //读取第几个sheet
             Sheet sheet = workbook.getSheetAt(0);
             //读取总行数
@@ -51,4 +55,46 @@ public class ReadExcelUtils {
         }
         return map;
     }
+
+    //检测Excel文件合法性
+    public static Integer textExcel(String filePath) throws IOException {
+        //判断文件是否存在
+        if(filePath == null && filePath == ""){
+        return -1;
+        }
+        File file = new File(filePath);
+        if(!file.exists())
+        {
+            return  -1;
+        }
+        // 检测是否是excel文件
+        if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
+            return -1;
+        }
+
+            InputStream inputStream = new FileInputStream(file);
+            //XSSFWorkbook可以处理xlsx文件
+        Workbook workbook =null;
+        //当文件以.xls结尾时
+        if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
+            workbook   = new HSSFWorkbook(inputStream);
+        }
+        else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
+        {
+            workbook   = new XSSFWorkbook(inputStream);
+        }
+            //读取第几个sheet
+            Sheet sheet = workbook.getSheetAt(0);
+            //读取总行数
+            int rows = sheet.getPhysicalNumberOfRows();
+         if(rows<=0)
+            {
+                return -2;
+            }
+            Row firstRow = sheet.getRow(0);
+         if(!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")){
+             return -2;
+         }
+        return  rows-1;
+    }
 }

+ 0 - 1
PAS/src/main/java/cn/cslg/pas/service/UploadPatentBatchService.java

@@ -94,7 +94,6 @@ public class UploadPatentBatchService {
 
                 //将装配对象中的数据保存到数据库
                 dataToDB(params, uploadParamsVO, projectImportPatentVO);
-
                 //通过WebSocket 在每一次循环结束后 向前端发送完成进度
                 WebSocketServer.sendInfo(Response.websocket(new TaskWebSocketDTO()
                         .setTaskId(params.getTaskId())

+ 9 - 2
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/ThreadSafeQueue.java

@@ -2,6 +2,7 @@ package cn.cslg.pas.service.upLoadPatent;
 
 import cn.cslg.pas.common.model.dto.UploadFileDTO;
 import cn.cslg.pas.common.model.vo.TaskParams;
+import cn.cslg.pas.common.model.vo.UploadParamsVO;
 import cn.cslg.pas.common.utils.FileUtils;
 import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
 import cn.cslg.pas.service.TaskService;
@@ -10,6 +11,7 @@ import cn.hutool.poi.excel.ExcelUtil;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -37,8 +39,11 @@ public class ThreadSafeQueue<T> {
             while (queue.size() == maxSize) {
                 notFull.await();
             }
-System.out.println("进入队列");
+
+            UploadParamsVO up =(UploadParamsVO)item;
+System.out.println("进入队列"+up.getFirstAddress());
             queue.add(item);
+            TimeUnit.MILLISECONDS.sleep(1000);
             notEmpty.signalAll();
         } finally {
             lock.unlock();
@@ -51,8 +56,10 @@ System.out.println("进入队列");
             while (queue.isEmpty()) {
                 notEmpty.await();
             }
-            System.out.println("出队列");
+            UploadParamsVO up =(UploadParamsVO)queue.element();
+            System.out.println("出队列"+up.getFirstAddress());
             T item = queue.remove();
+            TimeUnit.MILLISECONDS.sleep(2000);
             notFull.signalAll();
             return item;
         } finally {

+ 17 - 15
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/UploadTaskService.java

@@ -3,6 +3,7 @@ package cn.cslg.pas.service.upLoadPatent;
 import cn.cslg.pas.common.model.dto.UploadFileDTO;
 import cn.cslg.pas.common.model.vo.UploadParamsVO;
 import cn.cslg.pas.common.utils.FileUtils;
+import cn.cslg.pas.common.utils.ReadExcelUtils;
 import cn.cslg.pas.service.TaskService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -10,33 +11,34 @@ import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
+
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class UploadTaskService {
 
     private final FileUtils fileUtils;
     private final TaskService taskService;
-
-    public  static  Integer upLoadPatent() throws InterruptedException {
-//        //1.检查文档合法性
-//        UploadFileDTO fileDTO = fileUtils.uploadFile(file);
-//        //获得文件路径
-//        String filePath = fileUtils.getPath(fileDTO.getPath());
-//        //当不合法时返回-1
-//        if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
-//            return -1;
-//        }
+    private final ReadExcelUtils readExcelUtils;
+    public   Integer upLoadPatent(MultipartFile file) throws InterruptedException, IOException {
+        //1.检查文档合法性
+        UploadFileDTO fileDTO = fileUtils.uploadFile(file);
+        //获得文件路径
+        String filePath = fileUtils.getPath(fileDTO.getPath());
+      Integer total=  ReadExcelUtils.textExcel(filePath);
+      if(total<=0){
+          return  -1;
+      }
         //2.解析文档
         //4.建立任务
         Integer projectId =null;
-        Integer total =1;
 //        Integer taskId = taskService.add(fileDTO, projectId, null, total- 1, 1, 0, file.getOriginalFilename());
-
-       for(int i=0;i<10;i++) {
+        ThreadSafeQueue<UploadParamsVO> threadSafeQueue = new ThreadSafeQueue<>(8);
+       for(int i=0;i<total;i++) {
            //3.装载实体类
            UploadParamsVO uploadParamsVO = new UploadParamsVO();
+           uploadParamsVO.setFirstAddress(i+"");
            //5.上传到库
-           ThreadSafeQueue<UploadParamsVO> threadSafeQueue = new ThreadSafeQueue<>(100);
            Thread thread1 = new Thread(() -> {
                try {
                    threadSafeQueue.enqueue(uploadParamsVO);
@@ -59,6 +61,6 @@ public class UploadTaskService {
 
 
     public static void main(String[] args) throws InterruptedException {
-        UploadTaskService.upLoadPatent();
+//        UploadTaskService.upLoadPatent();
     }
 }