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
94cc08c1
Commit
94cc08c1
authored
May 21, 2024
by
cuijun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件压缩250图片格式
parent
ef557864
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
0 deletions
+54
-0
UploadController.java
...ns/platform/rs/universal/controller/UploadController.java
+18
-0
UploadService.java
...m/upyuns/platform/rs/universal/service/UploadService.java
+36
-0
No files found.
rs-universal/rs-universal-server/src/main/java/com/upyuns/platform/rs/universal/controller/UploadController.java
View file @
94cc08c1
...
...
@@ -130,6 +130,24 @@ public class UploadController{
return
JsonResultUtil
.
createSuccessResultWithObj
(
uploadService
.
uploadFile
(
file
,
prefix
));
}
@RequestMapping
(
value
=
"/app/unauth/admin/upload250"
,
method
=
RequestMethod
.
POST
)
public
JSONObject
uploads250
(
@RequestParam
(
"file"
)
MultipartFile
file
,
@RequestParam
(
value
=
"prefix"
,
defaultValue
=
"admin"
)
String
prefix
)
throws
Exception
{
String
contentType
=
file
.
getContentType
();
//图片文件类型
// String fileName = file.getOriginalFilename(); //图片名字
/* if (!contentType.equals("image/jpeg") && !contentType.equals("image/gif")&&!contentType.equals("image/png")) {
return JsonResultUtil.createFailedResult(2001,"格式不对!!!");
}*/
// if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
// return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
// }
return
JsonResultUtil
.
createSuccessResultWithObj
(
uploadService
.
uploadFile250
(
file
,
prefix
));
}
@RequestMapping
(
value
=
"/app/unauth/shp/upload"
,
method
=
RequestMethod
.
POST
)
public
ObjectRestResponse
shpUploads
(
@RequestParam
(
"file"
)
MultipartFile
file
,
...
...
rs-universal/rs-universal-server/src/main/java/com/upyuns/platform/rs/universal/service/UploadService.java
View file @
94cc08c1
...
...
@@ -25,6 +25,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.imageio.ImageIO
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
...
...
@@ -102,6 +103,41 @@ public class UploadService {
return
realFileRelPath
;
}
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
+
"/compressed_"
+
no
+
".jpg"
;
ImageIO
.
write
(
resizedImage
,
"jpg"
,
new
File
(
compressedFilePath
));
realFileRelPath
=
xx_url
+
SystemConfig
.
XXMP_URL
+
realFileRelPath
;
return
realFileRelPath
;
}
/**
* 写入上传文件,返回相对路径
* @param file
...
...
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