Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rs-cloud-platform
Project
Project
Details
Activity
Releases
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
周健威
rs-cloud-platform
Commits
55f85b34
Commit
55f85b34
authored
May 21, 2024
by
cuijun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件压缩250图片格式
parent
94cc08c1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
4 deletions
+48
-4
UploadService.java
...m/upyuns/platform/rs/universal/service/UploadService.java
+48
-4
No files found.
rs-universal/rs-universal-server/src/main/java/com/upyuns/platform/rs/universal/service/UploadService.java
View file @
55f85b34
...
@@ -104,6 +104,37 @@ public class UploadService {
...
@@ -104,6 +104,37 @@ public class UploadService {
}
}
// public String uploadFile250(MultipartFile file, String prefix) throws Exception {
//// // 创建本日存放目录
//// DateTime now = DateTime.now();
//// String dirPathToday = "/" + prefix + "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
//// String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
//// Long no = redisTemplate.opsForValue().increment(redisNoKey);
//// if (no.equals(1L)) {
//// redisTemplate.expire(redisNoKey, 1, TimeUnit.DAYS);
//// }
//// String fileName = file.getOriginalFilename();
//// String realFileRelPath = dirPathToday + "/" + no + fileName.substring(fileName.lastIndexOf("."));
//// // 文件存放路径
//// String filePath = baseUploadPath + realFileRelPath;
//// // 将文件写入指定位置
//// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
////
//// // 将上传的图片压缩成250*250的尺寸
//// BufferedImage originalImage = ImageIO.read(new File(filePath));
//// BufferedImage resizedImage = new BufferedImage(250, 250, originalImage.getType());
//// Graphics2D g = resizedImage.createGraphics();
//// g.drawImage(originalImage, 0, 0, 250, 250, null);
//// g.dispose();
////
//// // 保存压缩后的图片
//// String compressedFilePath = baseUploadPath + dirPathToday + no + ".jpg_250";
//// ImageIO.write(resizedImage, "jpg", new File(compressedFilePath));
////
//// realFileRelPath = xx_url + SystemConfig.XXMP_URL + realFileRelPath;
//// return realFileRelPath;
//// }
public
String
uploadFile250
(
MultipartFile
file
,
String
prefix
)
throws
Exception
{
public
String
uploadFile250
(
MultipartFile
file
,
String
prefix
)
throws
Exception
{
// 创建本日存放目录
// 创建本日存放目录
DateTime
now
=
DateTime
.
now
();
DateTime
now
=
DateTime
.
now
();
...
@@ -120,15 +151,28 @@ public class UploadService {
...
@@ -120,15 +151,28 @@ public class UploadService {
// 将文件写入指定位置
// 将文件写入指定位置
FileUtils
.
copyInputStreamToFile
(
file
.
getInputStream
(),
new
File
(
filePath
));
FileUtils
.
copyInputStreamToFile
(
file
.
getInputStream
(),
new
File
(
filePath
));
// 将上传的图片
压缩成250*250的
尺寸
// 将上传的图片
等比例缩放至250x250
尺寸
BufferedImage
originalImage
=
ImageIO
.
read
(
new
File
(
filePath
));
BufferedImage
originalImage
=
ImageIO
.
read
(
new
File
(
filePath
));
BufferedImage
resizedImage
=
new
BufferedImage
(
250
,
250
,
originalImage
.
getType
());
int
originalWidth
=
originalImage
.
getWidth
();
int
originalHeight
=
originalImage
.
getHeight
();
double
scaleFactor
=
1.0
;
if
(
originalWidth
>
originalHeight
)
{
scaleFactor
=
(
double
)
250
/
originalWidth
;
}
else
{
scaleFactor
=
(
double
)
250
/
originalHeight
;
}
int
newWidth
=
(
int
)
(
originalWidth
*
scaleFactor
);
int
newHeight
=
(
int
)
(
originalHeight
*
scaleFactor
);
BufferedImage
resizedImage
=
new
BufferedImage
(
newWidth
,
newHeight
,
originalImage
.
getType
());
Graphics2D
g
=
resizedImage
.
createGraphics
();
Graphics2D
g
=
resizedImage
.
createGraphics
();
g
.
drawImage
(
originalImage
,
0
,
0
,
250
,
250
,
null
);
g
.
drawImage
(
originalImage
,
0
,
0
,
newWidth
,
newHeight
,
null
);
g
.
dispose
();
g
.
dispose
();
// 保存压缩后的图片
// 保存压缩后的图片
String
compressedFilePath
=
baseUploadPath
+
dirPathToday
+
"/compressed_"
+
no
+
".jpg
"
;
String
compressedFilePath
=
baseUploadPath
+
dirPathToday
+
no
+
".jpg_250
"
;
ImageIO
.
write
(
resizedImage
,
"jpg"
,
new
File
(
compressedFilePath
));
ImageIO
.
write
(
resizedImage
,
"jpg"
,
new
File
(
compressedFilePath
));
realFileRelPath
=
xx_url
+
SystemConfig
.
XXMP_URL
+
realFileRelPath
;
realFileRelPath
=
xx_url
+
SystemConfig
.
XXMP_URL
+
realFileRelPath
;
...
...
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