Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
cfld-region-outline
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
cfld-backend
cfld-region-outline
Commits
6b477fb4
Commit
6b477fb4
authored
Jan 05, 2024
by
yaobaizheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片过大进行二次压缩
parent
b1329054
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
19 deletions
+127
-19
PicUtils.java
src/main/java/com/lyy/admin/common/utils/hwobs/PicUtils.java
+116
-0
UploadServiceImpl.java
...y/admin/moudle/system/service/impl/UploadServiceImpl.java
+11
-19
No files found.
src/main/java/com/lyy/admin/common/utils/hwobs/PicUtils.java
0 → 100644
View file @
6b477fb4
package
com
.
lyy
.
admin
.
common
.
utils
.
hwobs
;
import
cn.hutool.core.img.Img
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.util.IdUtil
;
import
org.apache.commons.io.FileUtils
;
import
org.springframework.web.multipart.MultipartFile
;
//import com.xiruo.medbid.util.OssFileUtils;
//import net.coobird.thumbnailator.Thumbnails;
//import org.apache.commons.io.FileUtils;
//import org.apache.http.client.utils.URIBuilder;
import
java.io.*
;
import
java.math.BigDecimal
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.Map
;
public
class
PicUtils
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// String url = "https://mbnprodfile.oss-cn-shenzhen.aliyuncs.com/xxxxx/5868500806729729.jpg";
// System.err.println(zipPic(url, 180));
// String pc_url = "C:\\Users\\26996\\Pictures\\Screenshots\\1704418796(1) - 副本.jpg";
// System.err.println(zipPicPC(pc_url, 400));
String
pc_url
=
"C:\\Users\\26996\\Pictures\\Screenshots\\微信图片_20240105112648 - 副本.jpg"
;
String
pc_url1
=
"C:\\Users\\26996\\Pictures\\Screenshots\\1704418796(1) - 副本1.jpg"
;
File
file
=
new
File
(
pc_url
);
File
file1
=
new
File
(
pc_url1
);
compressPicForScale1
(
file
,
file1
,
800
);
}
public
static
void
disposeFile
(
MultipartFile
files
,
String
localDir
,
String
localDir1
)
throws
IOException
{
//将MultipartFile转为File,存储为本地临时文件
File
file
=
new
File
(
localDir
);
InputStream
inputStream
=
files
.
getInputStream
();
FileUtils
.
copyInputStreamToFile
(
inputStream
,
file
);
//目标文件
File
file1
=
FileUtil
.
file
(
localDir1
);
compressPicForScale1
(
file
,
file1
,
800
);
}
/**
* 根据指定大小压缩图片
*
* @param file 源图片字节数组
* @param newFile 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @return 压缩质量后的图片字节数组
*/
public
static
void
compressPicForScale1
(
File
file
,
File
newFile
,
long
desFileSize
)
throws
IOException
{
long
fileLength
=
file
.
length
();
if
(
fileLength
<=
0
||
fileLength
<
desFileSize
*
1024
)
{
return
;
}
Img
.
from
(
file
).
setQuality
(
1
).
write
(
newFile
);
if
(
newFile
.
length
()
>
desFileSize
*
1024
){
double
accuracy
=
getAccuracy
(
newFile
.
length
()
/
1024
);
Img
.
from
(
newFile
).
setQuality
(
accuracy
).
write
(
newFile
);
}
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private
static
double
getAccuracy
(
long
size
)
{
double
accuracy
=
1
;
if
(
size
>=
800
&&
size
<
1024
)
{
accuracy
=
0.6
;
}
if
(
size
>=
1024
&&
size
<
2048
)
{
accuracy
=
0.3
;
}
if
(
size
>=
2048
)
{
accuracy
=
0.1
;
}
return
accuracy
;
}
public
static
byte
[]
readInputStream
(
InputStream
inStream
)
throws
IOException
{
ByteArrayOutputStream
outStream
=
new
ByteArrayOutputStream
();
//创建一个Buffer字符串
byte
[]
buffer
=
new
byte
[
1024
];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int
len
=
0
;
//使用一个输入流从buffer里把数据读取出来
while
((
len
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream
.
write
(
buffer
,
0
,
len
);
}
//关闭输入流
inStream
.
close
();
//把outStream里的数据写入内存
return
outStream
.
toByteArray
();
}
}
src/main/java/com/lyy/admin/moudle/system/service/impl/UploadServiceImpl.java
View file @
6b477fb4
...
@@ -7,6 +7,7 @@ import cn.hutool.core.img.ImgUtil;
...
@@ -7,6 +7,7 @@ import cn.hutool.core.img.ImgUtil;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.IdUtil
;
import
com.lyy.admin.common.utils.hwobs.ObsUtils
;
import
com.lyy.admin.common.utils.hwobs.ObsUtils
;
import
com.lyy.admin.common.utils.hwobs.PicUtils
;
import
com.lyy.admin.moudle.system.service.UploadService
;
import
com.lyy.admin.moudle.system.service.UploadService
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
...
@@ -62,37 +63,28 @@ public class UploadServiceImpl implements UploadService {
...
@@ -62,37 +63,28 @@ public class UploadServiceImpl implements UploadService {
}
else
{
}
else
{
String
newFile
=
IdUtil
.
simpleUUID
();
String
newFile
=
IdUtil
.
simpleUUID
();
String
newFilename
=
newFile
+
fileLastType
;
String
newFilename
=
newFile
+
fileLastType
;
TimeInterval
timer
=
DateUtil
.
timer
();
//源文件路径
//将MultipartFile转为File,存储为本地临时文件
String
localDir
=
tempFilePath
+
newFilename
;
String
localDir
=
tempFilePath
+
newFilename
;
File
file
=
new
File
(
localDir
);
//压缩之后的文件路径
InputStream
inputStream
=
files
.
getInputStream
();
FileUtils
.
copyInputStreamToFile
(
inputStream
,
file
);
String
newFilename1
=
IdUtil
.
simpleUUID
()
+
".jpg"
;
String
newFilename1
=
IdUtil
.
simpleUUID
()
+
".jpg"
;
String
localDir1
=
tempFilePath
+
newFilename1
;
String
localDir1
=
tempFilePath
+
newFilename1
;
File
file1
=
FileUtil
.
file
(
localDir1
);
System
.
out
.
println
(
"图片存储与新建"
+
timer
.
interval
());
TimeInterval
timer1
=
DateUtil
.
timer
();
Img
.
from
(
file
).
setQuality
(
1
).
write
(
file1
);
System
.
out
.
println
(
"图片压缩"
+
timer1
.
interval
());
//压缩文件处理
TimeInterval
timer1
=
DateUtil
.
timer
();
PicUtils
.
disposeFile
(
files
,
localDir
,
localDir1
);
System
.
out
.
println
(
"图片压缩处理:"
+
timer1
.
interval
());
//上传文件
TimeInterval
timer2
=
DateUtil
.
timer
();
TimeInterval
timer2
=
DateUtil
.
timer
();
InputStream
inputStream1
=
new
FileInputStream
(
localDir1
);
InputStream
inputStream1
=
new
FileInputStream
(
localDir1
);
filePath
=
"/"
+
date
+
"/"
+
newFile
+
".jpg"
;
filePath
=
"/"
+
date
+
"/"
+
newFile
+
".jpg"
;
obsUtils
.
ObsUpload
(
bucketName
,
rootPath
+
filePath
,
inputStream1
);
obsUtils
.
ObsUpload
(
bucketName
,
rootPath
+
filePath
,
inputStream1
);
System
.
out
.
println
(
"图片上传"
+
timer2
.
interval
());
System
.
out
.
println
(
"图片上传
:
"
+
timer2
.
interval
());
//删除临时文件
//删除临时文件
CompletableFuture
.
runAsync
(()->
{
CompletableFuture
.
runAsync
(()->
{
try
{
FileUtil
.
del
(
localDir
);
FileUtils
.
delete
(
file
);
FileUtil
.
del
(
localDir1
);
FileUtils
.
delete
(
file1
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
});
});
inputStream
.
close
();
inputStream1
.
close
();
inputStream1
.
close
();
}
}
return
rootPath
+
filePath
;
return
rootPath
+
filePath
;
...
...
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