Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
szpt
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
以墨为白
szpt
Commits
6a416f8a
Commit
6a416f8a
authored
Dec 29, 2024
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片上传-bug优化
parent
f4dd47a7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
4 deletions
+20
-4
src/main/java/com/zksy/szpt/service/ImageUploadService.java
src/main/java/com/zksy/szpt/service/ImageUploadService.java
+19
-3
src/test/java/com/zksy/szpt/MainTest.java
src/test/java/com/zksy/szpt/MainTest.java
+1
-1
No files found.
src/main/java/com/zksy/szpt/service/ImageUploadService.java
View file @
6a416f8a
...
...
@@ -4,17 +4,32 @@ import com.zksy.szpt.domain.UploadImageDTO;
import
com.zksy.szpt.domain.po.XxImgfile
;
import
com.zksy.szpt.exception.NotificationException
;
import
com.zksy.szpt.mapper.XxImgfileMapper
;
import
io.netty.channel.ChannelOption
;
import
io.netty.handler.timeout.ReadTimeoutHandler
;
import
io.netty.handler.timeout.WriteTimeoutHandler
;
import
org.springframework.http.client.reactive.ReactorClientHttpConnector
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.reactive.function.client.WebClient
;
import
org.springframework.web.reactive.function.client.WebClientResponseException
;
import
reactor.core.publisher.Mono
;
import
reactor.netty.http.client.HttpClient
;
import
reactor.netty.tcp.TcpClient
;
import
java.net.URISyntaxException
;
@Service
public
class
ImageUploadService
{
//日志
private
static
final
org
.
slf4j
.
Logger
log
=
org
.
slf4j
.
LoggerFactory
.
getLogger
(
ImageUploadService
.
class
);
private
final
XxImgfileMapper
xxImgfileMapper
;
// 创建一个 TcpClient 并设置超时时间
final
TcpClient
tcpClient
=
TcpClient
.
create
()
.
option
(
ChannelOption
.
CONNECT_TIMEOUT_MILLIS
,
5000
)
// 连接超时时间
.
doOnConnected
(
connection
->
connection
.
addHandlerLast
(
new
ReadTimeoutHandler
(
5
))
// 读取超时时间
.
addHandlerLast
(
new
WriteTimeoutHandler
(
5
)));
// 写入超时时间
public
ImageUploadService
(
XxImgfileMapper
xxImgfileMapper
)
{
this
.
xxImgfileMapper
=
xxImgfileMapper
;
}
...
...
@@ -22,6 +37,7 @@ public class ImageUploadService {
public
Integer
upload
(
UploadImageDTO
uploadImageDTO
)
throws
NotificationException
{
// 创建 WebClient 实例用于下载文件
WebClient
webClient
=
WebClient
.
builder
()
.
clientConnector
(
new
ReactorClientHttpConnector
(
HttpClient
.
from
(
tcpClient
)))
.
baseUrl
(
uploadImageDTO
.
getImagePath
())
.
build
();
// 下载文件到内存中的字节数组
...
...
@@ -30,15 +46,15 @@ public class ImageUploadService {
.
retrieve
()
.
bodyToMono
(
byte
[].
class
)
.
onErrorResume
(
WebClientResponseException
.
class
,
ex
->
{
System
.
err
.
println
(
"WebClientResponseException: "
+
ex
.
getResponseBodyAsString
());
log
.
warn
(
"WebClientResponseException: {}"
,
ex
.
getResponseBodyAsString
());
return
Mono
.
error
(
new
NotificationException
(
uploadImageDTO
.
getImagePath
()
+
",Failed to download file: "
+
ex
.
getStatusText
()));
})
.
onErrorResume
(
URISyntaxException
.
class
,
ex
->
{
System
.
err
.
println
(
"URISyntaxException: "
+
ex
.
getMessage
());
log
.
warn
(
"URISyntaxException: {}"
,
ex
.
getMessage
());
return
Mono
.
error
(
new
NotificationException
(
uploadImageDTO
.
getImagePath
()
+
",Invalid URL format"
));
})
.
onErrorResume
(
Exception
.
class
,
ex
->
{
System
.
err
.
println
(
"General Exception: "
+
ex
.
getMessage
());
log
.
warn
(
"General Exception: {}"
,
ex
.
getMessage
());
return
Mono
.
error
(
new
NotificationException
(
ex
.
getMessage
()));
});
...
...
src/test/java/com/zksy/szpt/MainTest.java
View file @
6a416f8a
...
...
@@ -94,7 +94,7 @@ public class MainTest {
Assertions
.
assertNotNull
(
secretKey
,
"appId不存在"
);
//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
UploadImageDTO
uploadImageDTO
=
new
UploadImageDTO
();
uploadImageDTO
.
setImagePath
(
""
);
uploadImageDTO
.
setImagePath
(
"
http://192.168.168.219/guoqing1.jpg
"
);
uploadImageDTO
.
setImageType
(
ListImageType
.
FACE
);
String
json
=
null
;
try
{
...
...
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