提交 d9d646e2 作者: chenshiqiang

package

上级 6e1065b5
...@@ -18,9 +18,6 @@ import io.swagger.annotations.Api; ...@@ -18,9 +18,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -31,12 +28,10 @@ import org.springframework.web.util.WebUtils; ...@@ -31,12 +28,10 @@ import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/** /**
* 知识 * 知识
...@@ -84,6 +79,9 @@ public class KnowledgeController { ...@@ -84,6 +79,9 @@ public class KnowledgeController {
public Result<?> getById(@RequestParam String id) { public Result<?> getById(@RequestParam String id) {
log.info("{}===query begin", new Date()); log.info("{}===query begin", new Date());
KnowledgeVO knowledgeVO = knowledgeService.getById(id); KnowledgeVO knowledgeVO = knowledgeService.getById(id);
if(knowledgeVO==null){
return Result.OK("nothing");
}
if (null != knowledgeVO.getPublishDate() && (!knowledgeVO.getPublishDate().isEmpty())) { if (null != knowledgeVO.getPublishDate() && (!knowledgeVO.getPublishDate().isEmpty())) {
knowledgeVO.setPublishDate(EsDateUtil.esFieldDateMapping(knowledgeVO.getPublishDate())); knowledgeVO.setPublishDate(EsDateUtil.esFieldDateMapping(knowledgeVO.getPublishDate()));
} }
......
...@@ -87,6 +87,9 @@ class KnowledgeServiceImpl implements IKnowledgeService { ...@@ -87,6 +87,9 @@ class KnowledgeServiceImpl implements IKnowledgeService {
@Value("${files.storage}") @Value("${files.storage}")
String filesStorage; String filesStorage;
String tmpFile = "/storage" + Constants.FSP + "brap" + Constants.FSP + "download"
+ Constants.FSP;
@Autowired @Autowired
private AsyncService asyncService; private AsyncService asyncService;
...@@ -721,10 +724,12 @@ class KnowledgeServiceImpl implements IKnowledgeService { ...@@ -721,10 +724,12 @@ class KnowledgeServiceImpl implements IKnowledgeService {
for (KnowledgeVO knowledgeVO : knowledgeVOS) { for (KnowledgeVO knowledgeVO : knowledgeVOS) {
pathList.add(filesStorage + knowledgeVO.getFiles().get(0).getFilePath()); pathList.add(filesStorage + knowledgeVO.getFiles().get(0).getFilePath());
} }
log.info("pathList:{}",pathList);
String filePackage = comprocessByName(pathList, UUID.randomUUID().toString().replace("-","")); String filePackage=UUID.randomUUID().toString().replace("-", "");
redisUtil.set(ids, filePackage, 3600 * 24); ZipUtils.compressFiles(tmpFile,filePackage,pathList );
return filePackage; String packagePath=tmpFile+filePackage+".zip";
redisUtil.set(ids, packagePath, 3600 * 24);
return packagePath;
} }
/* /*
...@@ -732,66 +737,14 @@ class KnowledgeServiceImpl implements IKnowledgeService { ...@@ -732,66 +737,14 @@ class KnowledgeServiceImpl implements IKnowledgeService {
*/ */
@Override @Override
public String comprocessByName(List<String> fileNameList, String downloadFileName) { public String comprocessByName(List<String> fileNameList, String downloadFileName) {
List<File> fileList = new ArrayList<File>(); if (fileNameList.size() == 1) {
for (String fileName : fileNameList) { return fileNameList.get(0);
log.info("file path:{}",fileName);
fileList.add(new File(fileName));
} }
return comprocessByFile(fileList, downloadFileName); ZipUtils.compressFiles(tmpFile,downloadFileName,fileNameList);
return tmpFile+downloadFileName+".zip";
} }
/*
* 将所有的文件都压缩到一个文件中去
*/
public String comprocessByFile(List<File> fileNamemList, String key) {
String tmpFile = "/storage" + Constants.FSP + "brap" + Constants.FSP + "download"
+ Constants.FSP;
byte[] buffer = new byte[4096];
FileInputStream fis = null;
ZipOutputStream zos = null;
File zipFile = null;
try {
zipFile = new File(tmpFile + key + ".zip");
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File file : fileNamemList) {
try {
fis = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(file.getName()));
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.flush();
zos.closeEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
FileUtils.forceDelete(file);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return zipFile.getAbsolutePath();
}
//导入发布库数据 //导入发布库数据
......
package com.zzsn.knowbase.util;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @Description
* @ClassName ZipUtils
* @Author yuhuofei
* @Date 2022/8/10 20:37
* @Version 1.0
*/
@Slf4j
public class ZipUtils {
/**
* @param zipPathDir 压缩包路径 ,如 /home/data/zip-folder/
* @param zipFileName 压缩包名称 ,如 测试文件.zip
* @param fileList 要压缩的文件列表(绝对路径),如 /home/person/test/测试.doc,/home/person/haha/测试.doc
* @return
*/
public static void compressFiles(String zipPathDir, String zipFileName, List<String> fileList) {
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(zipPathDir + zipFileName+".zip")))) {
File zipFile = new File(zipPathDir);
if (!zipFile.exists()) {
zipFile.mkdirs();
}
for (String filePath : fileList) {
log.info("file package file :{}",filePath);
File file = new File(filePath);
if (file.exists()) {
int index = file.getName().lastIndexOf('.');
ZipEntry zipEntry = new ZipEntry(file.getName().substring(0, index) + "-" + dateRandom18() + file.getName().substring(index));
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[2048];
compressSingleFile(file, zos, buffer);
}
}
zos.flush();
} catch (Exception e) {
log.error("压缩所有文件成zip包出错{}" , e.getMessage());
}
}
//压缩单个文件
public static void compressSingleFile(File file, ZipOutputStream zos, byte[] buffer) {
int len;
try (FileInputStream fis = new FileInputStream(file)) {
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
zos.flush();
}
zos.closeEntry();
} catch (IOException e) {
System.out.println("====压缩单个文件异常====" + e);
}
}
//生成随机数
public static String dateRandom18() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
String date = simpleDateFormat.format(new Date());
String timeMillis = String.valueOf(System.currentTimeMillis());
String fiveNumber = timeMillis.substring(timeMillis.length() - 6);
String tempRandom = String.valueOf(Math.random());
String number = tempRandom.substring(tempRandom.length() - 4);
return date + fiveNumber + number;
}
//测试方法
public static void main(String[] args) {
String zipPathDir = "D:/";
String zipFileName = "测试文件.zip";
List<String> list = new ArrayList<>();
list.add("D:/a.txt");
list.add("D:/b.txt");
compressFiles(zipPathDir, zipFileName, list);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论