Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
d9d646e2
提交
d9d646e2
authored
3月 20, 2024
作者:
chenshiqiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
package
上级
6e1065b5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
112 行增加
和
66 行删除
+112
-66
KnowledgeController.java
...ava/com/zzsn/knowbase/controller/KnowledgeController.java
+3
-5
KnowledgeServiceImpl.java
.../com/zzsn/knowbase/service/impl/KnowledgeServiceImpl.java
+14
-61
ZipUtils.java
src/main/java/com/zzsn/knowbase/util/ZipUtils.java
+95
-0
没有找到文件。
src/main/java/com/zzsn/knowbase/controller/KnowledgeController.java
浏览文件 @
d9d646e2
...
@@ -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
()));
}
}
...
...
src/main/java/com/zzsn/knowbase/service/impl/KnowledgeServiceImpl.java
浏览文件 @
d9d646e2
...
@@ -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
();
}
//导入发布库数据
//导入发布库数据
...
...
src/main/java/com/zzsn/knowbase/util/ZipUtils.java
0 → 100644
浏览文件 @
d9d646e2
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论