Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-oss
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-oss
Commits
8ea554f9
Commit
8ea554f9
authored
Oct 17, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
dedc1814
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
292 additions
and
4 deletions
+292
-4
yd-oss-api/src/main/java/com/yd/oss/api/controller/ApiOssController.java
+5
-0
yd-oss-api/src/main/java/com/yd/oss/api/service/ApiOssService.java
+2
-0
yd-oss-api/src/main/java/com/yd/oss/api/service/impl/ApiOssServiceImpl.java
+10
-0
yd-oss-feign/src/main/java/com/yd/oss/feign/client/ApiOssFeignClient.java
+7
-0
yd-oss-feign/src/main/java/com/yd/oss/feign/fallback/ApiOssFeignFallbackFactory.java
+5
-0
yd-oss-service/src/main/java/com/yd/oss/service/dao/ProductFileMapper.java
+16
-0
yd-oss-service/src/main/java/com/yd/oss/service/model/ProductFile.java
+99
-0
yd-oss-service/src/main/java/com/yd/oss/service/service/impl/FileUploadService.java
+144
-0
yd-oss-service/src/main/java/com/yd/oss/service/utils/MyBatisPlusCodeGenerator.java
+4
-4
No files found.
yd-oss-api/src/main/java/com/yd/oss/api/controller/ApiOssController.java
View file @
8ea554f9
...
...
@@ -96,4 +96,9 @@ public class ApiOssController implements ApiOssFeignClient {
public
Result
<
ApiFileMetadataResponse
>
getFileMetadata
(
String
fileKey
)
{
return
apiOssService
.
getFileMetadata
(
fileKey
,
""
,
""
);
}
@Override
public
Result
uploadBatchFile
()
{
return
apiOssService
.
uploadBatchFile
();
}
}
yd-oss-api/src/main/java/com/yd/oss/api/service/ApiOssService.java
View file @
8ea554f9
...
...
@@ -21,4 +21,6 @@ public interface ApiOssService {
Result
<
Boolean
>
doesFileExist
(
String
fileKey
,
String
bucket
,
String
provider
);
Result
<
ApiFileMetadataResponse
>
getFileMetadata
(
String
fileKey
,
String
bucket
,
String
provider
);
Result
uploadBatchFile
();
}
yd-oss-api/src/main/java/com/yd/oss/api/service/impl/ApiOssServiceImpl.java
View file @
8ea554f9
...
...
@@ -9,6 +9,7 @@ import com.yd.oss.service.dto.FileMetadata;
import
com.yd.oss.service.dto.OssUploadFileDto
;
import
com.yd.oss.service.dto.UploadResult
;
import
com.yd.oss.service.service.OssService
;
import
com.yd.oss.service.service.impl.FileUploadService
;
import
com.yd.oss.service.utils.FileUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.io.IOUtils
;
...
...
@@ -28,6 +29,9 @@ public class ApiOssServiceImpl implements ApiOssService {
@Autowired
private
OssService
ossService
;
@Autowired
private
FileUploadService
fileUploadService
;
/**
* 上传文件
* @param file 上传的文件
...
...
@@ -213,4 +217,10 @@ public class ApiOssServiceImpl implements ApiOssService {
return
Result
.
fail
(
"获取文件元数据失败: "
+
e
.
getMessage
());
}
}
@Override
public
Result
uploadBatchFile
()
{
fileUploadService
.
batchProcessUrls
();
return
Result
.
success
();
}
}
yd-oss-feign/src/main/java/com/yd/oss/feign/client/ApiOssFeignClient.java
View file @
8ea554f9
...
...
@@ -74,4 +74,11 @@ public interface ApiOssFeignClient {
*/
@GetMapping
(
"/metadata"
)
Result
<
ApiFileMetadataResponse
>
getFileMetadata
(
@RequestParam
(
"fileKey"
)
String
fileKey
);
/**
* 批量上传文件
* @return
*/
@GetMapping
(
"/upload/batch/file"
)
Result
uploadBatchFile
();
}
yd-oss-feign/src/main/java/com/yd/oss/feign/fallback/ApiOssFeignFallbackFactory.java
View file @
8ea554f9
...
...
@@ -52,6 +52,11 @@ public class ApiOssFeignFallbackFactory implements FallbackFactory<ApiOssFeignCl
public
Result
<
ApiFileMetadataResponse
>
getFileMetadata
(
String
fileKey
)
{
return
null
;
}
@Override
public
Result
uploadBatchFile
()
{
return
null
;
}
};
}
}
yd-oss-service/src/main/java/com/yd/oss/service/dao/ProductFileMapper.java
0 → 100644
View file @
8ea554f9
package
com
.
yd
.
oss
.
service
.
dao
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.yd.oss.service.model.ProductFile
;
/**
* <p>
* 产品文件关系表 Mapper 接口
* </p>
*
* @author zxm
* @since 2025-10-17
*/
public
interface
ProductFileMapper
extends
BaseMapper
<
ProductFile
>
{
}
yd-oss-service/src/main/java/com/yd/oss/service/model/ProductFile.java
0 → 100644
View file @
8ea554f9
package
com
.
yd
.
oss
.
service
.
model
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
lombok.Getter
;
import
lombok.Setter
;
import
java.io.Serializable
;
import
java.time.LocalDateTime
;
/**
* <p>
* 产品文件关系表
* </p>
*
* @author zxm
* @since 2025-10-17
*/
@Getter
@Setter
@TableName
(
"product_file"
)
public
class
ProductFile
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 主键ID
*/
@TableId
(
value
=
"id"
,
type
=
IdType
.
AUTO
)
private
Long
id
;
/**
* 未知ID
*/
@TableField
(
"biz_id"
)
private
String
bizId
;
/**
* 产品业务ID
*/
@TableField
(
"product_biz_id"
)
private
String
productBizId
;
/**
* 文件名称
*/
@TableField
(
"file_name"
)
private
String
fileName
;
/**
* 原文件URL
*/
@TableField
(
"old_file_url"
)
private
String
oldFileUrl
;
/**
* 新文件URL
*/
@TableField
(
"new_file_url"
)
private
String
newFileUrl
;
/**
* 通用备注
*/
@TableField
(
"remark"
)
private
String
remark
;
/**
* 删除标识: 0-正常, 1-删除
*/
@TableField
(
"is_deleted"
)
private
Boolean
isDeleted
;
/**
* 创建人ID
*/
@TableField
(
"creator_id"
)
private
String
creatorId
;
/**
* 更新人ID
*/
@TableField
(
"updater_id"
)
private
String
updaterId
;
/**
* 创建时间
*/
@TableField
(
"create_time"
)
private
LocalDateTime
createTime
;
/**
* 更新时间
*/
@TableField
(
"update_time"
)
private
LocalDateTime
updateTime
;
}
yd-oss-service/src/main/java/com/yd/oss/service/service/impl/FileUploadService.java
0 → 100644
View file @
8ea554f9
package
com
.
yd
.
oss
.
service
.
service
.
impl
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.model.PutObjectRequest
;
import
com.yd.oss.service.dao.ProductFileMapper
;
import
com.yd.oss.service.model.ProductFile
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.compress.utils.IOUtils
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.io.ByteArrayInputStream
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.util.List
;
import
java.util.UUID
;
@Service
@Slf4j
public
class
FileUploadService
{
@Autowired
private
OSS
ossClient
;
@Autowired
private
ProductFileMapper
productFileMapper
;
@Autowired
private
String
defaultBucket
;
// 从配置注入默认存储桶
@Autowired
private
String
defaultEndpoint
;
// 注入默认服务端点
/**
* 批量处理URL上传
*/
public
void
batchProcessUrls
()
{
List
<
ProductFile
>
productFileList
=
productFileMapper
.
selectList
(
null
);
for
(
ProductFile
productFile
:
productFileList
)
{
try
{
// 处理单个URL
processSingleUrl
(
productFile
);
}
catch
(
Exception
e
)
{
log
.
error
(
"处理URL失败: {}, 错误: {}"
,
productFile
.
getOldFileUrl
(),
e
.
getMessage
());
}
}
}
/**
* 处理单个URL
*/
private
void
processSingleUrl
(
ProductFile
productFile
)
throws
Exception
{
// 1. 下载文件
byte
[]
fileContent
=
downloadFile
(
productFile
.
getOldFileUrl
());
// 2. 生成OSS文件名(使用原始URL路径)
String
ossFileName
=
generateOssFileName
(
productFile
.
getOldFileUrl
(),
""
);
// 3. 上传到阿里云OSS
String
ossUrl
=
uploadToAliOss
(
fileContent
,
ossFileName
);
// 4. 更新数据库
productFile
.
setNewFileUrl
(
ossUrl
);
productFileMapper
.
updateById
(
productFile
);
log
.
info
(
"文件上传成功: {} -> {}"
,
productFile
.
getFileName
(),
ossUrl
);
}
/**
* 下载文件
*/
private
byte
[]
downloadFile
(
String
fileUrl
)
throws
Exception
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
HttpGet
httpGet
=
new
HttpGet
(
fileUrl
);
try
(
CloseableHttpResponse
response
=
httpClient
.
execute
(
httpGet
);
InputStream
inputStream
=
response
.
getEntity
().
getContent
())
{
return
IOUtils
.
toByteArray
(
inputStream
);
}
finally
{
httpClient
.
close
();
}
}
/**
* 生成OSS文件名 - 保留原始URL路径结构
*/
private
String
generateOssFileName
(
String
originalUrl
,
String
originalFileName
)
{
try
{
// 解析原始URL,提取路径部分
URL
url
=
new
URL
(
originalUrl
);
String
path
=
url
.
getPath
();
// 得到类似:/wslucky/product/2024/12/27/9ba093ee-be9a-4291-aa86-d6a1995e16f9.pdf
// 移除开头的斜杠(如果有)
if
(
path
.
startsWith
(
"/"
))
{
path
=
path
.
substring
(
1
);
}
// 如果路径为空,使用备选方案
if
(
path
.
isEmpty
())
{
return
generateFallbackFileName
(
originalFileName
);
}
return
path
;
}
catch
(
Exception
e
)
{
log
.
warn
(
"解析URL路径失败: {}, 使用备选方案"
,
originalUrl
,
e
);
return
generateFallbackFileName
(
originalFileName
);
}
}
/**
* 备选文件名生成方案
*/
private
String
generateFallbackFileName
(
String
originalFileName
)
{
String
fileExtension
=
originalFileName
.
substring
(
originalFileName
.
lastIndexOf
(
"."
));
String
timestamp
=
String
.
valueOf
(
System
.
currentTimeMillis
());
String
randomStr
=
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
).
substring
(
0
,
8
);
return
"insurance/"
+
timestamp
+
"_"
+
randomStr
+
fileExtension
;
}
/**
* 上传到阿里云OSS
*/
private
String
uploadToAliOss
(
byte
[]
fileContent
,
String
ossFileName
)
{
// 创建上传请求
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
defaultBucket
,
ossFileName
,
new
ByteArrayInputStream
(
fileContent
));
// 上传文件
ossClient
.
putObject
(
putObjectRequest
);
// 生成访问URL
return
String
.
format
(
"https://%s.%s/%s"
,
defaultBucket
,
defaultEndpoint
,
ossFileName
);
}
}
\ No newline at end of file
yd-oss-service/src/main/java/com/yd/oss/service/utils/MyBatisPlusCodeGenerator.java
View file @
8ea554f9
...
...
@@ -5,11 +5,11 @@ import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
public
class
MyBatisPlusCodeGenerator
{
public
static
void
main
(
String
[]
args
)
{
FastAutoGenerator
.
create
(
"jdbc:mysql://localhost:3306/
yd
?serverTimezone=GMT%2B8"
,
"root"
,
"123456"
)
FastAutoGenerator
.
create
(
"jdbc:mysql://localhost:3306/
test
?serverTimezone=GMT%2B8"
,
"root"
,
"123456"
)
.
globalConfig
(
builder
->
{
builder
.
author
(
"zxm"
)
// .outputDir("src/main/java/com/yd/user
/service");
.
outputDir
(
"D:/soft/ideaproject/v2/yd-oss/yd-oss-service/src/main/java"
);
.
outputDir
(
"src/main/java/com/yd/oss
/service"
);
//
.outputDir("D:/soft/ideaproject/v2/yd-oss/yd-oss-service/src/main/java");
})
.
packageConfig
(
builder
->
{
builder
.
parent
(
"com.yd.oss.service"
)
...
...
@@ -21,7 +21,7 @@ public class MyBatisPlusCodeGenerator {
})
.
strategyConfig
(
builder
->
{
builder
.
addInclude
(
"
file_templat
e"
"
product_fil
e"
)
.
entityBuilder
()
.
enableLombok
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment