Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
89af4415
提交
89af4415
authored
1月 09, 2024
作者:
ZhangJingKun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
文件上传 zhangjingkun
上级
6b6756c3
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
435 行增加
和
2 行删除
+435
-2
Constants.java
src/main/java/com/zzsn/knowbase/constant/Constants.java
+10
-1
KnowFile.java
src/main/java/com/zzsn/knowbase/entity/KnowFile.java
+2
-1
DocumentType.java
src/main/java/com/zzsn/knowbase/enums/DocumentType.java
+25
-0
ILocalFileService.java
...ain/java/com/zzsn/knowbase/service/ILocalFileService.java
+16
-0
KnowledgeServiceImpl.java
.../com/zzsn/knowbase/service/impl/KnowledgeServiceImpl.java
+12
-0
LocalFileServiceImpl.java
.../com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
+111
-0
DefaultFileUtility.java
.../java/com/zzsn/knowbase/util/file/DefaultFileUtility.java
+202
-0
FileUtility.java
src/main/java/com/zzsn/knowbase/util/file/FileUtility.java
+41
-0
application.yml
src/main/resources/application.yml
+16
-0
没有找到文件。
src/main/java/com/zzsn/knowbase/constant/Constants.java
浏览文件 @
89af4415
...
@@ -254,5 +254,14 @@ public class Constants {
...
@@ -254,5 +254,14 @@ public class Constants {
//招投标索引
//招投标索引
public
final
static
String
TENDER
=
"tender"
;
public
final
static
String
TENDER
=
"tender"
;
public
static
final
Integer
MAX_FILE_SIZE
=
5
*
1024
*
1024
;
public
static
final
Integer
CONVERT_TIMEOUT_MS
=
120000
;
public
static
final
String
CONVERTATION_ERROR_MESSAGE_TEMPLATE
=
"Error occurred in the ConvertService: "
;
public
static
final
Long
FULL_LOADING_IN_PERCENT
=
100L
;
public
static
final
Integer
FILE_SAVE_TIMEOUT
=
5000
;
public
static
final
Integer
MAX_KEY_LENGTH
=
20
;
public
static
final
Integer
ANONYMOUS_USER_ID
=
4
;
public
static
final
Integer
KILOBYTE_SIZE
=
1024
;
private
Constants
()
{
}
}
}
src/main/java/com/zzsn/knowbase/entity/KnowFile.java
浏览文件 @
89af4415
...
@@ -17,7 +17,8 @@ import lombok.NoArgsConstructor;
...
@@ -17,7 +17,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@NoArgsConstructor
public
class
KnowFile
{
public
class
KnowFile
{
private
String
fileId
;
private
String
fileId
;
private
String
fileName
;
private
String
filePath
;
private
String
filePath
;
private
String
fileType
;
private
String
fileType
;
private
Integer
fileSize
;
private
Long
fileSize
;
}
}
src/main/java/com/zzsn/knowbase/enums/DocumentType.java
0 → 100644
浏览文件 @
89af4415
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
com
.
zzsn
.
knowbase
.
enums
;
public
enum
DocumentType
{
word
,
cell
,
slide
}
src/main/java/com/zzsn/knowbase/service/ILocalFileService.java
0 → 100644
浏览文件 @
89af4415
package
com
.
zzsn
.
knowbase
.
service
;
import
com.zzsn.knowbase.entity.Knowledge
;
import
com.zzsn.knowbase.vo.Result
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* @Version 1.0
* @Author: ZhangJingKun
* @Date: 2024/1/9 9:34
* @Content: 文件上传下载服务
*/
public
interface
ILocalFileService
{
Result
<
Knowledge
>
upload
(
MultipartFile
file
,
String
uid
);
}
src/main/java/com/zzsn/knowbase/service/impl/KnowledgeServiceImpl.java
浏览文件 @
89af4415
...
@@ -12,9 +12,11 @@ import com.zzsn.knowbase.entity.Knowledge;
...
@@ -12,9 +12,11 @@ import com.zzsn.knowbase.entity.Knowledge;
import
com.zzsn.knowbase.kafka.message.KnowledgeMessage
;
import
com.zzsn.knowbase.kafka.message.KnowledgeMessage
;
import
com.zzsn.knowbase.kafka.producer.ProduceInfo
;
import
com.zzsn.knowbase.kafka.producer.ProduceInfo
;
import
com.zzsn.knowbase.service.IKnowledgeService
;
import
com.zzsn.knowbase.service.IKnowledgeService
;
import
com.zzsn.knowbase.service.ILocalFileService
;
import
com.zzsn.knowbase.util.*
;
import
com.zzsn.knowbase.util.*
;
import
com.zzsn.knowbase.vo.KnowledgeParam
;
import
com.zzsn.knowbase.vo.KnowledgeParam
;
import
com.zzsn.knowbase.vo.KnowledgeVO
;
import
com.zzsn.knowbase.vo.KnowledgeVO
;
import
com.zzsn.knowbase.vo.Result
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.lucene.search.join.ScoreMode
;
import
org.apache.lucene.search.join.ScoreMode
;
import
org.elasticsearch.action.search.SearchRequest
;
import
org.elasticsearch.action.search.SearchRequest
;
...
@@ -32,6 +34,9 @@ import org.springframework.beans.BeanUtils;
...
@@ -32,6 +34,9 @@ import org.springframework.beans.BeanUtils;
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.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
org.springframework.web.util.WebUtils
;
import
springfox.documentation.spring.web.json.Json
;
import
springfox.documentation.spring.web.json.Json
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
...
@@ -64,10 +69,17 @@ class KnowledgeServiceImpl implements IKnowledgeService {
...
@@ -64,10 +69,17 @@ class KnowledgeServiceImpl implements IKnowledgeService {
@Autowired
@Autowired
private
RedisUtil
redisUtil
;
private
RedisUtil
redisUtil
;
@Autowired
private
ILocalFileService
localFileService
;
@Override
@Override
public
void
addKnowledge
(
HttpServletRequest
httpServletRequest
,
Knowledge
knowledge
)
{
public
void
addKnowledge
(
HttpServletRequest
httpServletRequest
,
Knowledge
knowledge
)
{
MultipartHttpServletRequest
multipartRequest
=
WebUtils
.
getNativeRequest
(
httpServletRequest
,
MultipartHttpServletRequest
.
class
);
MultipartFile
file
=
multipartRequest
.
getFile
(
"file"
);
Result
<
Knowledge
>
result
=
localFileService
.
upload
(
file
,
knowledge
.
getId
());
if
(
null
==
knowledge
.
getId
())
{
if
(
null
==
knowledge
.
getId
())
{
knowledge
.
setId
(
codeGenerateUtil
.
geneIdNo
(
Constants
.
FINANCE
,
8
));
knowledge
.
setId
(
codeGenerateUtil
.
geneIdNo
(
Constants
.
FINANCE
,
8
));
}
}
...
...
src/main/java/com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
0 → 100644
浏览文件 @
89af4415
package
com
.
zzsn
.
knowbase
.
service
.
impl
;
import
com.zzsn.knowbase.entity.KnowFile
;
import
com.zzsn.knowbase.entity.Knowledge
;
import
com.zzsn.knowbase.service.ILocalFileService
;
import
com.zzsn.knowbase.util.file.FileUtility
;
import
com.zzsn.knowbase.vo.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.time.LocalDate
;
import
java.util.UUID
;
/**
* @Version 1.0
* @Author: ZhangJingKun
* @Date: 2024/1/9 9:36
* @Content:
*/
@Service
@Slf4j
public
class
LocalFileServiceImpl
implements
ILocalFileService
{
@Autowired
private
FileUtility
fileUtility
;
@Value
(
"${files.storage}"
)
String
filesStorage
;
@Override
public
Result
<
Knowledge
>
upload
(
MultipartFile
file
,
String
uid
)
{
try
{
String
fullFileName
=
file
.
getOriginalFilename
();
// get file name
String
fileExtension
=
fileUtility
.
getFileExtension
(
fullFileName
);
// get file extension
long
fileSize
=
file
.
getSize
();
// get file size
// check if the file size exceeds the maximum file size or is less than 0
if
(
fileUtility
.
getMaxFileSize
()
<
fileSize
||
fileSize
<=
0
)
{
Result
result
=
Result
.
error
(
"文件大小不正确!"
);
return
result
;
}
// check if file extension is supported by the editor
if
(!
fileUtility
.
getFileExts
().
contains
(
fileExtension
))
{
Result
result
=
Result
.
error
(
"不支持的文件类型!"
);
return
result
;
}
String
fileName
=
file
.
getOriginalFilename
();
String
fileSuffix
=
getFileSuffix
(
fileName
);
String
filePath
=
getFilePath
()
+
UUID
.
randomUUID
();
byte
[]
bytes
=
file
.
getBytes
();
// get file in bytes
//Files.write(Paths.get(filePath), bytes);
file
.
transferTo
(
new
File
(
filePath
));
KnowFile
knowFile
=
new
KnowFile
();
knowFile
.
setFileId
(
fileName
);
knowFile
.
setFileName
(
fileName
);
knowFile
.
setFilePath
(
filePath
);
knowFile
.
setFileType
(
fileSuffix
);
knowFile
.
setFileSize
(
fileSize
);
Result
result
=
Result
.
OK
(
knowFile
);
return
result
;
// create user metadata and return it
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// if the operation of file uploading is unsuccessful, an error occurs
Result
result
=
Result
.
error
(
"上传文件时出现问题!"
);
return
result
;
}
private
String
getFilePath
(){
LocalDate
currentDate
=
LocalDate
.
now
();
//System.out.println("当前日期: " + currentDate);
String
filePath
=
filesStorage
+
currentDate
+
"\\"
;
//判断文件夹是否存在,不存在创建
Path
directory
=
Paths
.
get
(
filePath
);
if
(!
Files
.
exists
(
directory
))
{
try
{
Files
.
createDirectories
(
directory
);
log
.
info
(
"文件夹创建成功:"
+
filePath
);
}
catch
(
IOException
e
)
{
log
.
error
(
"文件夹创建失败:"
+
filePath
);
e
.
printStackTrace
();
}
}
return
filePath
;
}
private
String
getFileSuffix
(
String
fileName
){
int
lastIndexOfDot
=
fileName
.
lastIndexOf
(
'.'
);
String
fileExtension
=
""
;
if
(
lastIndexOfDot
!=
-
1
)
{
fileExtension
=
fileName
.
substring
(
lastIndexOfDot
+
1
);
}
return
fileExtension
;
}
}
src/main/java/com/zzsn/knowbase/util/file/DefaultFileUtility.java
0 → 100644
浏览文件 @
89af4415
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
com
.
zzsn
.
knowbase
.
util
.
file
;
import
com.zzsn.knowbase.constant.Constants
;
import
com.zzsn.knowbase.enums.DocumentType
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
@Component
@Qualifier
(
"default"
)
public
class
DefaultFileUtility
implements
FileUtility
{
@Value
(
"${filesize-max}"
)
private
String
filesizeMax
;
@Value
(
"${files.docservice.viewed-docs}"
)
private
String
docserviceViewedDocs
;
@Value
(
"${files.docservice.edited-docs}"
)
private
String
docserviceEditedDocs
;
@Value
(
"${files.docservice.convert-docs}"
)
private
String
docserviceConvertDocs
;
@Value
(
"${files.docservice.fillforms-docs}"
)
private
String
docserviceFillDocs
;
// document extensions
private
List
<
String
>
extsDocument
=
Arrays
.
asList
(
".doc"
,
".docx"
,
".docm"
,
".dot"
,
".dotx"
,
".dotm"
,
".odt"
,
".fodt"
,
".ott"
,
".rtf"
,
".txt"
,
".html"
,
".htm"
,
".mht"
,
".xml"
,
".pdf"
,
".djvu"
,
".fb2"
,
".epub"
,
".xps"
,
".oform"
);
// spreadsheet extensions
private
List
<
String
>
extsSpreadsheet
=
Arrays
.
asList
(
".xls"
,
".xlsx"
,
".xlsm"
,
".xlsb"
,
".xlt"
,
".xltx"
,
".xltm"
,
".ods"
,
".fods"
,
".ots"
,
".csv"
);
// presentation extensions
private
List
<
String
>
extsPresentation
=
Arrays
.
asList
(
".pps"
,
".ppsx"
,
".ppsm"
,
".ppt"
,
".pptx"
,
".pptm"
,
".pot"
,
".potx"
,
".potm"
,
".odp"
,
".fodp"
,
".otp"
);
// get the document type
public
DocumentType
getDocumentType
(
final
String
fileName
)
{
String
ext
=
getFileExtension
(
fileName
).
toLowerCase
();
// get file extension from its name
// word type for document extensions
if
(
extsDocument
.
contains
(
ext
))
{
return
DocumentType
.
word
;
}
// cell type for spreadsheet extensions
if
(
extsSpreadsheet
.
contains
(
ext
))
{
return
DocumentType
.
cell
;
}
// slide type for presentation extensions
if
(
extsPresentation
.
contains
(
ext
))
{
return
DocumentType
.
slide
;
}
// default file type is word
return
DocumentType
.
word
;
}
// get file name from its URL
public
String
getFileName
(
final
String
url
)
{
if
(
url
==
null
)
{
return
""
;
}
// get file name from the last part of URL
String
fileName
=
url
.
substring
(
url
.
lastIndexOf
(
'/'
)
+
1
);
fileName
=
fileName
.
split
(
"\\?"
)[
0
];
return
fileName
;
}
// get file name without extension
public
String
getFileNameWithoutExtension
(
final
String
url
)
{
String
fileName
=
getFileName
(
url
);
if
(
fileName
==
null
)
{
return
null
;
}
String
fileNameWithoutExt
=
fileName
.
substring
(
0
,
fileName
.
lastIndexOf
(
'.'
));
return
fileNameWithoutExt
;
}
// get file extension from URL
public
String
getFileExtension
(
final
String
url
)
{
String
fileName
=
getFileName
(
url
);
if
(
fileName
==
null
)
{
return
null
;
}
String
fileExt
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
return
fileExt
.
toLowerCase
();
}
// get an editor internal extension
public
String
getInternalExtension
(
final
DocumentType
type
)
{
// .docx for word file type
if
(
type
.
equals
(
DocumentType
.
word
))
{
return
".docx"
;
}
// .xlsx for cell file type
if
(
type
.
equals
(
DocumentType
.
cell
))
{
return
".xlsx"
;
}
// .pptx for slide file type
if
(
type
.
equals
(
DocumentType
.
slide
))
{
return
".pptx"
;
}
// the default file type is .docx
return
".docx"
;
}
public
List
<
String
>
getFillExts
()
{
return
Arrays
.
asList
(
docserviceFillDocs
.
split
(
"\\|"
));
}
// get file extensions that can be viewed
public
List
<
String
>
getViewedExts
()
{
return
Arrays
.
asList
(
docserviceViewedDocs
.
split
(
"\\|"
));
}
// get file extensions that can be edited
public
List
<
String
>
getEditedExts
()
{
return
Arrays
.
asList
(
docserviceEditedDocs
.
split
(
"\\|"
));
}
// get file extensions that can be converted
public
List
<
String
>
getConvertExts
()
{
return
Arrays
.
asList
(
docserviceConvertDocs
.
split
(
"\\|"
));
}
// get all the supported file extensions
public
List
<
String
>
getFileExts
()
{
List
<
String
>
res
=
new
ArrayList
<>();
res
.
addAll
(
getViewedExts
());
res
.
addAll
(
getEditedExts
());
res
.
addAll
(
getConvertExts
());
res
.
addAll
(
getFillExts
());
return
res
;
}
// generate the file path from file directory and name
public
Path
generateFilepath
(
final
String
directory
,
final
String
fullFileName
)
{
String
fileName
=
getFileNameWithoutExtension
(
fullFileName
);
// get file name without extension
String
fileExtension
=
getFileExtension
(
fullFileName
);
// get file extension
Path
path
=
Paths
.
get
(
directory
+
fullFileName
);
// get the path to the files with the specified name
for
(
int
i
=
1
;
Files
.
exists
(
path
);
i
++)
{
// run through all the files with the specified name
// get a name of each file without extension and add an index to it
fileName
=
getFileNameWithoutExtension
(
fullFileName
)
+
"("
+
i
+
")"
;
// create a new path for this file with the correct name and extension
path
=
Paths
.
get
(
directory
+
fileName
+
fileExtension
);
}
path
=
Paths
.
get
(
directory
+
fileName
+
fileExtension
);
return
path
;
}
// get maximum file size
public
long
getMaxFileSize
()
{
long
size
=
Long
.
parseLong
(
filesizeMax
);
return
size
>
0
?
size
:
Constants
.
MAX_FILE_SIZE
;
}
}
src/main/java/com/zzsn/knowbase/util/file/FileUtility.java
0 → 100644
浏览文件 @
89af4415
/**
*
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
com
.
zzsn
.
knowbase
.
util
.
file
;
import
com.zzsn.knowbase.enums.DocumentType
;
import
java.nio.file.Path
;
import
java.util.List
;
// specify the file utility functions
public
interface
FileUtility
{
DocumentType
getDocumentType
(
String
fileName
);
// get the document type
String
getFileName
(
String
url
);
// get file name from its URL
String
getFileNameWithoutExtension
(
String
url
);
// get file name without extension
String
getFileExtension
(
String
url
);
// get file extension from URL
String
getInternalExtension
(
DocumentType
type
);
// get an editor internal extension
List
<
String
>
getFileExts
();
// get all the supported file extensions
List
<
String
>
getFillExts
();
// get file extensions that can be filled
List
<
String
>
getViewedExts
();
// get file extensions that can be viewed
List
<
String
>
getEditedExts
();
// get file extensions that can be edited
List
<
String
>
getConvertExts
();
// get file extensions that can be converted
Path
generateFilepath
(
String
directory
,
String
fullFileName
);
/* generate the file path
from file directory and name */
long
getMaxFileSize
();
// get maximum file size
}
src/main/resources/application.yml
浏览文件 @
89af4415
...
@@ -2,6 +2,10 @@ server:
...
@@ -2,6 +2,10 @@ server:
port
:
9088
port
:
9088
spring
:
spring
:
servlet
:
multipart
:
max-request-size
:
1024MB
max-file-size
:
100MB
datasource
:
datasource
:
url
:
jdbc:mysql://114.116.44.11:3306/knowledge?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=True
url
:
jdbc:mysql://114.116.44.11:3306/knowledge?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=True
username
:
ciglobal
username
:
ciglobal
...
@@ -44,3 +48,15 @@ know:
...
@@ -44,3 +48,15 @@ know:
thirdpartyurl
:
thirdpartyurl
:
checkuserurl
:
http://127.0.0.1:9988/sys/checkToken
checkuserurl
:
http://127.0.0.1:9988/sys/checkToken
getusersurl
:
http://127.0.0.1:9988/sys/user/thirdparty
getusersurl
:
http://127.0.0.1:9988/sys/user/thirdparty
files
:
storage
:
E:\\aaa\\
docservice
:
fillforms-docs
:
.docx|.oform
viewed-docs
:
.djvu|.oxps|.pdf|.xps
edited-docs
:
.csv|.docm|.docx|.docxf|.dotm|.dotx|.epub|.fb2|.html|.odp|.ods|.odt|.otp|.ots|.ott|.potm|.potx|.ppsm|.ppsx|.pptm|.pptx|.rtf|.txt|.xlsm|.xlsx|.xltm|.xltx
convert-docs
:
.doc|.dot|.dps|.dpt|.epub|.et|.ett|.fb2|.fodp|.fods|.fodt|.htm|.html|.mht|.mhtml|.odp|.ods|.odt|.otp|.ots|.ott|.pot|.pps|.ppt|.rtf|.stw|.sxc|.sxi|.sxw|.wps|.wpt|.xls|.xlsb|.xlt|.xml
timeout
:
120000
history
:
postfix
:
-hist
filesize-max
:
5242880
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论