Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fangxinjiang
yifu-mvp
Commits
15000ac4
Commit
15000ac4
authored
Jun 14, 2024
by
fangxinjiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
二维码生成fxj 2024-06-14
parent
58f90fe2
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
179 additions
and
2 deletions
+179
-2
pom.xml
yifu-archives/yifu-archives-biz/pom.xml
+12
-0
FileUploadController.java
...lus/v1/yifu/archives/controller/FileUploadController.java
+14
-0
FileUploadService.java
...loud/plus/v1/yifu/archives/service/FileUploadService.java
+3
-0
FileUploadServiceImpl.java
.../v1/yifu/archives/service/impl/FileUploadServiceImpl.java
+76
-1
QrCodeUtil.java
...om/yifu/cloud/plus/v1/yifu/archives/utils/QrCodeUtil.java
+72
-0
spring.factories
...archives-biz/src/main/resources/META-INF/spring.factories
+2
-1
No files found.
yifu-archives/yifu-archives-biz/pom.xml
View file @
15000ac4
...
...
@@ -94,6 +94,18 @@
<version>
4.1.1
</version>
<scope>
compile
</scope>
</dependency>
<!-- 二维码生成JAR开始 -->
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
core
</artifactId>
<version>
3.5.1
</version>
</dependency>
<dependency>
<groupId>
com.google.zxing
</groupId>
<artifactId>
javase
</artifactId>
<version>
3.5.1
</version>
</dependency>
<!-- 二维码生成JAR结束 -->
</dependencies>
<build>
...
...
yifu-archives/yifu-archives-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/archives/controller/FileUploadController.java
View file @
15000ac4
...
...
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import
com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo
;
import
com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService
;
import
com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService
;
import
com.yifu.cloud.plus.v1.yifu.archives.service.impl.FileUploadServiceImpl
;
import
com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
...
...
@@ -143,4 +145,16 @@ public class FileUploadController {
}
return
R
.
ok
(
voList
);
}
/**
* 获取附件
* @author fxj
* @return R
*/
@Operation
(
summary
=
"生成二维码并上传阿里云保存到本地"
,
description
=
"生成二维码并上传阿里云保存到本地"
)
@SysLog
(
"生成二维码并上传阿里云保存到本地"
)
@GetMapping
(
"/createQrCode"
)
public
R
<
FileVo
>
createQrCode
(
@RequestParam
String
domainId
,
@RequestParam
String
url
,
@RequestParam
String
name
)
{
return
fileUploadService
.
createQrCode
(
domainId
,
url
,
name
);
}
}
yifu-archives/yifu-archives-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/archives/service/FileUploadService.java
View file @
15000ac4
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
archives
.
service
;
import
com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo
;
import
org.springframework.web.multipart.MultipartFile
;
...
...
@@ -15,4 +16,6 @@ import java.io.IOException;
public
interface
FileUploadService
{
R
<
FileVo
>
uploadImg
(
MultipartFile
file
,
String
filePath
,
Integer
type
,
String
domain
)
throws
IOException
;
R
<
FileVo
>
createQrCode
(
String
domainId
,
String
url
,
String
name
);
}
yifu-archives/yifu-archives-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/archives/service/impl/FileUploadServiceImpl.java
View file @
15000ac4
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
archives
.
service
.
impl
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo
;
import
com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService
;
import
com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService
;
import
com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.time.LocalDateTime
;
...
...
@@ -25,6 +30,8 @@ public class FileUploadServiceImpl implements FileUploadService {
private
final
TAttaInfoService
tAttaInfoService
;
private
final
QrCodeUtil
codeUtil
;
@Override
public
R
<
FileVo
>
uploadImg
(
MultipartFile
file
,
String
filePath
,
Integer
type
,
String
domain
)
throws
IOException
{
if
(
null
==
file
)
{
...
...
@@ -78,6 +85,57 @@ public class FileUploadServiceImpl implements FileUploadService {
}
}
private
R
<
FileVo
>
uploadImg2
(
InputStream
fileIn
,
String
filePath
,
Integer
type
,
String
domain
,
String
fileName
)
throws
IOException
{
if
(
null
==
fileIn
)
{
return
R
.
failed
(
"文件上传异常,请重新上传!"
);
}
if
(
Common
.
isEmpty
(
fileName
))
{
return
R
.
failed
(
"文件名不能为空!"
);
}
if
(
fileName
.
length
()
>
100
)
{
return
R
.
failed
(
"文件名不能超过100个字符!"
);
}
//filePath不传默认存储空间的根目录
//jpg,jpeg,png,bmp
String
key
=
""
;
if
(
Common
.
isNotNull
(
filePath
))
{
key
=
filePath
+
"/"
+
System
.
currentTimeMillis
()
+
fileName
;
}
else
{
key
=
System
.
currentTimeMillis
()
+
fileName
;
}
if
(!
Common
.
checkFile
(
key
))
{
return
R
.
failed
(
"非法上传类型!"
);
}
boolean
flag
=
ossUtil
.
uploadFileByStream
(
fileIn
,
key
,
null
);
FileVo
fileVo
;
TAttaInfo
attaInfo
;
URL
url
;
if
(
flag
)
{
log
.
info
(
"文件:"
+
fileName
+
"上传至存储空间"
+
ossUtil
.
getBucketName
()
+
"成功!"
);
attaInfo
=
initUnitAttaForInsert
(
fileName
,
key
,
0
);
attaInfo
.
setRelationType
(
String
.
valueOf
(
type
));
try
{
attaInfo
.
setDomainId
(
domain
);
attaInfo
=
tAttaInfoService
.
add
(
attaInfo
);
}
catch
(
Exception
e
)
{
log
.
error
(
"OSS文件上传接口异常:"
+
e
.
getMessage
());
ossUtil
.
deleteObject
(
null
,
key
);
return
R
.
failed
(
"fail:"
+
e
.
getMessage
());
}
//有实体id则插入关系,用于编辑
if
(
Common
.
isNotNull
(
domain
))
{
url
=
ossUtil
.
getObjectUrl
(
null
,
attaInfo
.
getAttaSrc
());
fileVo
=
new
FileVo
(
attaInfo
.
getId
(),
attaInfo
.
getAttaName
(),
url
.
toString
());
return
R
.
ok
(
fileVo
,
"success"
);
}
url
=
ossUtil
.
getObjectUrl
(
null
,
attaInfo
.
getAttaSrc
());
fileVo
=
new
FileVo
(
attaInfo
.
getId
(),
attaInfo
.
getAttaName
(),
url
.
toString
());
return
R
.
ok
(
fileVo
,
"success"
);
}
else
{
return
R
.
failed
(
"fail:上传失败"
);
}
}
/**
* 对象初始化
* @param fileName
...
...
@@ -102,4 +160,21 @@ public class FileUploadServiceImpl implements FileUploadService {
unitAtta
.
setCreateTime
(
LocalDateTime
.
now
());
return
unitAtta
;
}
}
@Override
public
R
<
FileVo
>
createQrCode
(
String
domainId
,
String
url
,
String
name
)
{
TAttaInfo
attaInfo
=
tAttaInfoService
.
getOne
(
Wrappers
.<
TAttaInfo
>
query
().
lambda
().
eq
(
TAttaInfo:
:
getAttaSrc
,
domainId
).
last
(
CommonConstants
.
LAST_ONE_SQL
));
FileVo
fileVo
;
if
(
Common
.
isNotNull
(
attaInfo
)){
fileVo
=
new
FileVo
(
attaInfo
.
getId
(),
attaInfo
.
getAttaName
(),
url
.
toString
());
return
R
.
ok
(
fileVo
);
}
InputStream
in
=
codeUtil
.
createPng
(
url
);
try
{
return
this
.
uploadImg2
(
in
,
"mvp_qr"
,
CommonConstants
.
EIGHT_INT
,
domainId
,
name
);
}
catch
(
IOException
e
)
{
log
.
error
(
"获取二维码失败"
,
e
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
);
}
}
}
\ No newline at end of file
yifu-archives/yifu-archives-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/archives/utils/QrCodeUtil.java
0 → 100644
View file @
15000ac4
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
archives
.
utils
;
import
cn.hutool.core.net.multipart.MultipartRequestInputStream
;
import
com.google.zxing.BarcodeFormat
;
import
com.google.zxing.EncodeHintType
;
import
com.google.zxing.WriterException
;
import
com.google.zxing.common.BitMatrix
;
import
com.google.zxing.qrcode.QRCodeWriter
;
import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
;
import
com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.imageio.ImageIO
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @Author fxj
* @Date 2024/6/13
* @Description
* @Version 1.0
*/
@Slf4j
@Component
public
class
QrCodeUtil
{
public
InputStream
createPng
(
String
qrCodeContent
){
try
{
// 定义二维码的配置参数
Map
<
EncodeHintType
,
Object
>
hints
=
new
HashMap
<>();
hints
.
put
(
EncodeHintType
.
ERROR_CORRECTION
,
ErrorCorrectionLevel
.
M
);
// 设置纠错级别(L、M、Q、H,从低到高)
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"UTF-8"
);
// 设置字符集
// 创建二维码编码器
QRCodeWriter
qrCodeWriter
=
new
QRCodeWriter
();
// 生成二维码矩阵
BitMatrix
bitMatrix
=
qrCodeWriter
.
encode
(
qrCodeContent
,
BarcodeFormat
.
QR_CODE
,
250
,
250
,
hints
);
// 将BitMatrix转换为BufferedImage
int
width
=
bitMatrix
.
getWidth
();
int
height
=
bitMatrix
.
getHeight
();
BufferedImage
image
=
new
BufferedImage
(
width
,
height
,
BufferedImage
.
TYPE_INT_RGB
);
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
image
.
setRGB
(
x
,
y
,
bitMatrix
.
get
(
x
,
y
)
?
0xFF000000
:
0xFFFFFFFF
);
// 黑白二值化
}
}
// 保存为PNG图片
File
file
=
new
File
(
"output_qrcode.png"
);
ImageIO
.
write
(
image
,
"PNG"
,
file
);
InputStream
in
=
new
FileInputStream
(
file
);
return
in
;
}
catch
(
IOException
e
)
{
log
.
error
(
"failed:生成二维码失败"
,
e
.
getMessage
());
}
catch
(
WriterException
e
)
{
log
.
error
(
"failed:生成二维码失败"
,
e
.
getMessage
());
}
return
null
;
}
}
yifu-archives/yifu-archives-biz/src/main/resources/META-INF/spring.factories
View file @
15000ac4
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yifu.cloud.plus.v1.yifu.archives.config.FddAutoConfigue
\ No newline at end of file
com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil,\
com.yifu.cloud.plus.v1.yifu.archives.config.FddAutoConfigue
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