5 éve
szülő
commit
c93fae35fb

+ 1 - 0
bms/src/main/java/com/hssx/bms/constant/Constant.java

@@ -10,4 +10,5 @@ public class Constant {
     public static final String DEFAULT_PWD = "000000";//默认密码
     public static final Integer ADMINISTRATORS_TYPE = 0;//管理员角色类型
     public static final Integer INSTITUTION_TYPE = 1;//培训机构角色类型
+    public static final String APP_ID = "2EeNyJ6U89S6OsmMiwnpJkTMUgDz_suZjAlyU0uXwUU";//appid
 }

+ 1 - 1
bms/src/main/java/com/hssx/bms/controller/BookController.java

@@ -64,7 +64,7 @@ public class BookController {
     /**
      * 图书信息的录入
      * 参数:bookId 图书对应的id
-     *      file 上传的图书图片文件数组
+     *      file 上传的图书图片文件zip
      * @return
      */
     @ApiOperation(value = "图书图片的上传", notes = "图书图片的上传方法")

+ 57 - 4
bms/src/main/java/com/hssx/bms/service/impl/BookPageServiceImpl.java

@@ -1,6 +1,7 @@
 package com.hssx.bms.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.hssx.bms.entity.Book;
 import com.hssx.bms.entity.BookCategory;
 import com.hssx.bms.entity.BookPage;
 import com.hssx.bms.mapper.BookCategoryMapper;
@@ -9,11 +10,15 @@ import com.hssx.bms.service.BookPageService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.hssx.bms.until.HttpRespMsg;
 import com.hssx.bms.until.UploadFileToFileNameUtil;
+import net.lingala.zip4j.core.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+import net.lingala.zip4j.model.FileHeader;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.util.List;
 
 /**
@@ -36,14 +41,62 @@ public class BookPageServiceImpl extends ServiceImpl<BookPageMapper, BookPage> i
     public HttpRespMsg addBookPage(BookPage bookPage, MultipartFile file) {
         HttpRespMsg msg = new HttpRespMsg();
         if(file != null){
-            Integer count = bookPageMapper.selectCount(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()));
+            //上传之前删除数据
+            Integer count = bookPageMapper.delete(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()));
             String fileName = UploadFileToFileNameUtil.uploadFile(file, path);
-            bookPage.setFile(fileName);
-            bookPage.setPageNum(count+1);
-            bookPageMapper.insert(bookPage);
+            String filePath = path.substring(0, path.length() - "/upload/".length()) + fileName;
+            try {
+                Unzip4j1(filePath,path+bookPage.getBookId(),bookPage);
+            } catch (ZipException e) {
+                e.printStackTrace();
+            }
         }
         return msg;
     }
+    // 解压方法2
+    public void Unzip4j1(String zipFile,String folderPath,BookPage bookPage) throws ZipException {
+        long startTime = System.currentTimeMillis();
+        File folder = new File(folderPath);
+        deleteDir(folder);
+        ZipFile zipFile2 = new ZipFile(zipFile);
+        //设置编码格式
+        zipFile2.setFileNameCharset("GBK");
+        if (!zipFile2.isValidZipFile()) {
+            throw new ZipException("文件不合法或不存在");
+        }
+        //检查是否需要密码
+//        checkEncrypted(zipFile2);
+        List<FileHeader> fileHeaderList = zipFile2.getFileHeaders();
+        for (int i = 0; i < fileHeaderList.size(); i++) {
+            FileHeader fileHeader = fileHeaderList.get(i);
+            bookPage.setPageNum(i+1);
+            bookPage.setFile(fileHeader.getFileName());
+            bookPageMapper.insert(bookPage);
+            zipFile2.extractFile(fileHeader, folderPath);
+        }
+        System.out.println("解压成功!");
+        long endTime = System.currentTimeMillis();
+        System.out.println("耗时:" + (endTime - startTime) + "ms");
+    }
+
+    private static boolean deleteDir(File dir) {
+        if (dir.isDirectory()) {
+            String[] children = dir.list();
+            if (children != null && children.length > 0) {
+                // 递归删除目录中的子目录下
+                for (int i = 0; i < children.length; i++) {
+                    boolean success = deleteDir(new File(dir, children[i]));
+                    if (!success) {
+                        return false;
+                    }
+                }
+            }
+        }
+        // 目录此时为空,可以删除
+        return dir.delete();
+    }
+
+
 
     @Override
     public HttpRespMsg getBookPageList(BookPage bookPage) {

+ 24 - 3
bms/src/main/java/com/hssx/bms/until/Zip4jUtil.java

@@ -4,6 +4,7 @@ import net.lingala.zip4j.core.ZipFile;
 import net.lingala.zip4j.exception.ZipException;
 import net.lingala.zip4j.model.FileHeader;
 
+import java.io.File;
 import java.util.List;
 import java.util.Scanner;
 
@@ -14,13 +15,16 @@ import java.util.Scanner;
  * Version: 1.0
  */
 public class Zip4jUtil {
-    public static void main(String[] args) throws ZipException {
+    public static void main(String[] args) throws ZipException, InterruptedException {
         Unzip4j1("D:\\666.zip","D:\\666");
     }
 
     // 解压方法2
-    public static void Unzip4j1(String zipFile,String folderPath) throws ZipException {
+    public static void Unzip4j1(String zipFile,String folderPath) throws ZipException, InterruptedException {
         long startTime = System.currentTimeMillis();
+        File folder = new File(folderPath);
+        deleteDir(folder);
+//        Thread.sleep(100000);
         ZipFile zipFile2 = new ZipFile(zipFile);
         //设置编码格式
         zipFile2.setFileNameCharset("GBK");
@@ -32,7 +36,7 @@ public class Zip4jUtil {
         List<FileHeader> fileHeaderList = zipFile2.getFileHeaders();
         for (int i = 0; i < fileHeaderList.size(); i++) {
             FileHeader fileHeader = fileHeaderList.get(i);
-            fileHeader.getFileName();
+            System.out.println("  fileHeader.getFileName();"+  fileHeader.getFileName());
             zipFile2.extractFile(fileHeader, folderPath);
         }
         System.out.println("解压成功!");
@@ -50,4 +54,21 @@ public class Zip4jUtil {
         }
         in.close();
     }
+
+    private static boolean deleteDir(File dir) {
+        if (dir.isDirectory()) {
+            String[] children = dir.list();
+            if (children != null && children.length > 0) {
+                // 递归删除目录中的子目录下
+                for (int i = 0; i < children.length; i++) {
+                    boolean success = deleteDir(new File(dir, children[i]));
+                    if (!success) {
+                        return false;
+                    }
+                }
+            }
+        }
+        // 目录此时为空,可以删除
+        return dir.delete();
+    }
 }