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
0987d6e9
Commit
0987d6e9
authored
Apr 22, 2025
by
fangxinjiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传文件大小压缩到1M以内-fxj
parent
fef5f3fb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
0 deletions
+23
-0
ExcelToImage.java
...com/yifu/cloud/plus/v1/yifu/social/util/ExcelToImage.java
+23
-0
No files found.
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/util/ExcelToImage.java
View file @
0987d6e9
...
@@ -12,11 +12,14 @@ import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog;
...
@@ -12,11 +12,14 @@ import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
javax.imageio.ImageIO
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.net.URL
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.Base64
;
import
java.util.Base64
;
/**
/**
...
@@ -55,6 +58,8 @@ public class ExcelToImage {
...
@@ -55,6 +58,8 @@ public class ExcelToImage {
// 上传税友文件服务器,返回文件路径
// 上传税友文件服务器,返回文件路径
file
=
new
File
(
projectRoot
+
toImag
);
file
=
new
File
(
projectRoot
+
toImag
);
if
(
file
.
exists
()){
if
(
file
.
exists
()){
// file 压缩到1M以内
file
=
compressImage
(
projectRoot
,
toImag
,
1024
*
1024
);
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
String
key
=
System
.
currentTimeMillis
()
+
file
.
getName
();
byte
[]
fileContent
=
Files
.
readAllBytes
(
file
.
toPath
());
byte
[]
fileContent
=
Files
.
readAllBytes
(
file
.
toPath
());
String
base64String
=
Base64
.
getEncoder
().
encodeToString
(
fileContent
);
String
base64String
=
Base64
.
getEncoder
().
encodeToString
(
fileContent
);
...
@@ -80,6 +85,24 @@ public class ExcelToImage {
...
@@ -80,6 +85,24 @@ public class ExcelToImage {
}
}
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
);
}
}
//压缩文件到指定大小
private
File
compressImage
(
String
projectRoot
,
String
toImag
,
int
maxSize
)
throws
IOException
{
File
originalFile
=
Paths
.
get
(
projectRoot
,
toImag
).
toFile
();
BufferedImage
image
=
ImageIO
.
read
(
originalFile
);
int
quality
=
100
;
// 从100%开始
File
compressedFile
=
new
File
(
projectRoot
,
"compressed_"
+
toImag
);
while
(
originalFile
.
length
()
>
maxSize
&&
quality
>
10
)
{
quality
-=
5
;
ImageIO
.
write
(
image
,
"png"
,
compressedFile
);
if
(
compressedFile
.
length
()
<
maxSize
)
{
originalFile
.
delete
();
compressedFile
.
renameTo
(
originalFile
);
break
;
}
compressedFile
.
delete
();
}
return
originalFile
;
}
public
R
<
URL
>
excelToImg
(
InputStream
inputStream
,
OSSUtil
ossUtil
){
public
R
<
URL
>
excelToImg
(
InputStream
inputStream
,
OSSUtil
ossUtil
){
if
(
null
==
inputStream
||
null
==
ossUtil
){
if
(
null
==
inputStream
||
null
==
ossUtil
){
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagParamError
);
return
R
.
failed
(
CommonConstants
.
RESULT_DATA_FAIL
+
toImagParamError
);
...
...
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