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
8bc94872
Commit
8bc94872
authored
Dec 30, 2024
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片上传-bug优化
parent
8a2e1b61
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
7 deletions
+54
-7
src/main/java/com/zksy/szpt/domain/po/XxImgindex.java
src/main/java/com/zksy/szpt/domain/po/XxImgindex.java
+6
-0
src/main/java/com/zksy/szpt/mapper/XxImgindexMapper.java
src/main/java/com/zksy/szpt/mapper/XxImgindexMapper.java
+4
-1
src/main/java/com/zksy/szpt/service/ImageUploadService.java
src/main/java/com/zksy/szpt/service/ImageUploadService.java
+44
-6
No files found.
src/main/java/com/zksy/szpt/domain/po/XxImgindex.java
View file @
8bc94872
package
com
.
zksy
.
szpt
.
domain
.
po
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
java.util.Date
;
@TableName
public
class
XxImgindex
{
/**
* 图片IID
...
...
@@ -11,6 +16,7 @@ public class XxImgindex {
/**
* 图片ID
*/
@TableId
(
type
=
IdType
.
ASSIGN_ID
)
private
String
id
;
/**
...
...
src/main/java/com/zksy/szpt/mapper/XxImgindexMapper.java
View file @
8bc94872
package
com
.
zksy
.
szpt
.
mapper
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.zksy.szpt.domain.po.XxImgindex
;
import
com.zksy.szpt.domain.po.XxImgindexExample
;
import
java.util.List
;
import
org.apache.ibatis.annotations.Param
;
public
interface
XxImgindexMapper
{
public
interface
XxImgindexMapper
extends
BaseMapper
<
XxImgindex
>
{
long
countByExample
(
XxImgindexExample
example
);
int
deleteByExample
(
XxImgindexExample
example
);
...
...
src/main/java/com/zksy/szpt/service/ImageUploadService.java
View file @
8bc94872
...
...
@@ -2,8 +2,11 @@ package com.zksy.szpt.service;
import
com.zksy.szpt.domain.UploadImageDTO
;
import
com.zksy.szpt.domain.po.XxImgfile
;
import
com.zksy.szpt.domain.po.XxImgindex
;
import
com.zksy.szpt.domain.po.XxImgindexExample
;
import
com.zksy.szpt.exception.NotificationException
;
import
com.zksy.szpt.mapper.XxImgfileMapper
;
import
com.zksy.szpt.mapper.XxImgindexMapper
;
import
io.netty.channel.ChannelOption
;
import
io.netty.handler.timeout.ReadTimeoutHandler
;
import
io.netty.handler.timeout.WriteTimeoutHandler
;
...
...
@@ -16,6 +19,8 @@ import reactor.netty.http.client.HttpClient;
import
reactor.netty.tcp.TcpClient
;
import
java.net.URISyntaxException
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
@Service
public
class
ImageUploadService
{
...
...
@@ -24,17 +29,20 @@ public class ImageUploadService {
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
)));
// 写入超时时间
private
final
XxImgindexMapper
xxImgindexMapper
;
public
ImageUploadService
(
XxImgfileMapper
xxImgfileMapper
)
{
public
ImageUploadService
(
XxImgfileMapper
xxImgfileMapper
,
XxImgindexMapper
xxImgindexMapper
)
{
this
.
xxImgfileMapper
=
xxImgfileMapper
;
this
.
xxImgindexMapper
=
xxImgindexMapper
;
}
public
Integer
upload
(
UploadImageDTO
uploadImageDTO
)
throws
NotificationException
{
// 创建一个 TcpClient 并设置超时时间
final
TcpClient
tcpClient
=
TcpClient
.
create
()
.
option
(
ChannelOption
.
CONNECT_TIMEOUT_MILLIS
,
5000
)
// 连接超时时间
.
doOnConnected
(
connection
->
connection
.
addHandlerLast
(
new
ReadTimeoutHandler
(
5
))
// 读取超时时间
.
addHandlerLast
(
new
WriteTimeoutHandler
(
5
)));
// 写入超时时间
// 创建 WebClient 实例用于下载文件
WebClient
webClient
=
WebClient
.
builder
()
.
clientConnector
(
new
ReactorClientHttpConnector
(
HttpClient
.
from
(
tcpClient
)))
...
...
@@ -68,6 +76,16 @@ public class ImageUploadService {
throw
new
NotificationException
(
"Failed to download file"
);
}
xxImgfile
.
setData
(
fileContent
);
// 将文件内容转换为十六进制字符串
String
fileContentHexString
=
toMd5
(
fileContent
);
if
(
checkByMd5
(
fileContentHexString
))
{
throw
new
NotificationException
(
"File already exists"
);
}
//入库
XxImgindex
xxImgindex
=
new
XxImgindex
();
xxImgindex
.
setOurl
(
uploadImageDTO
.
getImagePath
());
xxImgindex
.
setMd5
(
fileContentHexString
);
xxImgindexMapper
.
insert
(
xxImgindex
);
return
xxImgfileMapper
.
insert
(
xxImgfile
);
}
catch
(
Exception
e
)
{
throw
new
NotificationException
(
e
.
getCause
()
==
null
?
e
.
getMessage
()
:
e
.
getCause
().
getMessage
());
...
...
@@ -87,4 +105,24 @@ public class ImageUploadService {
// error.printStackTrace();
// });
}
private
boolean
checkByMd5
(
String
imageUrl
)
{
XxImgindexExample
example
=
new
XxImgindexExample
();
example
.
createCriteria
().
andMd5EqualTo
(
imageUrl
);
return
xxImgindexMapper
.
countByExample
(
example
)
>=
1
;
}
private
static
final
char
[]
hexCode
=
"0123456789ABCDEF"
.
toCharArray
();
private
static
String
toMd5
(
byte
[]
data
)
throws
NoSuchAlgorithmException
{
MessageDigest
digest
=
MessageDigest
.
getInstance
(
"MD5"
);
digest
.
update
(
data
,
0
,
data
.
length
);
byte
[]
hashBytes
=
digest
.
digest
();
StringBuilder
r
=
new
StringBuilder
(
hashBytes
.
length
*
2
);
for
(
byte
b
:
hashBytes
)
{
r
.
append
(
hexCode
[(
b
>>
4
)
&
0xF
]);
r
.
append
(
hexCode
[(
b
&
0xF
)]);
}
return
r
.
toString
();
}
}
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