Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gk
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
micro_app
gk
Commits
64603599
Commit
64603599
authored
Jun 12, 2025
by
daniel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(oss): 新增oss上传接口
parent
5e934a5d
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
290 additions
and
11 deletions
+290
-11
pom.xml
pom.xml
+6
-0
OssConfiguration.java
...n/java/com/dlbridge/jadp/config/web/OssConfiguration.java
+26
-0
AliOssUtil.java
...java/com/dlbridge/jadp/plugins/cmpt/utils/AliOssUtil.java
+160
-0
CommonController.java
...va/com/dlbridge/jadp/plugins/common/CommonController.java
+67
-0
AliOssProperties.java
...idge/jadp/plugins/common/properties/AliOssProperties.java
+15
-0
application.properties
src/main/resources/application.properties
+7
-2
bootstrap.yml
src/main/resources/bootstrap.yml
+8
-8
swiper.jsp
src/main/webapp/WEB-INF/views/cmpt/swiper.jsp
+1
-1
No files found.
pom.xml
View file @
64603599
...
...
@@ -390,6 +390,12 @@
<artifactId>
rmi
</artifactId>
<version>
0.0.3
</version>
</dependency>
<!-- aliOss by ggb 2025-06-12 -->
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
<version>
3.10.2
</version>
</dependency>
</dependencies>
<repositories>
<repository>
...
...
src/main/java/com/dlbridge/jadp/config/web/OssConfiguration.java
0 → 100644
View file @
64603599
package
com
.
dlbridge
.
jadp
.
config
.
web
;
import
com.dlbridge.jadp.plugins.cmpt.utils.AliOssUtil
;
import
com.dlbridge.jadp.plugins.common.properties.AliOssProperties
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
@Slf4j
public
class
OssConfiguration
{
@Bean
@ConditionalOnMissingBean
public
AliOssUtil
aliOssUtil
(
AliOssProperties
aliOssProperties
){
log
.
info
(
"开始创建阿里云文件上传工具类对象{}"
,
aliOssProperties
);
return
new
AliOssUtil
(
aliOssProperties
.
getEndpoint
(),
aliOssProperties
.
getAccessKeyId
(),
aliOssProperties
.
getAccessKeySecret
(),
aliOssProperties
.
getBucketName
()
);
}
}
src/main/java/com/dlbridge/jadp/plugins/cmpt/utils/AliOssUtil.java
0 → 100644
View file @
64603599
package
com
.
dlbridge
.
jadp
.
plugins
.
cmpt
.
utils
;
import
com.aliyun.oss.ClientException
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.OSSException
;
import
com.aliyun.oss.model.GetObjectRequest
;
import
com.aliyun.oss.model.OSSObject
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
@Data
@AllArgsConstructor
@RequiredArgsConstructor
@Slf4j
public
class
AliOssUtil
{
private
String
endpoint
;
private
String
accessKeyId
;
private
String
accessKeySecret
;
private
String
bucketName
;
/**
* 文件上传
*
* @param bytes 文件
* @param objectName 文件名
* @return oss 的完整地址
*/
public
String
upload
(
byte
[]
bytes
,
String
objectName
)
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
try
{
// 创建PutObject请求。
ossClient
.
putObject
(
bucketName
,
objectName
,
new
ByteArrayInputStream
(
bytes
));
}
catch
(
OSSException
oe
)
{
System
.
out
.
println
(
"Caught an OSSException, which means your request made it to OSS, "
+
"but was rejected with an error response for some reason."
);
System
.
out
.
println
(
"Error Message:"
+
oe
.
getErrorMessage
());
System
.
out
.
println
(
"Error Code:"
+
oe
.
getErrorCode
());
System
.
out
.
println
(
"Request ID:"
+
oe
.
getRequestId
());
System
.
out
.
println
(
"Host ID:"
+
oe
.
getHostId
());
}
catch
(
ClientException
ce
)
{
System
.
out
.
println
(
"Caught an ClientException, which means the client encountered "
+
"a serious internal problem while trying to communicate with OSS, "
+
"such as not being able to access the network."
);
System
.
out
.
println
(
"Error Message:"
+
ce
.
getMessage
());
}
finally
{
if
(
ossClient
!=
null
)
{
ossClient
.
shutdown
();
}
}
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
return
"https://"
+
bucketName
+
"."
+
endpoint
+
"/"
+
objectName
;
}
/**
* 文件删除
*
* @param url 文件URL
*/
public
void
delete
(
String
url
)
{
String
objectName
=
extractObjectName
(
url
);
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
try
{
ossClient
.
deleteObject
(
bucketName
,
objectName
);
log
.
info
(
"文件删除成功: {}/{}"
,
bucketName
,
objectName
);
}
catch
(
OSSException
oe
)
{
System
.
out
.
println
(
"Caught an OSSException, which means your request made it to OSS, "
+
"but was rejected with an error response for some reason."
);
System
.
out
.
println
(
"Error Message:"
+
oe
.
getErrorMessage
());
System
.
out
.
println
(
"Error Code:"
+
oe
.
getErrorCode
());
System
.
out
.
println
(
"Request ID:"
+
oe
.
getRequestId
());
System
.
out
.
println
(
"Host ID:"
+
oe
.
getHostId
());
}
catch
(
ClientException
ce
)
{
System
.
out
.
println
(
"Caught an ClientException, which means the client encountered "
+
"a serious internal problem while trying to communicate with OSS, "
+
"such as not being able to access the network."
);
System
.
out
.
println
(
"Error Message:"
+
ce
.
getMessage
());
}
finally
{
if
(
ossClient
!=
null
)
{
ossClient
.
shutdown
();
}
}
}
/**
* 从URL中提取出objectName
*
* @param url 文件URL
* @return objectName
*/
private
String
extractObjectName
(
String
url
)
{
String
regex
=
"https://[^/]+/(.+)"
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
pattern
.
matcher
(
url
);
if
(
matcher
.
find
())
{
return
matcher
.
group
(
1
);
}
else
{
throw
new
IllegalArgumentException
(
"Invalid URL format: "
+
url
);
}
}
/**
* oss 文件下载
* @param url oss地址
* @param folder 文件夹
* @param zos zipOs
* @throws IOException 异常
*/
public
void
downloadAndZipImage
(
String
url
,
String
folder
,
ZipOutputStream
zos
)
throws
IOException
{
// 提取文件的 ObjectName
String
objectName
=
extractObjectName
(
url
);
// 创建 OSS 客户端并下载图片
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
try
{
// 获取文件输入流
OSSObject
ossObject
=
ossClient
.
getObject
(
new
GetObjectRequest
(
bucketName
,
objectName
));
InputStream
inputStream
=
ossObject
.
getObjectContent
();
if
(
inputStream
.
available
()
==
0
)
{
throw
new
IOException
(
"Downloaded image has no content."
);
}
// 将图片写入 ZIP 文件
ZipEntry
zipEntry
=
new
ZipEntry
(
folder
+
"/"
+
new
File
(
url
).
getName
());
zos
.
putNextEntry
(
zipEntry
);
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
inputStream
.
read
(
buffer
))
>
0
)
{
zos
.
write
(
buffer
,
0
,
length
);
}
zos
.
closeEntry
();
inputStream
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
// 关闭 OSS 客户端
ossClient
.
shutdown
();
}
}
}
src/main/java/com/dlbridge/jadp/plugins/common/CommonController.java
0 → 100644
View file @
64603599
package
com
.
dlbridge
.
jadp
.
plugins
.
common
;
import
com.dlbridge.jadp.plugins.cmpt.utils.AliOssUtil
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.neusoft.www.utils.web.Result
;
import
java.io.IOException
;
import
java.util.Objects
;
import
java.util.UUID
;
@RestController
@RequiredArgsConstructor
@RequestMapping
(
"/api-common/v1"
)
@Api
(
tags
=
"公共相关"
,
value
=
"分销相关接口"
)
@Slf4j
public
class
CommonController
{
final
private
AliOssUtil
aliOssUtil
;
@PostMapping
(
"/upload"
)
@ApiOperation
(
value
=
"文件上传"
)
public
Result
<?>
upload
(
@RequestPart
@RequestParam
(
"file"
)
MultipartFile
file
,
@RequestParam
(
value
=
"folder"
,
defaultValue
=
""
)
String
folder
,
@RequestParam
(
value
=
"cmptId"
)
String
cmptId
)
{
try
{
String
originalFilename
=
file
.
getOriginalFilename
();
assert
originalFilename
!=
null
;
// 检查文件名中是否包含点号
int
lastIndex
=
originalFilename
.
lastIndexOf
(
"."
);
if
(
lastIndex
==
-
1
)
{
// 如果没有后缀名,直接返回报错信息
return
Result
.
fail
().
setMsg
(
"上传的文件没有后缀名,请检查文件"
);
}
String
extension
=
originalFilename
.
substring
(
lastIndex
);
String
objectName
=
UUID
.
randomUUID
()
+
extension
;
if
(!
Objects
.
equals
(
folder
,
""
))
{
objectName
=
folder
+
"/"
+
cmptId
+
"/"
+
objectName
;
}
String
filePath
=
aliOssUtil
.
upload
(
file
.
getBytes
(),
objectName
);
log
.
info
(
"文件上传名: {} 保存的文件名:{}"
,
originalFilename
,
filePath
);
return
Result
.
success
().
setContent
(
filePath
);
}
catch
(
IOException
e
)
{
log
.
error
(
"文件上传失败:{}"
,
e
);
}
return
Result
.
fail
().
setMsg
(
"文件上传失败"
);
}
@PostMapping
(
"/deleteFile"
)
@ApiOperation
(
value
=
"文件删除"
)
public
Result
<?>
deleteFile
(
@RequestParam
String
url
){
try
{
aliOssUtil
.
delete
(
url
);
return
Result
.
success
();
}
catch
(
Exception
e
){
log
.
error
(
"文件删除失败:{}"
,
e
);
}
return
Result
.
fail
().
setMsg
(
"文件删除失败"
);
}
}
src/main/java/com/dlbridge/jadp/plugins/common/properties/AliOssProperties.java
0 → 100644
View file @
64603599
package
com
.
dlbridge
.
jadp
.
plugins
.
common
.
properties
;
import
lombok.Data
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
@Component
@ConfigurationProperties
(
prefix
=
"geexek.alioss"
)
@Data
public
class
AliOssProperties
{
private
String
endpoint
;
private
String
accessKeyId
;
private
String
accessKeySecret
;
private
String
bucketName
;
private
String
domain
;
}
src/main/resources/application.properties
View file @
64603599
spring.profiles.active
=
production
spring.profiles.active
=
dev
spring.session.store-type
=
REDIS
spring.session.timeout
=
PT30M
\ No newline at end of file
spring.session.timeout
=
PT30M
geexek.alioss.access-key-id
=
LTAI5t5datcjmyTp1hbd3T3k
geexek.alioss.access-key-secret
=
iN3T1eN5u35XCkJgrjzLqSKtY3NV81
geexek.alioss.endpoint
=
oss-cn-hangzhou.aliyuncs.com
geexek.alioss.bucket-name
=
geexekimgoss-sxk
geexek.alioss.bucket-domain
=
\ No newline at end of file
src/main/resources/bootstrap.yml
View file @
64603599
...
...
@@ -4,28 +4,28 @@ spring:
cloud
:
nacos
:
config
:
server-addr
:
http://
192.168.31.133
:8848
server-addr
:
http://
localhost
:8848
file-extension
:
properties
group
:
dev
namespace
:
187c2f4d-80c2-4a67-b4e5-319d5c9151d1
server-addr
:
http://
192.168.31.133
:8848
namespace
:
0c214852-53b7-44b0-b472-427013a29ebe
server-addr
:
http://
localhost
:8848
discovery
:
group
:
dev
namespace
:
187c2f4d-80c2-4a67-b4e5-319d5c9151d1
namespace
:
0c214852-53b7-44b0-b472-427013a29ebe
---
spring
:
profiles
:
dev
cloud
:
nacos
:
config
:
server-addr
:
http
s://nacos.geexek.com
server-addr
:
http
://localhost:8848
file-extension
:
properties
group
:
dev
namespace
:
187c2f4d-80c2-4a67-b4e5-319d5c9151d1
server-addr
:
http
s://nacos.geexek.com
namespace
:
0c214852-53b7-44b0-b472-427013a29ebe
server-addr
:
http
://localhost:8848
discovery
:
group
:
dev
namespace
:
187c2f4d-80c2-4a67-b4e5-319d5c9151d1
namespace
:
0c214852-53b7-44b0-b472-427013a29ebe
---
spring
:
profiles
:
test
...
...
src/main/webapp/WEB-INF/views/cmpt/swiper.jsp
View file @
64603599
...
...
@@ -319,7 +319,7 @@
</tr>
</tbody>
</table>
</div>
</div>
,
<div
class=
"dialog"
v-show=
"dialog"
style=
"width:auto;height: 80vh; z-index: 11;display: flex;flex-direction: column;left: 50%;
transform: translateX(-50%);margin-left:0;"
>
<div>
...
...
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