Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
34077304
提交
34077304
authored
2月 06, 2024
作者:
ZhangJingKun
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
文件储存修改位minio服务器
上级
33452034
显示空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
347 行增加
和
32 行删除
+347
-32
pom.xml
pom.xml
+6
-0
DocumentServiceImpl.java
...a/com/zzsn/knowbase/service/impl/DocumentServiceImpl.java
+2
-4
KnowledgeServiceImpl.java
.../com/zzsn/knowbase/service/impl/KnowledgeServiceImpl.java
+5
-6
LocalFileServiceImpl.java
.../com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
+43
-22
MinioConfig.java
src/main/java/com/zzsn/knowbase/util/MinioConfig.java
+37
-0
MinioUtil.java
src/main/java/com/zzsn/knowbase/util/MinioUtil.java
+220
-0
FileUtil.java
src/main/java/com/zzsn/knowbase/util/file/FileUtil.java
+12
-0
Md5Utils.java
src/main/java/com/zzsn/knowbase/util/file/Md5Utils.java
+17
-0
application.yml
src/main/resources/application.yml
+5
-0
没有找到文件。
pom.xml
浏览文件 @
34077304
...
@@ -209,6 +209,12 @@
...
@@ -209,6 +209,12 @@
<version>
2.2.10
</version>
<version>
2.2.10
</version>
</dependency>
</dependency>
<dependency>
<groupId>
io.minio
</groupId>
<artifactId>
minio
</artifactId>
<version>
8.0.3
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/zzsn/knowbase/service/impl/DocumentServiceImpl.java
浏览文件 @
34077304
...
@@ -69,11 +69,9 @@ public class DocumentServiceImpl implements DocumentService {
...
@@ -69,11 +69,9 @@ public class DocumentServiceImpl implements DocumentService {
if
(
StringUtils
.
isNotBlank
(
fileName
)
&&
!
fileType
.
equalsIgnoreCase
(
FilenameUtils
.
getExtension
(
fileName
)))
{
if
(
StringUtils
.
isNotBlank
(
fileName
)
&&
!
fileType
.
equalsIgnoreCase
(
FilenameUtils
.
getExtension
(
fileName
)))
{
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_EXTENSION_NOT_MATCH
);
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_EXTENSION_NOT_MATCH
);
}
}
File
docFile
=
new
File
(
path
);
// 校验文件实体
// 校验文件实体
preFileCheck
(
docFile
);
//preFileCheck(docFile);
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
String
fileKey
=
FileUtil
.
fileKey
(
filePath
,
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/KnowledgeServiceImpl.java
浏览文件 @
34077304
...
@@ -41,10 +41,7 @@ import org.springframework.web.multipart.MultipartFile;
...
@@ -41,10 +41,7 @@ import org.springframework.web.multipart.MultipartFile;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.ByteArrayInputStream
;
import
java.io.*
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.*
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
...
@@ -104,8 +101,10 @@ class KnowledgeServiceImpl implements IKnowledgeService {
...
@@ -104,8 +101,10 @@ class KnowledgeServiceImpl implements IKnowledgeService {
List
<
String
>
contentStringList
=
new
ArrayList
<>();
List
<
String
>
contentStringList
=
new
ArrayList
<>();
String
html
=
null
;
String
html
=
null
;
try
{
try
{
File
file
=
new
File
(
filesStorage
+
knowledge
.
getFiles
().
get
(
0
).
getFilePath
());
// File file = new File(filesStorage + knowledge.getFiles().get(0).getFilePath());
html
=
DocUtil
.
convertDocStream2Html
(
new
FileInputStream
(
file
));
// html = DocUtil.convertDocStream2Html(new FileInputStream(file));
InputStream
fis
=
MinioUtil
.
getMinioFile
(
knowledge
.
getFiles
().
get
(
0
).
getFilePath
());
html
=
DocUtil
.
convertDocStream2Html
(
fis
);
String
htmlWithTable
=
html
.
replace
(
"</p>"
,
"######</p>"
);
String
htmlWithTable
=
html
.
replace
(
"</p>"
,
"######</p>"
);
htmlWithTable
=
htmlWithTable
.
replace
(
"</title>"
,
"######</title>"
);
htmlWithTable
=
htmlWithTable
.
replace
(
"</title>"
,
"######</title>"
);
htmlWithTable
=
htmlWithTable
.
replace
(
"</h1>"
,
"######</h1>"
);
htmlWithTable
=
htmlWithTable
.
replace
(
"</h1>"
,
"######</h1>"
);
...
...
src/main/java/com/zzsn/knowbase/service/impl/LocalFileServiceImpl.java
浏览文件 @
34077304
...
@@ -16,6 +16,7 @@ import com.zzsn.knowbase.util.file.FileUtil;
...
@@ -16,6 +16,7 @@ import com.zzsn.knowbase.util.file.FileUtil;
import
com.zzsn.knowbase.util.file.FileUtility
;
import
com.zzsn.knowbase.util.file.FileUtility
;
import
com.zzsn.knowbase.util.file.Md5Utils
;
import
com.zzsn.knowbase.util.file.Md5Utils
;
import
com.zzsn.knowbase.vo.Result
;
import
com.zzsn.knowbase.vo.Result
;
import
io.minio.GetObjectArgs
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.hashids.Hashids
;
import
org.hashids.Hashids
;
...
@@ -41,6 +42,7 @@ import java.net.URLEncoder;
...
@@ -41,6 +42,7 @@ import java.net.URLEncoder;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
java.time.LocalDate
;
import
java.time.LocalDate
;
import
java.util.*
;
import
java.util.*
;
...
@@ -75,6 +77,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -75,6 +77,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
@Override
@Override
public
Result
<
KnowFile
>
upload
(
MultipartFile
file
)
{
public
Result
<
KnowFile
>
upload
(
MultipartFile
file
)
{
//file.getOriginalFilename()文件的原始名称 file.getName()前端传过来的参数名称
try
{
try
{
String
fileName
=
file
.
getOriginalFilename
();
// 获取文件名称
String
fileName
=
file
.
getOriginalFilename
();
// 获取文件名称
String
fileExtension
=
fileUtility
.
getFileExtension
(
fileName
);
// 获取文件扩展名
String
fileExtension
=
fileUtility
.
getFileExtension
(
fileName
);
// 获取文件扩展名
...
@@ -97,9 +100,9 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -97,9 +100,9 @@ public class LocalFileServiceImpl implements ILocalFileService {
String
fileId
=
codeGenerateUtil
.
geneIdNo
(
Constants
.
FINANCE
,
8
);
String
fileId
=
codeGenerateUtil
.
geneIdNo
(
Constants
.
FINANCE
,
8
);
String
filePath
=
getFilePath
()
+
fileId
+
fileExtension
;
String
filePath
=
getFilePath
()
+
fileId
+
fileExtension
;
//byte[] bytes = file.getBytes(); // get file in bytes
//
Files.write(Paths.get(filePath), bytes);
//
file.transferTo(new File(filesStorage + filePath)); //文件存本地
file
.
transferTo
(
new
File
(
filesStorage
+
filePath
));
MinioUtil
.
upload
(
file
,
filePath
);
//文件存minio
KnowFile
knowFile
=
new
KnowFile
();
KnowFile
knowFile
=
new
KnowFile
();
knowFile
.
setFileId
(
fileId
);
knowFile
.
setFileId
(
fileId
);
...
@@ -124,7 +127,6 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -124,7 +127,6 @@ public class LocalFileServiceImpl implements ILocalFileService {
List
<
KnowFile
>
list
=
new
ArrayList
<>();
List
<
KnowFile
>
list
=
new
ArrayList
<>();
Result
<
List
<
KnowFile
>>
res
=
Result
.
OK
(
list
);
Result
<
List
<
KnowFile
>>
res
=
Result
.
OK
(
list
);
for
(
Map
.
Entry
<
String
,
MultipartFile
>
entity
:
fileMap
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
MultipartFile
>
entity
:
fileMap
.
entrySet
())
{
MultipartFile
file
=
entity
.
getValue
();
// 获取上传文件对象
MultipartFile
file
=
entity
.
getValue
();
// 获取上传文件对象
Result
<
KnowFile
>
result
=
upload
(
file
);
Result
<
KnowFile
>
result
=
upload
(
file
);
...
@@ -141,12 +143,23 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -141,12 +143,23 @@ public class LocalFileServiceImpl implements ILocalFileService {
public
void
download
(
String
fileName
,
String
filePath
,
HttpServletResponse
response
)
{
public
void
download
(
String
fileName
,
String
filePath
,
HttpServletResponse
response
)
{
// path是指想要下载的文件的路径
// path是指想要下载的文件的路径
File
file
=
new
File
(
filesStorage
+
filePath
);
//
File file = new File(filesStorage + filePath);
try
{
try
{
// 将文件写入输入流
// 将文件写入输入流
FileInputStream
fileInputStream
=
new
FileInputStream
(
file
);
//FileInputStream fileInputStream = new FileInputStream(file);
InputStream
fis
=
new
BufferedInputStream
(
fileInputStream
);
//InputStream fis = new BufferedInputStream(fileInputStream);
byte
[]
buffer
=
new
byte
[
fis
.
available
()];
InputStream
fis
=
MinioUtil
.
getMinioFile
(
filePath
);
ByteArrayOutputStream
output
=
new
ByteArrayOutputStream
();
// 创建一个缓冲区大小为4096的字节数组
byte
[]
b
=
new
byte
[
4096
];
int
bytesRead
;
while
((
bytesRead
=
fis
.
read
(
b
))
!=
-
1
)
{
output
.
write
(
b
,
0
,
bytesRead
);
}
byte
[]
buffer
=
output
.
toByteArray
();
output
.
close
();
fis
.
read
(
buffer
);
fis
.
read
(
buffer
);
fis
.
close
();
fis
.
close
();
// 清空response
// 清空response
...
@@ -158,7 +171,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -158,7 +171,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
filePath
,
"UTF-8"
));
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
filePath
,
"UTF-8"
));
// 告知浏览器文件的大小
// 告知浏览器文件的大小
response
.
addHeader
(
"Content-Length"
,
""
+
file
.
length
()
);
response
.
addHeader
(
"Content-Length"
,
""
+
buffer
.
length
);
OutputStream
outputStream
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
OutputStream
outputStream
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
response
.
setContentType
(
"application/octet-stream"
);
response
.
setContentType
(
"application/octet-stream"
);
outputStream
.
write
(
buffer
);
outputStream
.
write
(
buffer
);
...
@@ -273,20 +286,28 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -273,20 +286,28 @@ public class LocalFileServiceImpl implements ILocalFileService {
InputStream
stream
=
connection
.
getInputStream
();
InputStream
stream
=
connection
.
getInputStream
();
//更换为实际的路径F:\DataOfHongQuanzheng\java\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Java Example\\app_data\192.168.56.1\
//更换为实际的路径F:\DataOfHongQuanzheng\java\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Java Example\\app_data\192.168.56.1\
//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
+
fileName
);
// savedFile = new File(filesStorage + fileName);
}
// }
//
//
// try (FileOutputStream out = new FileOutputStream(savedFile)) {
// int read;
// final byte[] bytes = new byte[1024];
// while ((read = stream.read(bytes)) != -1) {
// out.write(bytes, 0, read);
// }
// out.flush();
// }
try
(
FileOutputStream
out
=
new
FileOutputStream
(
savedFile
))
{
try
{
int
read
;
MinioUtil
.
upload
(
stream
,
fileName
);
final
byte
[]
bytes
=
new
byte
[
1024
];
}
catch
(
Exception
e
)
{
while
((
read
=
stream
.
read
(
bytes
))
!=
-
1
)
{
e
.
printStackTrace
();
out
.
write
(
bytes
,
0
,
read
);
log
.
error
(
"回调函数保存文件失败!"
);
}
out
.
flush
();
}
}
connection
.
disconnect
();
connection
.
disconnect
();
}
}
...
@@ -366,7 +387,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
...
@@ -366,7 +387,7 @@ public class LocalFileServiceImpl implements ILocalFileService {
File
docFile
=
new
File
(
filesStorage
+
filePath
);
File
docFile
=
new
File
(
filesStorage
+
filePath
);
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
fileName
=
StringUtils
.
isNotBlank
(
fileName
)
?
fileName
:
docFile
.
getName
();
String
fileKey
=
FileUtil
.
fileKey
(
docFile
,
fileName
);
String
fileKey
=
FileUtil
.
fileKey
(
filePath
,
fileName
);
map
.
put
(
"key"
,
fileKey
);
map
.
put
(
"key"
,
fileKey
);
map
.
put
(
"userdata"
,
"sample userdata"
);
map
.
put
(
"userdata"
,
"sample userdata"
);
...
...
src/main/java/com/zzsn/knowbase/util/MinioConfig.java
0 → 100644
浏览文件 @
34077304
package
com
.
zzsn
.
knowbase
.
util
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
/**
* Minio文件上传配置文件
*/
@Slf4j
@Configuration
public
class
MinioConfig
{
@Value
(
value
=
"${minio.minio_url:}"
)
private
String
minioUrl
;
@Value
(
value
=
"${minio.minio_name:}"
)
private
String
minioName
;
@Value
(
value
=
"${minio.minio_pass:}"
)
private
String
minioPass
;
@Value
(
value
=
"${minio.bucketName:}"
)
private
String
bucketName
;
@Bean
public
void
initMinio
(){
if
(!
minioUrl
.
startsWith
(
"http"
)){
minioUrl
=
"http://"
+
minioUrl
;
}
if
(!
minioUrl
.
endsWith
(
"/"
)){
minioUrl
=
minioUrl
.
concat
(
"/"
);
}
MinioUtil
.
setMinioUrl
(
minioUrl
);
MinioUtil
.
setMinioName
(
minioName
);
MinioUtil
.
setMinioPass
(
minioPass
);
MinioUtil
.
setBucketName
(
bucketName
);
}
}
src/main/java/com/zzsn/knowbase/util/MinioUtil.java
0 → 100644
浏览文件 @
34077304
package
com
.
zzsn
.
knowbase
.
util
;
import
io.minio.*
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.InputStream
;
import
java.net.URLDecoder
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.PatternSyntaxException
;
/**
* minio文件上传工具类
*/
@Slf4j
public
class
MinioUtil
{
private
static
String
minioUrl
;
//minio 访问aip地址
private
static
String
minioName
;
//用户名
private
static
String
minioPass
;
//密码
private
static
String
bucketName
;
//桶名
public
static
void
setMinioUrl
(
String
minioUrl
)
{
MinioUtil
.
minioUrl
=
minioUrl
;
}
public
static
void
setMinioName
(
String
minioName
)
{
MinioUtil
.
minioName
=
minioName
;
}
public
static
void
setMinioPass
(
String
minioPass
)
{
MinioUtil
.
minioPass
=
minioPass
;
}
public
static
void
setBucketName
(
String
bucketName
)
{
MinioUtil
.
bucketName
=
bucketName
;
}
public
static
String
getMinioUrl
()
{
return
minioUrl
;
}
public
static
String
getBucketName
()
{
return
bucketName
;
}
private
static
MinioClient
minioClient
=
null
;
/**
* 上传文件
* @param file
* @return
*/
public
static
String
upload
(
MultipartFile
file
,
String
objectName
)
{
String
file_url
=
""
;
try
{
initMinio
(
minioUrl
,
minioName
,
minioPass
);
InputStream
stream
=
file
.
getInputStream
();
// 使用putObject上传一个本地文件到存储桶中。
if
(
objectName
.
startsWith
(
"/"
)){
objectName
=
objectName
.
substring
(
1
);
}
PutObjectArgs
objectArgs
=
PutObjectArgs
.
builder
().
object
(
objectName
)
.
bucket
(
bucketName
)
.
contentType
(
"application/octet-stream"
)
.
stream
(
stream
,
stream
.
available
(),-
1
).
build
();
minioClient
.
putObject
(
objectArgs
);
stream
.
close
();
file_url
=
minioUrl
+
bucketName
+
"/"
+
objectName
;
}
catch
(
Exception
e
){
log
.
error
(
e
.
getMessage
(),
e
);
}
return
file_url
;
}
/**
* 获取文件流
* @param bucketName
* @param objectName
* @return
*/
public
static
InputStream
getMinioFile
(
String
bucketName
,
String
objectName
){
InputStream
inputStream
=
null
;
try
{
initMinio
(
minioUrl
,
minioName
,
minioPass
);
GetObjectArgs
objectArgs
=
GetObjectArgs
.
builder
().
object
(
objectName
)
.
bucket
(
bucketName
).
build
();
inputStream
=
minioClient
.
getObject
(
objectArgs
);
}
catch
(
Exception
e
)
{
log
.
info
(
"文件获取失败"
+
e
.
getMessage
());
}
return
inputStream
;
}
public
static
InputStream
getMinioFile
(
String
objectName
){
return
getMinioFile
(
bucketName
,
objectName
);
}
/**
* 删除文件
* @param bucketName
* @param objectName
* @throws Exception
*/
public
static
void
removeObject
(
String
bucketName
,
String
objectName
)
{
try
{
initMinio
(
minioUrl
,
minioName
,
minioPass
);
RemoveObjectArgs
objectArgs
=
RemoveObjectArgs
.
builder
().
object
(
objectName
)
.
bucket
(
bucketName
).
build
();
minioClient
.
removeObject
(
objectArgs
);
}
catch
(
Exception
e
){
log
.
info
(
"文件删除失败"
+
e
.
getMessage
());
}
}
/**
* 获取文件外链
* @param bucketName
* @param objectName
* @param expires
* @return
*/
public
static
String
getObjectURL
(
String
bucketName
,
String
objectName
,
Integer
expires
)
{
initMinio
(
minioUrl
,
minioName
,
minioPass
);
try
{
GetPresignedObjectUrlArgs
objectArgs
=
GetPresignedObjectUrlArgs
.
builder
().
object
(
objectName
)
.
bucket
(
bucketName
)
.
expiry
(
expires
).
build
();
String
url
=
minioClient
.
getPresignedObjectUrl
(
objectArgs
);
return
URLDecoder
.
decode
(
url
,
"UTF-8"
);
}
catch
(
Exception
e
){
log
.
info
(
"文件路径获取失败"
+
e
.
getMessage
());
}
return
null
;
}
/**
* 初始化客户端
* @param minioUrl
* @param minioName
* @param minioPass
* @return
*/
private
static
MinioClient
initMinio
(
String
minioUrl
,
String
minioName
,
String
minioPass
)
{
if
(
minioClient
==
null
)
{
try
{
minioClient
=
MinioClient
.
builder
()
.
endpoint
(
minioUrl
)
.
credentials
(
minioName
,
minioPass
)
.
build
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
minioClient
;
}
/**
* 上传文件到minio
* @param stream
* @param relativePath
* @return
*/
public
static
String
upload
(
InputStream
stream
,
String
relativePath
)
throws
Exception
{
initMinio
(
minioUrl
,
minioName
,
minioPass
);
if
(
minioClient
.
bucketExists
(
BucketExistsArgs
.
builder
().
bucket
(
bucketName
).
build
()))
{
log
.
info
(
"Bucket already exists."
);
}
else
{
// 创建一个名为ota的存储桶
minioClient
.
makeBucket
(
MakeBucketArgs
.
builder
().
bucket
(
bucketName
).
build
());
log
.
info
(
"create a new bucket."
);
}
PutObjectArgs
objectArgs
=
PutObjectArgs
.
builder
().
object
(
relativePath
)
.
bucket
(
bucketName
)
.
contentType
(
"application/octet-stream"
)
.
stream
(
stream
,
stream
.
available
(),-
1
).
build
();
minioClient
.
putObject
(
objectArgs
);
stream
.
close
();
return
minioUrl
+
bucketName
+
"/"
+
relativePath
;
}
public
static
String
filter
(
String
str
)
throws
PatternSyntaxException
{
// 清除掉所有特殊字符
String
regEx
=
"[`_《》~!@#$%^&*()+=|{}':;',\\[\\].<>?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"
;
Pattern
p
=
Pattern
.
compile
(
regEx
);
Matcher
m
=
p
.
matcher
(
str
);
return
m
.
replaceAll
(
""
).
trim
();
}
public
static
boolean
isNotEmpty
(
Object
object
)
{
if
(
object
!=
null
&&
!
object
.
equals
(
""
)
&&
!
object
.
equals
(
"null"
))
{
return
(
true
);
}
return
(
false
);
}
/**
* 判断文件名是否带盘符,重新处理
* @param fileName
* @return
*/
public
static
String
getFileName
(
String
fileName
){
//判断是否带有盘符信息
// Check for Unix-style path
int
unixSep
=
fileName
.
lastIndexOf
(
'/'
);
// Check for Windows-style path
int
winSep
=
fileName
.
lastIndexOf
(
'\\'
);
// Cut off at latest possible point
int
pos
=
(
winSep
>
unixSep
?
winSep
:
unixSep
);
if
(
pos
!=
-
1
)
{
// Any sort of path separator found...
fileName
=
fileName
.
substring
(
pos
+
1
);
}
//替换上传文件名字的特殊字符
fileName
=
fileName
.
replace
(
"="
,
""
).
replace
(
","
,
""
).
replace
(
"&"
,
""
).
replace
(
"#"
,
""
);
//替换上传文件名字中的空格
fileName
=
fileName
.
replaceAll
(
"\\s"
,
""
);
return
fileName
;
}
}
src/main/java/com/zzsn/knowbase/util/file/FileUtil.java
浏览文件 @
34077304
...
@@ -205,4 +205,16 @@ public class FileUtil {
...
@@ -205,4 +205,16 @@ public class FileUtil {
}
}
return
key
;
return
key
;
}
}
public
static
String
fileKey
(
String
filePath
,
String
name
)
{
String
pathShortMd5
=
Md5Utils
.
md5
(
filePath
);
String
nameShortMd5
=
Md5Utils
.
md5
(
name
);
Hashids
hashids
=
new
Hashids
(
DocumentConstants
.
HASH_KEY
);
// (将路径字符串短md5值 + 名称字符串短md5值) ==> 再转成短id形式 ==> 作为文档的key(暂且认为是不会重复的)
String
key
=
hashids
.
encodeHex
(
String
.
format
(
"%s%s"
,
pathShortMd5
,
nameShortMd5
));
if
(
StringUtils
.
isBlank
(
key
))
{
throw
new
DocumentException
(
ErrorCodeEnum
.
DOC_FILE_KEY_ERROR
);
}
return
key
;
}
}
}
src/main/java/com/zzsn/knowbase/util/file/Md5Utils.java
浏览文件 @
34077304
...
@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
...
@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.security.MessageDigest
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.NoSuchAlgorithmException
;
...
@@ -64,6 +65,22 @@ public class Md5Utils {
...
@@ -64,6 +65,22 @@ public class Md5Utils {
}
}
/**
/**
* 对一个文件流获取md5值
*/
public
static
String
getFileMd5
(
InputStream
fis
)
{
try
{
byte
[]
buffer
=
new
byte
[
8192
];
int
length
;
while
((
length
=
fis
.
read
(
buffer
))
!=
-
1
)
{
MD5
.
update
(
buffer
,
0
,
length
);
}
return
new
String
(
Hex
.
encodeHex
(
MD5
.
digest
()));
}
catch
(
IOException
e
)
{
log
.
error
(
"$$$ 获取文件md5失败!"
,
e
);
return
null
;
}
}
/**
* 计算字符串的md5值
* 计算字符串的md5值
* @param target 字符串
* @param target 字符串
* @return md5 value
* @return md5 value
...
...
src/main/resources/application.yml
浏览文件 @
34077304
...
@@ -73,3 +73,8 @@ files:
...
@@ -73,3 +73,8 @@ files:
history
:
history
:
postfix
:
-hist
postfix
:
-hist
filesize-max
:
52428800
filesize-max
:
52428800
minio
:
minio_url
:
http://192.168.1.216:9000
minio_name
:
fileadmin
minio_pass
:
fileadmin
bucketName
:
know
张京坤
@ZhangJingKun
被提及 commit
06c15bdb
2月 06, 2024
被提及 commit
06c15bdb
被提及 commit 06c15bdbc5969a745a75f8ba7225f67c3a24d78c
切换提交列表
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论