Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
K
know-base
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
张京坤
know-base
Commits
c1139d3a
提交
c1139d3a
authored
1月 03, 2024
作者:
ChenShiQiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
disable obs
上级
45522ebd
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
202 行增加
和
202 行删除
+202
-202
ObsUtil.java
src/main/java/com/zzsn/knowbase/util/ObsUtil.java
+193
-193
ReportUtil.java
src/main/java/com/zzsn/knowbase/util/ReportUtil.java
+9
-9
没有找到文件。
src/main/java/com/zzsn/knowbase/util/ObsUtil.java
浏览文件 @
c1139d3a
package
com
.
zzsn
.
knowbase
.
util
;
//
package com.zzsn.knowbase.util;
//
//
import
com.github.tobato.fastdfs.proto.storage.DownloadByteArray
;
//
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import
com.obs.services.ObsClient
;
//
import com.obs.services.ObsClient;
import
com.obs.services.model.*
;
//
import com.obs.services.model.*;
//import org.apache.commons.lang.StringUtils;
//
//
import org.apache.commons.lang.StringUtils;
import
org.apache.commons.lang3.StringUtils
;
//
import org.apache.commons.lang3.StringUtils;
import
org.springframework.beans.factory.annotation.Autowired
;
//
import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component
;
//
import org.springframework.stereotype.Component;
//
import
javax.servlet.http.HttpServletRequest
;
//
import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse
;
//
import javax.servlet.http.HttpServletResponse;
import
java.io.*
;
//
import java.io.*;
import
java.util.ArrayList
;
//
import java.util.ArrayList;
import
java.util.List
;
//
import java.util.List;
//
/**
/
//
**
* Description: obs桶文件操作
//
* Description: obs桶文件操作
* Author: EDY
//
* Author: EDY
* Date: 2023/10/9
//
* Date: 2023/10/9
*/
//
*/
@Component
//
@Component
public
class
ObsUtil
{
//
public class ObsUtil {
//
@Autowired
//
@Autowired
ObsClient
obsClient
;
//
ObsClient obsClient;
/**桶名称*/
//
/**桶名称*/
private
String
bucketName
=
"zzsn"
;
//
private String bucketName = "zzsn";
//
/**判断桶是否存在*/
//
/**判断桶是否存在*/
public
Boolean
existsBucket
(
String
bucket
){
//
public Boolean existsBucket(String bucket){
return
obsClient
.
headBucket
(
bucket
);
//
return obsClient.headBucket(bucket);
}
//
}
public
Boolean
existsBucket
(){
//
public Boolean existsBucket(){
return
obsClient
.
headBucket
(
bucketName
);
//
return obsClient.headBucket(bucketName);
}
//
}
//
/**
//
/**
* 创建文件夹本质上来说是创建了一个大小为0且对象名以“/”结尾的对象。
//
* 创建文件夹本质上来说是创建了一个大小为0且对象名以“/”结尾的对象。
* 多级文件夹创建最后一级即可,比如src1/src2/src3/,创建src1/src2/src3/即可,无需创建src1/、src1/src2/。
//
* 多级文件夹创建最后一级即可,比如src1/src2/src3/,创建src1/src2/src3/即可,无需创建src1/、src1/src2/。
* keySuffixWithSlash为文件夹名称,以 / 结尾
//
* keySuffixWithSlash为文件夹名称,以 / 结尾
* */
//
* */
public
boolean
mkdir
(
String
keySuffixWithSlash
){
//
public boolean mkdir(String keySuffixWithSlash){
PutObjectResult
putObjectResult
=
obsClient
.
putObject
(
bucketName
,
keySuffixWithSlash
,
new
ByteArrayInputStream
(
new
byte
[
0
]));
//
PutObjectResult putObjectResult = obsClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
if
(
putObjectResult
.
getStatusCode
()==
200
)
{
//
if (putObjectResult.getStatusCode()==200) {
return
true
;
//
return true;
}
else
{
//
}else {
return
false
;
//
return false;
}
//
}
}
//
}
//
/**查询桶内文件夹下所有文件
//
/**查询桶内文件夹下所有文件
* folderPrefix 为文件夹名称,以 / 结尾
//
* folderPrefix 为文件夹名称,以 / 结尾
* */
//
* */
public
List
<
ObsObject
>
getPathFileList
(
String
folderPrefix
){
//
public List<ObsObject> getPathFileList(String folderPrefix){
List
<
ObsObject
>
res
=
new
ArrayList
<>();
//
List<ObsObject> res = new ArrayList<>();
ListObjectsRequest
request
=
new
ListObjectsRequest
(
bucketName
);
//
ListObjectsRequest request = new ListObjectsRequest(bucketName);
request
.
setPrefix
(
folderPrefix
);
//
request.setPrefix(folderPrefix);
ObjectListing
result
=
obsClient
.
listObjects
(
request
);
//
ObjectListing result = obsClient.listObjects(request);
for
(
ObsObject
obsObject
:
result
.
getObjects
())
{
//
for (ObsObject obsObject : result.getObjects()) {
res
.
add
(
obsObject
);
//
res.add(obsObject);
}
//
}
return
res
;
//
return res;
}
//
}
/**
//
/**
* 获取文件夹下的文件数量
//
* 获取文件夹下的文件数量
* */
//
* */
public
Integer
getCount
(
String
folderPrefix
){
//
public Integer getCount (String folderPrefix){
ListObjectsRequest
listObjectsRequest
=
new
ListObjectsRequest
(
bucketName
);
//
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
listObjectsRequest
.
setPrefix
(
folderPrefix
);
//
listObjectsRequest.setPrefix(folderPrefix);
listObjectsRequest
.
setMaxKeys
(
1000
);
//
listObjectsRequest.setMaxKeys(1000);
int
fileCount
=
0
;
//
int fileCount = 0;
//
ObjectListing
objectListing
;
//
ObjectListing objectListing;
//
do
{
//
do {
objectListing
=
obsClient
.
listObjects
(
listObjectsRequest
);
//
objectListing = obsClient.listObjects(listObjectsRequest);
//
List
<
S3Object
>
objectSummaries
=
objectListing
.
getObjectSummaries
();
//
List<S3Object> objectSummaries = objectListing.getObjectSummaries();
fileCount
+=
objectSummaries
.
size
();
//
fileCount += objectSummaries.size();
//
String
nextMarker
=
objectListing
.
getNextMarker
();
//
String nextMarker = objectListing.getNextMarker();
listObjectsRequest
.
setMarker
(
nextMarker
);
//
listObjectsRequest.setMarker(nextMarker);
}
while
(
objectListing
.
isTruncated
());
//
} while (objectListing.isTruncated());
//
return
fileCount
;
//
return fileCount;
}
//
}
//
/**删除桶内文件
//
/**删除桶内文件
* objectKey为文件路径,起始为桶内某文件夹,或者直接为桶内文件
//
* objectKey为文件路径,起始为桶内某文件夹,或者直接为桶内文件
* */
//
* */
public
boolean
delFile
(
String
objectKey
){
//
public boolean delFile (String objectKey){
DeleteObjectResult
deleteObjectResult
=
obsClient
.
deleteObject
(
bucketName
,
objectKey
);
//
DeleteObjectResult deleteObjectResult = obsClient.deleteObject(bucketName, objectKey);
if
(
deleteObjectResult
.
getStatusCode
()==
200
)
{
//
if (deleteObjectResult.getStatusCode()==200) {
return
true
;
//
return true;
}
else
{
//
}else {
return
false
;
//
return false;
}
//
}
}
//
}
//
/**文件上传
//
/**文件上传
* objectKey为文件路径
//
* objectKey为文件路径
* */
//
* */
public
PutObjectResult
uploadFile
(
String
objectKey
,
byte
[]
bytes
){
//
public PutObjectResult uploadFile(String objectKey,byte[] bytes){
PutObjectResult
putObjectResult
=
obsClient
.
putObject
(
bucketName
,
objectKey
,
new
ByteArrayInputStream
(
bytes
));
//
PutObjectResult putObjectResult = obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(bytes));
return
putObjectResult
;
//
return putObjectResult;
}
//
}
/**文件上传
//
/**文件上传
* objectKey为文件路径
//
* objectKey为文件路径
* */
//
* */
public
PutObjectResult
uploadFile
(
String
objectKey
,
InputStream
inputStream
){
//
public PutObjectResult uploadFile(String objectKey ,InputStream inputStream){
PutObjectResult
putObjectResult
=
obsClient
.
putObject
(
bucketName
,
objectKey
,
inputStream
);
//
PutObjectResult putObjectResult = obsClient.putObject(bucketName, objectKey, inputStream);
return
putObjectResult
;
//
return putObjectResult;
}
//
}
/**
//
/**
* 获取文件流
//
* 获取文件流
*
//
*
* */
//
* */
public
InputStream
getObjectStream
(
String
objectKey
){
//
public InputStream getObjectStream(String objectKey){
ObsObject
obsObject
=
obsClient
.
getObject
(
bucketName
,
objectKey
);
//
ObsObject obsObject = obsClient.getObject(bucketName, objectKey);
return
obsObject
.
getObjectContent
();
//
return obsObject.getObjectContent();
}
//
}
/**
//
/**
* 获取文件流
//
* 获取文件流
*
//
*
* */
//
* */
public
byte
[]
getObjectByte
(
String
objectKey
){
//
public byte[] getObjectByte(String objectKey){
ObsObject
obsObject
=
obsClient
.
getObject
(
bucketName
,
objectKey
);
//
ObsObject obsObject = obsClient.getObject(bucketName, objectKey);
// 获取文件的输入流
//
// 获取文件的输入流
InputStream
objectContent
=
obsObject
.
getObjectContent
();
//
InputStream objectContent = obsObject.getObjectContent();
//
// 将输入流转换为byte[]
//
// 将输入流转换为byte[]
ByteArrayOutputStream
byteArrayOutputStream
=
new
ByteArrayOutputStream
();
//
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte
[]
buffer
=
new
byte
[
4096
];
//
byte[] buffer = new byte[4096];
int
bytesRead
;
//
int bytesRead;
while
(
true
)
{
//
while (true) {
try
{
//
try {
if
(!((
bytesRead
=
objectContent
.
read
(
buffer
))
!=
-
1
))
{
//
if (!((bytesRead = objectContent.read(buffer)) != -1)) {
break
;
//
break;
}
//
}
}
catch
(
IOException
e
)
{
//
} catch (IOException e) {
throw
new
RuntimeException
(
e
);
//
throw new RuntimeException(e);
}
//
}
byteArrayOutputStream
.
write
(
buffer
,
0
,
bytesRead
);
//
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
//
}
byte
[]
fileBytes
=
byteArrayOutputStream
.
toByteArray
();
//
byte[] fileBytes = byteArrayOutputStream.toByteArray();
return
fileBytes
;
//
return fileBytes;
}
//
}
//
/*
//
/*
* 文件预览
//
* 文件预览
* @param fileName
//
* @param fileName
*/
//
*/
public
boolean
previewImg
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
//
public boolean previewImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
String
filePath
=
request
.
getParameter
(
"attachmentPath"
);
//
String filePath = request.getParameter("attachmentPath");
String
group
=
request
.
getParameter
(
"group"
);
//
String group = request.getParameter("group");
if
(
StringUtils
.
isBlank
(
filePath
))
{
//
if (StringUtils.isBlank(filePath)) {
return
false
;
//
return false;
}
//
}
DownloadByteArray
downloadByteArray
=
new
DownloadByteArray
();
//
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte
[]
content
=
getObjectByte
(
filePath
);
//
byte[] content = getObjectByte(filePath);
if
(
content
==
null
||
content
.
length
==
0
)
{
//
if (content == null || content.length == 0) {
return
false
;
//
return false;
}
//
}
//
response
.
addHeader
(
"Pragma"
,
"No-cache"
);
//
response.addHeader("Pragma", "No-cache");
response
.
addHeader
(
"Cache-Control"
,
"no-store,No-cache"
);
//
response.addHeader("Cache-Control", "no-store,No-cache");
response
.
setCharacterEncoding
(
"UTF-8"
);
//
response.setCharacterEncoding("UTF-8");
// response.setContentType("application/json;charset=utf-8");
//
//
response.setContentType("application/json;charset=utf-8");
String
s
=
filePath
.
split
(
"/"
)[
filePath
.
split
(
"/"
).
length
-
1
];
//
String s = filePath.split("/")[filePath.split("/").length - 1];
String
mimeType
=
request
.
getServletContext
().
getMimeType
(
s
);
//
String mimeType = request.getServletContext().getMimeType(s);
System
.
out
.
println
(
"文件类型为"
+
mimeType
);
//
System.out.println("文件类型为" + mimeType);
response
.
setContentType
(
request
.
getServletContext
().
getMimeType
(
s
)
+
";charset=utf-8"
);
//
response.setContentType(request.getServletContext().getMimeType(s) + ";charset=utf-8");
OutputStream
out
=
response
.
getOutputStream
();
//
OutputStream out = response.getOutputStream();
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
out
);
//
BufferedOutputStream bos = new BufferedOutputStream(out);
try
{
//
try {
bos
.
write
(
content
,
0
,
content
.
length
);
//
bos.write(content, 0, content.length);
bos
.
flush
();
//
bos.flush();
}
catch
(
Exception
e
)
{
//
} catch (Exception e) {
e
.
printStackTrace
();
//
e.printStackTrace();
}
finally
{
//
} finally {
if
(
bos
!=
null
)
{
//
if (bos != null) {
bos
.
close
();
//
bos.close();
}
//
}
if
(
out
!=
null
)
{
//
if (out != null) {
out
.
close
();
//
out.close();
}
//
}
}
//
}
return
true
;
//
return true;
}
//
}
//
}
//
}
src/main/java/com/zzsn/knowbase/util/ReportUtil.java
浏览文件 @
c1139d3a
...
@@ -37,8 +37,8 @@ import java.util.regex.Pattern;
...
@@ -37,8 +37,8 @@ import java.util.regex.Pattern;
*/
*/
@Slf4j
@Slf4j
public
class
ReportUtil
{
public
class
ReportUtil
{
@Autowired
//
@Autowired
private
static
ObsUtil
obsUtil
;
//
private static ObsUtil obsUtil;
// @Resource
// @Resource
// private static StreamBridge streamBridge;
// private static StreamBridge streamBridge;
...
@@ -753,13 +753,13 @@ public class ReportUtil {
...
@@ -753,13 +753,13 @@ public class ReportUtil {
public
static
void
formatFile
(
AiReportScienceFile
reportTemplate
,
String
extension
,
MultipartFile
file
)
throws
Exception
{
public
static
void
formatFile
(
AiReportScienceFile
reportTemplate
,
String
extension
,
MultipartFile
file
)
throws
Exception
{
//TODO 文件上传
//
//TODO 文件上传
obsUtil
=
GetBeanUtil
.
getApplicationContext
().
getBean
(
ObsUtil
.
class
);
//
obsUtil = GetBeanUtil.getApplicationContext().getBean(ObsUtil.class);
//文件路径
//
//文件路径
byte
[]
bytes
=
file
.
getBytes
();
//
byte[] bytes = file.getBytes();
PutObjectResult
putObjectResult
=
obsUtil
.
uploadFile
(
DirEnum
.
SCIENCE_FILE
.
getPath
()
+
UUID
.
randomUUID
()
+
"."
+
extension
,
bytes
);
//
PutObjectResult putObjectResult = obsUtil.uploadFile(DirEnum.SCIENCE_FILE.getPath() + UUID.randomUUID() + "." + extension, bytes);
reportTemplate
.
setFilePathObs
(
Constants
.
OBS_FILE_PATH_URL_PREFIX_NOS
+
putObjectResult
.
getObjectKey
());
//
reportTemplate.setFilePathObs(Constants.OBS_FILE_PATH_URL_PREFIX_NOS + putObjectResult.getObjectKey());
//
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论