Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
c86fa3a8
提交
c86fa3a8
authored
1月 17, 2024
作者:
ZhangJingKun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改保存 zhangjingkun
上级
a0b877ff
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
63 行增加
和
11 行删除
+63
-11
KbFileEditKnowledgeController.java
...sn/knowbase/controller/KbFileEditKnowledgeController.java
+0
-1
DocumentServiceImpl.java
...a/com/zzsn/knowbase/service/impl/DocumentServiceImpl.java
+2
-1
LocalFileServiceImpl.java
.../com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
+38
-9
FileUtil.java
src/main/java/com/zzsn/knowbase/util/file/FileUtil.java
+23
-0
没有找到文件。
src/main/java/com/zzsn/knowbase/controller/KbFileEditKnowledgeController.java
浏览文件 @
c86fa3a8
...
@@ -31,7 +31,6 @@ public class KbFileEditKnowledgeController {
...
@@ -31,7 +31,6 @@ public class KbFileEditKnowledgeController {
/**
/**
* 临时保存,保存并审核通过, 保存并审核不通过
* 临时保存,保存并审核通过, 保存并审核不通过
* @param request
* @param knowledge
* @param knowledge
* @return
* @return
*
*
...
...
src/main/java/com/zzsn/knowbase/service/impl/DocumentServiceImpl.java
浏览文件 @
c86fa3a8
...
@@ -7,6 +7,7 @@ import com.zzsn.knowbase.entity.Document;
...
@@ -7,6 +7,7 @@ import com.zzsn.knowbase.entity.Document;
import
com.zzsn.knowbase.entity.DocumentEditParam
;
import
com.zzsn.knowbase.entity.DocumentEditParam
;
import
com.zzsn.knowbase.service.DocumentService
;
import
com.zzsn.knowbase.service.DocumentService
;
import
com.zzsn.knowbase.util.DocumentException
;
import
com.zzsn.knowbase.util.DocumentException
;
import
com.zzsn.knowbase.util.file.FileUtil
;
import
com.zzsn.knowbase.util.file.Md5Utils
;
import
com.zzsn.knowbase.util.file.Md5Utils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
...
@@ -72,7 +73,7 @@ public class DocumentServiceImpl implements DocumentService {
...
@@ -72,7 +73,7 @@ public class DocumentServiceImpl implements DocumentService {
// 校验文件实体
// 校验文件实体
preFileCheck
(
docFile
);
preFileCheck
(
docFile
);
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
String
fileKey
=
this
.
fileKey
(
docFile
,
fileName
);
String
fileKey
=
FileUtil
.
fileKey
(
docFile
,
fileName
);
Document
document
=
Document
.
builder
()
Document
document
=
Document
.
builder
()
.
fileType
(
fileType
)
.
fileType
(
fileType
)
.
title
(
fileName
)
.
title
(
fileName
)
...
...
src/main/java/com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
浏览文件 @
c86fa3a8
...
@@ -144,7 +144,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -144,7 +144,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
file
Name
,
"UTF-8"
));
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
file
Path
,
"UTF-8"
));
// 告知浏览器文件的大小
// 告知浏览器文件的大小
response
.
addHeader
(
"Content-Length"
,
""
+
file
.
length
());
response
.
addHeader
(
"Content-Length"
,
""
+
file
.
length
());
OutputStream
outputStream
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
OutputStream
outputStream
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
...
@@ -252,7 +252,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -252,7 +252,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
//File savedFile = new File("F:\\DataOfHongQuanzheng\\onlyoffice_data\\app_data\\"+fileName);
//File savedFile = new File("F:\\DataOfHongQuanzheng\\onlyoffice_data\\app_data\\"+fileName);
File
savedFile
=
new
File
(
filesStorage
+
fileName
);
File
savedFile
=
new
File
(
filesStorage
+
fileName
);
if
(
null
!=((
String
)
jsonObj
.
get
(
"userdata"
))&&((
String
)
jsonObj
.
get
(
"userdata"
)).
equals
(
"sample userdata"
))
{
if
(
null
!=((
String
)
jsonObj
.
get
(
"userdata"
))&&((
String
)
jsonObj
.
get
(
"userdata"
)).
equals
(
"sample userdata"
))
{
savedFile
=
new
File
(
filesStorage
+
"v1"
+
fileName
);
savedFile
=
new
File
(
filesStorage
+
fileName
);
}
}
...
@@ -303,12 +303,19 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -303,12 +303,19 @@ public class LocalFileServiceImpl implements ILocalFileService {
KnowFile
knowFile
=
files
.
get
(
0
);
KnowFile
knowFile
=
files
.
get
(
0
);
String
filePath
=
knowFile
.
getFilePath
();
String
filePath
=
knowFile
.
getFilePath
();
try
{
try
{
getDoucmentEditStatus
(
filePath
);
getDoucmentEditStatus
(
filePath
,
knowFile
.
getFileName
());
}
catch
(
ParseException
e
)
{
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
result
=
Result
.
error
(
"文件保存失败!"
);
result
=
Result
.
error
(
"文件保存失败!"
);
}
}
try
{
Thread
.
sleep
(
5000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
String
fileType
=
fileUtility
.
getFileType
(
knowFile
.
getFileName
());
String
fileType
=
fileUtility
.
getFileType
(
knowFile
.
getFileName
());
knowFile
.
setFileType
(
fileType
);
knowFile
.
setFileType
(
fileType
);
File
file
=
new
File
(
filesStorage
+
knowFile
.
getFilePath
());
File
file
=
new
File
(
filesStorage
+
knowFile
.
getFilePath
());
...
@@ -317,7 +324,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -317,7 +324,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
knowledgeService
.
addKnowledge
(
knowFile
,
knowledge
,
userInfo
);
knowledgeService
.
addKnowledge
(
knowFile
,
knowledge
,
userInfo
);
return
result
;
return
result
;
}
}
public
ResponseEntity
<
Object
>
getDoucmentEditStatus
(
String
filePath
)
throws
ParseException
{
public
ResponseEntity
<
Object
>
getDoucmentEditStatus
(
String
filePath
,
String
fileName
)
throws
ParseException
{
String
url
=
officeUrl
+
officeCommand
;
String
url
=
officeUrl
+
officeCommand
;
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
map
.
put
(
"c"
,
"forcesave"
);
map
.
put
(
"c"
,
"forcesave"
);
...
@@ -325,18 +332,40 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -325,18 +332,40 @@ public class LocalFileServiceImpl implements ILocalFileService {
if
(
StringUtils
.
isBlank
(
docFileMd5
))
{
if
(
StringUtils
.
isBlank
(
docFileMd5
))
{
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_MD5_ERROR
);
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_MD5_ERROR
);
}
}
String
pathShortMd5
=
Md5Utils
.
md5
(
filesStorage
+
filePath
);
//
String pathShortMd5 = Md5Utils.md5(filesStorage + filePath);
String
nameShortMd5
=
Md5Utils
.
md5
(
filePath
);
// String nameShortMd5 = Md5Utils.md5(fileName
);
Hashids
hashids
=
new
Hashids
(
DocumentConstants
.
HASH_KEY
);
//
Hashids hashids = new Hashids(DocumentConstants.HASH_KEY);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String
key
=
hashids
.
encodeHex
(
String
.
format
(
"%s%s%s"
,
docFileMd5
,
pathShortMd5
,
nameShortMd5
));
//String key = hashids.encodeHex(String.format("%s%s%s", docFileMd5,pathShortMd5, nameShortMd5));
map
.
put
(
"key"
,
key
);
File
docFile
=
new
File
(
filesStorage
+
filePath
);
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
String
fileKey
=
FileUtil
.
fileKey
(
docFile
,
fileName
);
map
.
put
(
"key"
,
fileKey
);
map
.
put
(
"userdata"
,
"sample userdata"
);
map
.
put
(
"userdata"
,
"sample userdata"
);
JSONObject
obj
=
(
JSONObject
)
new
JSONParser
().
parse
(
FileUtil
.
editStatus
(
url
,
JSON
.
toJSONString
(
map
)));
JSONObject
obj
=
(
JSONObject
)
new
JSONParser
().
parse
(
FileUtil
.
editStatus
(
url
,
JSON
.
toJSONString
(
map
)));
return
new
ResponseEntity
<
Object
>(
obj
,
HttpStatus
.
OK
);
return
new
ResponseEntity
<
Object
>(
obj
,
HttpStatus
.
OK
);
}
}
public
String
fileKey
(
File
docFile
,
String
name
)
{
String
docFileMd5
=
Md5Utils
.
getFileMd5
(
docFile
);
if
(
StringUtils
.
isBlank
(
docFileMd5
))
{
log
.
error
(
"$$$ 构建文件信息失败!计算文件 md5 失败!"
);
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_MD5_ERROR
);
}
String
pathShortMd5
=
Md5Utils
.
md5
(
docFile
.
getAbsolutePath
());
String
nameShortMd5
=
Md5Utils
.
md5
(
name
);
Hashids
hashids
=
new
Hashids
(
DocumentConstants
.
HASH_KEY
);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String
key
=
hashids
.
encodeHex
(
String
.
format
(
"%s%s%s"
,
docFileMd5
,
pathShortMd5
,
nameShortMd5
));
if
(
StringUtils
.
isBlank
(
key
))
{
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_KEY_ERROR
);
}
return
key
;
}
/**
/**
* 文件下载
* 文件下载
...
...
src/main/java/com/zzsn/knowbase/util/file/FileUtil.java
浏览文件 @
c86fa3a8
package
com
.
zzsn
.
knowbase
.
util
.
file
;
package
com
.
zzsn
.
knowbase
.
util
.
file
;
import
com.zzsn.knowbase.constant.DocumentConstants
;
import
com.zzsn.knowbase.constant.ErrorCodeEnum
;
import
com.zzsn.knowbase.util.DocumentException
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.hashids.Hashids
;
import
org.json.simple.JSONObject
;
import
org.json.simple.JSONObject
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
...
@@ -182,4 +187,22 @@ public class FileUtil {
...
@@ -182,4 +187,22 @@ public class FileUtil {
}
}
}
}
public
static
String
fileKey
(
File
docFile
,
String
name
)
{
String
docFileMd5
=
Md5Utils
.
getFileMd5
(
docFile
);
if
(
StringUtils
.
isBlank
(
docFileMd5
))
{
System
.
out
.
println
(
"$$$ 构建文件信息失败!计算文件 md5 失败!"
);
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_MD5_ERROR
);
}
String
pathShortMd5
=
Md5Utils
.
md5
(
docFile
.
getAbsolutePath
());
String
nameShortMd5
=
Md5Utils
.
md5
(
name
);
Hashids
hashids
=
new
Hashids
(
DocumentConstants
.
HASH_KEY
);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String
key
=
hashids
.
encodeHex
(
String
.
format
(
"%s%s%s"
,
docFileMd5
,
pathShortMd5
,
nameShortMd5
));
if
(
StringUtils
.
isBlank
(
key
))
{
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_KEY_ERROR
);
}
return
key
;
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论