Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
C
chinafrica
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
zhengfg
chinafrica
Commits
c741e8be
提交
c741e8be
authored
3月 23, 2021
作者:
吴德鹏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增图片压缩
上级
35afd3b8
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
108 行增加
和
21 行删除
+108
-21
OssTestApi.java
...src/main/java/com/platform/controller/api/OssTestApi.java
+19
-15
platform.properties
platform-admin/src/main/resources/dev/platform.properties
+2
-2
platform.properties
platform-admin/src/main/resources/prod/platform.properties
+2
-2
pom.xml
platform-common/pom.xml
+6
-2
PicUtils.java
...orm-common/src/main/java/com/platform/utils/PicUtils.java
+79
-0
没有找到文件。
platform-admin/src/main/java/com/platform/controller/api/OssTestApi.java
浏览文件 @
c741e8be
package
com
.
platform
.
controller
.
api
;
package
com
.
platform
.
controller
.
api
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.text.DecimalFormat
;
import
java.text.DecimalFormat
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpSession
;
import
javax.servlet.http.HttpSession
;
import
com.platform.utils.OssUtil
;
import
com.platform.utils.OssUtil
;
import
com.platform.utils.PicUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
@@ -52,21 +51,26 @@ public class OssTestApi {
...
@@ -52,21 +51,26 @@ public class OssTestApi {
@ResponseBody
@ResponseBody
public
String
upload
(
MultipartFile
file
,
String
secondaryFolderName
)
{
public
String
upload
(
MultipartFile
file
,
String
secondaryFolderName
)
{
try
{
try
{
byte
[]
bytes
=
PicUtils
.
compressPicForScale
(
file
.
getBytes
(),
70
);
// 获取文件名
// 获取文件名
String
fileName
=
file
.
getOriginalFilename
();
//
String fileName = file.getOriginalFilename();
// 获取文件后缀
//
// 获取文件后缀
String
prefix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
//
String prefix = fileName.substring(fileName.lastIndexOf("."));
// 用uuid作为文件名,防止生成的文件重复
// 用uuid作为文件名,防止生成的文件重复
final
File
mfile
=
File
.
createTempFile
(
UuidUtil
.
get32UUID
(),
prefix
);
// final File mfile = File.createTempFile(UuidUtil.get32UUID(), prefix);
long
l
=
System
.
currentTimeMillis
();
String
urlName
=
UUID
.
randomUUID
()
+
"-"
+
String
.
valueOf
(
l
).
substring
(
6
)
+
".jpg"
;
// MultipartFile to File
String
url
=
OssUtil
.
upload
(
bytes
,
urlName
,
"afrishop_new"
);
file
.
transferTo
(
mfile
);
if
(
StringUtils
.
isBlank
(
secondaryFolderName
))
{
secondaryFolderName
=
"afrishop"
;
}
String
url
=
OssUtil
.
uploadObject2OSS
(
mfile
,
secondaryFolderName
);
System
.
out
.
println
(
mfile
.
getName
());
System
.
out
.
println
(
url
);
System
.
out
.
println
(
url
);
// MultipartFile to File
// file.transferTo(mfile);
// if (StringUtils.isBlank(secondaryFolderName)) {
// secondaryFolderName = "afrishop";
// }
// String url = OssUtil.uploadObject2OSS(mfile, secondaryFolderName);
// System.out.println(mfile.getName());
// System.out.println(url);
return
url
;
return
url
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
platform-admin/src/main/resources/dev/platform.properties
浏览文件 @
c741e8be
...
@@ -7,9 +7,9 @@
...
@@ -7,9 +7,9 @@
#jdbc.password=diaoyun666
#jdbc.password=diaoyun666
jdbc.url
:
jdbc:mysql://159.138.48.71:3306/
test
?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.url
:
jdbc:mysql://159.138.48.71:3306/
chinafrica
?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.username
:
root
jdbc.username
:
root
jdbc.password
:
Diaoyunnuli.
8
jdbc.password
:
AfriShop_date@2#!&*
8
jdbc.initialSize
=
5
jdbc.initialSize
=
5
jdbc.maxActive
=
30
jdbc.maxActive
=
30
...
...
platform-admin/src/main/resources/prod/platform.properties
浏览文件 @
c741e8be
...
@@ -7,9 +7,9 @@
...
@@ -7,9 +7,9 @@
#jdbc.password=diaoyun666
#jdbc.password=diaoyun666
jdbc.url
:
jdbc:mysql://159.138.48.71:3306/
test
?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.url
:
jdbc:mysql://159.138.48.71:3306/
chinafrica
?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.username
:
root
jdbc.username
:
root
jdbc.password
:
Diaoyunnuli.
8
jdbc.password
:
AfriShop_date@2#!&*
8
jdbc.initialSize
=
5
jdbc.initialSize
=
5
jdbc.maxActive
=
30
jdbc.maxActive
=
30
...
...
platform-common/pom.xml
浏览文件 @
c741e8be
...
@@ -25,7 +25,11 @@
...
@@ -25,7 +25,11 @@
</properties>
</properties>
<dependencies>
<dependencies>
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.8
</version>
</dependency>
<!-- excel工具 -->
<!-- excel工具 -->
<dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<groupId>
javax.servlet
</groupId>
...
@@ -249,4 +253,4 @@
...
@@ -249,4 +253,4 @@
</dependency>-->
</dependency>-->
</dependencies>
</dependencies>
</project>
</project>
\ No newline at end of file
platform-common/src/main/java/com/platform/utils/PicUtils.java
0 → 100644
浏览文件 @
c741e8be
package
com
.
platform
.
utils
;
import
net.coobird.thumbnailator.Thumbnails
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
/**
* @Auther: wudepeng
* @Date: 2021/01/25
* @Description:
*/
public
class
PicUtils
{
//以下是常量,按照阿里代码开发规范,不允许代码中出现魔法值
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
PicUtils
.
class
);
private
static
final
Integer
ZERO
=
0
;
private
static
final
Integer
ONE_ZERO_TWO_FOUR
=
1024
;
private
static
final
Integer
NINE_ZERO_ZERO
=
900
;
private
static
final
Integer
THREE_TWO_SEVEN_FIVE
=
3275
;
private
static
final
Integer
TWO_ZERO_FOUR_SEVEN
=
2047
;
private
static
final
Double
ZERO_EIGHT_FIVE
=
0.85
;
private
static
final
Double
ZERO_SIX
=
0.6
;
private
static
final
Double
ZERO_FOUR_FOUR
=
0.44
;
private
static
final
Double
ZERO_FOUR
=
0.4
;
/**
* 根据指定大小压缩图片
*
* @param imageBytes 源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @return 压缩质量后的图片字节数组
*/
public
static
byte
[]
compressPicForScale
(
byte
[]
imageBytes
,
long
desFileSize
)
{
if
(
imageBytes
==
null
||
imageBytes
.
length
<=
ZERO
||
imageBytes
.
length
<
desFileSize
*
ONE_ZERO_TWO_FOUR
)
{
return
imageBytes
;
}
long
srcSize
=
imageBytes
.
length
;
double
accuracy
=
getAccuracy
(
srcSize
/
ONE_ZERO_TWO_FOUR
);
try
{
while
(
imageBytes
.
length
>
desFileSize
*
ONE_ZERO_TWO_FOUR
)
{
ByteArrayInputStream
inputStream
=
new
ByteArrayInputStream
(
imageBytes
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
(
imageBytes
.
length
);
Thumbnails
.
of
(
inputStream
)
.
scale
(
accuracy
)
.
outputQuality
(
accuracy
)
.
toOutputStream
(
outputStream
);
imageBytes
=
outputStream
.
toByteArray
();
}
logger
.
info
(
"图片原大小={}kb | 压缩后大小={}kb"
,
srcSize
/
ONE_ZERO_TWO_FOUR
,
imageBytes
.
length
/
ONE_ZERO_TWO_FOUR
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"【图片压缩】msg=图片压缩失败!"
,
e
);
}
return
imageBytes
;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private
static
double
getAccuracy
(
long
size
)
{
double
accuracy
;
if
(
size
<
NINE_ZERO_ZERO
)
{
accuracy
=
ZERO_EIGHT_FIVE
;
}
else
if
(
size
<
TWO_ZERO_FOUR_SEVEN
)
{
accuracy
=
ZERO_SIX
;
}
else
if
(
size
<
THREE_TWO_SEVEN_FIVE
)
{
accuracy
=
ZERO_FOUR_FOUR
;
}
else
{
accuracy
=
ZERO_FOUR
;
}
return
accuracy
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论