Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
ne-shield-standard-interfaces
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
1
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 825e1cd0
authored
Nov 08, 2022
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化图片压缩接口
1 parent
65d25b68
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
631 additions
and
196 deletions
pom.xml
src/main/java/cn/com/glsx/stdinterface/modules/api/WorkOrderApiController.java
src/main/java/cn/com/glsx/stdinterface/modules/entity/WorkOrderDocs.java
src/main/java/cn/com/glsx/stdinterface/modules/mapper/WorkOrderDocsMapper.java
src/main/java/cn/com/glsx/stdinterface/modules/mapper/WorkOrderMapper.java
src/main/java/cn/com/glsx/stdinterface/modules/service/WorkOrderDocsService.java
src/main/java/cn/com/glsx/stdinterface/modules/service/WorkOrderService.java
src/main/java/cn/com/glsx/stdinterface/modules/util/ThumbnailatorUtil.java
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileConstants.java
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileUploadServerData.java
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileUploadUtil.java
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/DownFile.java
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/MultipleThreadDownloadManager.java
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/UnitDownloader.java
src/main/resources/mapper/WorkOrderDocsMapper.xml
pom.xml
View file @
825e1cd
...
...
@@ -91,6 +91,12 @@
<version>
1.2.0-SNAPSHOT
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.8
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/cn/com/glsx/stdinterface/modules/api/WorkOrderApiController.java
View file @
825e1cd
...
...
@@ -15,10 +15,13 @@ import cn.com.glsx.stdinterface.modules.service.WorkOrderService;
import
cn.com.glsx.stdinterface.modules.service.ZhongliVehicleService
;
import
cn.com.glsx.stdinterface.modules.util.Sm4DecryptUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.glsx.plat.common.annotation.SysLog
;
import
com.glsx.plat.common.enums.OperateType
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.context.utils.PropertiesUtils
;
import
com.glsx.plat.core.web.R
;
import
com.google.common.collect.Lists
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -292,4 +295,21 @@ public class WorkOrderApiController {
return
decryptData
;
}
/**
* 压缩工单图片
*
* @param workOrderId 工单id
* @return
*/
@GetMapping
(
value
=
"/compressImages"
)
@SysLog
(
module
=
"工单API"
,
value
=
"压缩图片"
,
action
=
OperateType
.
QUERY
)
@ApiOperation
(
value
=
"压缩图片"
)
public
R
compressImages
(
@RequestParam
(
required
=
false
)
Long
workOrderId
)
{
try
{
workOrderDocsService
.
compressImages
(
workOrderId
);
}
catch
(
Exception
e
)
{
log
.
info
(
"compressImages error:"
+
e
.
getMessage
(),
e
);
}
return
R
.
ok
();
}
}
src/main/java/cn/com/glsx/stdinterface/modules/entity/WorkOrderDocs.java
View file @
825e1cd
...
...
@@ -31,4 +31,10 @@ public class WorkOrderDocs extends BaseEntity {
*/
private
String
type
;
/**
* 压缩之后图片地址/路径
*/
@Column
(
name
=
"compress_url"
)
private
String
compressUrl
;
}
\ No newline at end of file
src/main/java/cn/com/glsx/stdinterface/modules/mapper/WorkOrderDocsMapper.java
View file @
825e1cd
...
...
@@ -21,6 +21,14 @@ public interface WorkOrderDocsMapper extends CommonBaseMapper<WorkOrderDocs> {
List
<
WorkOrderDocs
>
selectByOrderId
(
@Param
(
"orderId"
)
Long
orderId
);
/**
* 获取所有图片数据
*
* @return 图片数据列表
*/
List
<
WorkOrderDocs
>
selectAllImages
();
/**
* 通过 工单Id 获取图片数据
*
* @param orderIds 工单Id
...
...
src/main/java/cn/com/glsx/stdinterface/modules/mapper/WorkOrderMapper.java
View file @
825e1cd
...
...
@@ -104,5 +104,4 @@ public interface WorkOrderMapper extends CommonBaseMapper<WorkOrder> {
* @return
*/
List
<
CompletedWorkOrderModel
>
selectAllCompletedWorkOrders
();
}
\ No newline at end of file
src/main/java/cn/com/glsx/stdinterface/modules/service/WorkOrderDocsService.java
View file @
825e1cd
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
service
;
import
cn.com.glsx.admin.common.util.fileupload.FileConstants
;
import
cn.com.glsx.admin.common.util.fileupload.FileUploadServerData
;
import
cn.com.glsx.admin.common.util.fileupload.FileUploadUtil
;
import
cn.com.glsx.stdinterface.modules.entity.WorkOrderDocs
;
import
cn.com.glsx.stdinterface.modules.entity.WorkOrderVehicle
;
import
cn.com.glsx.stdinterface.modules.mapper.WorkOrderDocsMapper
;
import
cn.com.glsx.stdinterface.modules.mapper.WorkOrderMapper
;
import
cn.com.glsx.stdinterface.modules.mapper.WorkOrderVehicleMapper
;
import
cn.com.glsx.stdinterface.modules.model.CompletedWorkOrderModel
;
import
cn.com.glsx.stdinterface.modules.util.fileupload.FileConstants
;
import
cn.com.glsx.stdinterface.modules.util.fileupload.FileUploadServerData
;
import
cn.com.glsx.stdinterface.modules.util.fileupload.FileUploadUtil
;
import
cn.com.glsx.stdinterface.modules.util.ThumbnailatorUtil
;
import
cn.hutool.core.date.StopWatch
;
import
com.glsx.plat.common.utils.DateUtils
;
import
com.glsx.plat.common.utils.HttpUtils
;
...
...
@@ -21,10 +25,7 @@ import javax.annotation.Resource;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
...
...
@@ -39,6 +40,9 @@ public class WorkOrderDocsService {
@Resource
private
WorkOrderDocsMapper
workOrderDocsMapper
;
@Resource
private
WorkOrderVehicleMapper
workOrderVehicleMapper
;
public
void
buildWorkOrderDocsZips
()
{
//获取所有工单
List
<
CompletedWorkOrderModel
>
workOrderList
=
workOrderMapper
.
selectAllCompletedWorkOrders
();
...
...
@@ -56,7 +60,7 @@ public class WorkOrderDocsService {
workOrderDocsMap
.
forEach
((
k
,
v
)
->
{
String
vin
=
vinMap
.
get
(
k
);
try
{
this
.
syncUploadDocsAndZip
(
k
,
vin
,
v
);
this
.
syncUploadDocsAndZip
ByCmd
(
k
,
vin
,
v
);
}
catch
(
Exception
e
)
{
log
.
error
(
"打包上传工单{}图片失败{}"
,
k
,
e
.
getMessage
());
}
...
...
@@ -78,7 +82,7 @@ public class WorkOrderDocsService {
Long
workOrderId
=
workOrder
.
getWorkOrderId
();
String
vin
=
workOrder
.
getVin
();
try
{
this
.
syncUploadDocsAndZip
(
workOrderId
,
vin
,
workOrderDocsList
);
this
.
syncUploadDocsAndZip
ByCmd
(
workOrderId
,
vin
,
workOrderDocsList
);
}
catch
(
Exception
e
)
{
log
.
error
(
"打包上传工单{}图片失败{}"
,
workOrderId
,
e
.
getMessage
());
}
...
...
@@ -87,17 +91,20 @@ public class WorkOrderDocsService {
@Async
public
void
syncUploadDocsAndZip
(
Long
workOrderId
,
String
vin
,
List
<
WorkOrderDocs
>
docsList
)
throws
IOException
{
if
(
CollectionUtils
.
isNotEmpty
(
docsList
))
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
final
ZipOutputStream
zout
=
new
ZipOutputStream
(
baos
);
//车架号目录
String
vinPath
=
vin
+
File
.
separator
;
if
(
CollectionUtils
.
isEmpty
(
docsList
))
{
log
.
info
(
"workOrderId:{},vin:{},docsList is null"
,
workOrderId
,
vin
);
return
;
}
//车架号目录
StopWatch
sw
=
new
StopWatch
();
sw
.
start
(
"下载并压缩图片文件"
);
log
.
info
(
"开始下载并压缩图片文件==="
);
String
vinPath
=
vin
+
File
.
separator
;
try
(
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
final
ZipOutputStream
zout
=
new
ZipOutputStream
(
baos
))
{
byte
[]
bytes
;
for
(
WorkOrderDocs
doc
:
docsList
)
{
byte
[]
bytes
=
HttpUtils
.
downloadBytes
(
doc
.
getPath
());
String
filename
=
doc
.
getName
();
if
(
StringUtils
.
isNullOrEmpty
(
filename
))
{
filename
=
doc
.
getPath
().
substring
(
doc
.
getPath
().
lastIndexOf
(
"/"
));
...
...
@@ -107,21 +114,36 @@ public class WorkOrderDocsService {
filename
=
filename
+
suffix
;
}
}
if
(
StringUtils
.
isNotEmpty
(
doc
.
getCompressUrl
()))
{
bytes
=
HttpUtils
.
downloadBytes
(
doc
.
getCompressUrl
());
}
else
{
bytes
=
ThumbnailatorUtil
.
compressImageByUrl
(
doc
.
getPath
());
FileUploadServerData
uploadServerData
=
FileUploadUtil
.
uploadToFileSystem
(
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
)),
bytes
,
FileConstants
.
JPG_TYPE
);
String
compressImgUrl
=
uploadServerData
.
getSecureUrl
();
doc
.
setCompressUrl
(
compressImgUrl
);
workOrderDocsMapper
.
saveOrUpdate
(
doc
);
}
if
(
bytes
==
null
)
{
continue
;
}
zout
.
putNextEntry
(
new
ZipEntry
(
vinPath
+
filename
));
zout
.
write
(
bytes
,
0
,
bytes
.
length
);
zout
.
closeEntry
();
}
zout
.
close
();
log
.
info
(
"打包工单{}图片完成,begin uploading..."
,
workOrderId
);
sw
.
stop
();
log
.
info
(
"打包工单{}图片完成,begin uploading...{}耗时:{}s"
,
workOrderId
,
sw
.
getLastTaskName
(),
sw
.
getLastTaskTimeMillis
()
/
1000
);
StopWatch
sw
=
new
StopWatch
();
sw
.
start
();
sw
.
start
(
"上传图片压缩文件"
);
String
filename
=
vin
+
"_"
+
DateUtils
.
formatSerial
(
new
Date
());
FileUploadServerData
uploadServerData
=
FileUploadUtil
.
uploadToFileSystem
(
filename
,
baos
.
toByteArray
(),
FileConstants
.
ZIP_TYPE
);
String
zipPath
=
uploadServerData
.
getUrl
();
baos
.
close
();
sw
.
stop
();
log
.
info
(
"上传工单{}图片路径:{},
耗时:{}s"
,
workOrderId
,
zipPath
,
sw
.
getTotalTimeSeconds
());
log
.
info
(
"上传工单{}图片路径:{},
{}耗时:{}s,总耗时:{}"
,
workOrderId
,
zipPath
,
sw
.
getLastTaskName
(),
sw
.
getLastTaskTimeMillis
()
/
1000
,
sw
.
getTotalTimeSeconds
());
if
(
StringUtils
.
isNullOrEmpty
(
zipPath
))
{
log
.
warn
(
"上传工单{}图片失败"
,
workOrderId
);
...
...
@@ -137,7 +159,97 @@ public class WorkOrderDocsService {
workOrderDocs
.
setPath
(
zipPath
);
workOrderDocs
.
setType
(
"30"
);
workOrderDocsMapper
.
saveOrUpdate
(
workOrderDocs
);
}
catch
(
Exception
e
)
{
log
.
error
(
"压缩图片异常:"
+
e
.
getMessage
(),
e
);
}
}
@Async
public
void
syncUploadDocsAndZipByCmd
(
Long
workOrderId
,
String
vin
,
List
<
WorkOrderDocs
>
docsList
)
throws
IOException
{
if
(
CollectionUtils
.
isEmpty
(
docsList
))
{
log
.
info
(
"workOrderId:{},vin:{},docsList is null"
,
workOrderId
,
vin
);
return
;
}
StopWatch
sw
=
new
StopWatch
();
sw
.
start
(
"下载并压缩图片文件"
);
log
.
info
(
"下载并压缩图片文件==="
);
byte
[]
bytes
;
for
(
WorkOrderDocs
doc
:
docsList
)
{
if
(
StringUtils
.
isEmpty
(
doc
.
getCompressUrl
()))
{
bytes
=
ThumbnailatorUtil
.
compressImageByUrl
(
doc
.
getPath
());
String
filename
=
doc
.
getName
();
if
(
StringUtils
.
isNullOrEmpty
(
filename
))
{
filename
=
doc
.
getPath
().
substring
(
doc
.
getPath
().
lastIndexOf
(
"/"
));
}
else
{
if
(!
filename
.
contains
(
"."
))
{
String
suffix
=
doc
.
getPath
().
substring
(
doc
.
getPath
().
lastIndexOf
(
"."
));
filename
=
filename
+
suffix
;
}
}
FileUploadServerData
uploadServerData
=
FileUploadUtil
.
uploadToFileSystem
(
filename
.
substring
(
0
,
doc
.
getName
().
lastIndexOf
(
"."
)),
bytes
,
FileConstants
.
JPG_TYPE
);
String
filePath
=
uploadServerData
.
getSecureUrl
();
//入库
doc
.
setCompressUrl
(
filePath
);
workOrderDocsMapper
.
saveOrUpdate
(
doc
);
}
}
sw
.
stop
();
log
.
info
(
"上传{}图片完成,{}耗时:{}s,begin zip..."
,
workOrderId
,
sw
.
getLastTaskName
(),
sw
.
getLastTaskTimeMillis
()
/
1000
);
sw
.
start
(
"压缩图片"
);
String
filename
=
vin
+
"_"
+
DateUtils
.
formatSerial
(
new
Date
());
List
<
String
>
filePathList
=
docsList
.
stream
().
map
(
WorkOrderDocs:
:
getCompressUrl
).
filter
(
StringUtils:
:
isNotEmpty
).
collect
(
Collectors
.
toList
());
FileUploadServerData
uploadServerData
=
FileUploadUtil
.
zipByCmdToFileSystem
(
filename
,
filePathList
);
String
zipPath
=
uploadServerData
.
getSecureUrl
();
sw
.
stop
();
log
.
info
(
"压缩工单{}图片路径:{},{}耗时:{}s,总耗时:{}s"
,
workOrderId
,
zipPath
,
sw
.
getLastTaskName
(),
sw
.
getLastTaskTimeMillis
()
/
1000
,
sw
.
getTotalTimeSeconds
());
if
(
StringUtils
.
isNullOrEmpty
(
zipPath
))
{
log
.
warn
(
"上传工单{}图片失败"
,
workOrderId
);
return
;
}
WorkOrderDocs
workOrderDocs
=
workOrderDocsMapper
.
selectByOrderIdAndType
(
workOrderId
,
30
);
if
(
workOrderDocs
==
null
)
{
workOrderDocs
=
new
WorkOrderDocs
();
}
workOrderDocs
.
setWorkOrderId
(
workOrderId
);
workOrderDocs
.
setName
(
zipPath
.
substring
(
zipPath
.
lastIndexOf
(
"/"
)
+
1
));
workOrderDocs
.
setPath
(
zipPath
);
workOrderDocs
.
setCompressUrl
(
zipPath
);
workOrderDocs
.
setType
(
"30"
);
workOrderDocsMapper
.
saveOrUpdate
(
workOrderDocs
);
}
/**
* 压缩图片
*
* @param workOrderId 工单ID
*/
public
void
compressImages
(
Long
workOrderId
)
throws
Exception
{
if
(
workOrderId
==
null
)
{
List
<
Long
>
workOrderIdList
=
workOrderDocsMapper
.
selectAllWorkOrderId
();
log
.
info
(
"workOrderIdList==="
+
workOrderIdList
.
size
());
for
(
Long
aLong
:
workOrderIdList
)
{
getDocsByWorkOrderId
(
aLong
);
}
}
else
{
getDocsByWorkOrderId
(
workOrderId
);
}
}
/**
* 通过工单ID获取图片数据
*
* @param workOrderId 工单ID
*/
private
void
getDocsByWorkOrderId
(
Long
workOrderId
)
throws
Exception
{
List
<
WorkOrderDocs
>
docsList
=
workOrderDocsMapper
.
selectByOrderId
(
workOrderId
);
WorkOrderVehicle
workOrderVehicle
=
workOrderVehicleMapper
.
selectOneByOrderId
(
workOrderId
);
syncUploadDocsAndZipByCmd
(
workOrderId
,
workOrderVehicle
.
getVin
(),
docsList
);
}
}
src/main/java/cn/com/glsx/stdinterface/modules/service/WorkOrderService.java
View file @
825e1cd
...
...
@@ -1711,8 +1711,8 @@ public class WorkOrderService {
//2022/3/28 异步上传图片压缩包
WorkOrderVehicle
workOrderVehicle
=
workOrderVehicleMapper
.
selectOneByOrderId
(
workOrderId
);
try
{
workOrderDocsService
.
syncUploadDocsAndZip
(
workOrderId
,
workOrderVehicle
!=
null
?
workOrderVehicle
.
getVin
()
:
"UnknownVin"
,
newWorkOrderDocsList
);
}
catch
(
IO
Exception
e
)
{
workOrderDocsService
.
syncUploadDocsAndZip
ByCmd
(
workOrderId
,
workOrderVehicle
!=
null
?
workOrderVehicle
.
getVin
()
:
"UnknownVin"
,
newWorkOrderDocsList
);
}
catch
(
Exception
e
)
{
log
.
error
(
"打包上传工单{}图片失败{}"
,
workOrderId
,
e
.
getMessage
());
}
}
...
...
src/main/java/cn/com/glsx/stdinterface/modules/util/ThumbnailatorUtil.java
0 → 100644
View file @
825e1cd
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
;
import
cn.hutool.core.date.StopWatch
;
import
com.glsx.plat.common.utils.HttpUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
net.coobird.thumbnailator.Thumbnails
;
import
java.io.*
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.Arrays
;
/**
* @author Administrator
* @Title ThumbnailatorUtil.java
* @Package com.hhx.tiger.util.image
* @Description TODO 图片处理工具
* @date 2018年6月27日 下午3:25:37
*/
@Slf4j
public
class
ThumbnailatorUtil
{
/**
* 将图片压缩到1.2M以下
*
* @param filePath
* @param filename
* @param destFilename
*/
public
static
boolean
compressImage
(
String
filePath
,
String
filename
,
String
destFilename
)
{
try
{
double
outputQuality
=
1.0d
;
File
file
=
new
File
(
filePath
+
File
.
separator
+
filename
);
long
fileSize
=
file
.
length
();
if
(
fileSize
>
1.2
*
1024
*
1024
)
{
if
(
fileSize
>=
10
*
1024
*
1024
)
{
outputQuality
=
0.1d
;
}
else
if
(
8
*
1024
*
1024
<=
fileSize
&&
fileSize
<
10
*
1024
*
1024
)
{
outputQuality
=
0.20d
;
}
else
if
(
7
*
1024
*
1024
<=
fileSize
&&
fileSize
<
8
*
1024
*
1024
)
{
outputQuality
=
0.25d
;
}
else
if
(
6
*
1024
*
1024
<=
fileSize
&&
fileSize
<
7
*
1024
*
1024
)
{
outputQuality
=
0.50d
;
}
else
if
(
5
*
1024
*
1024
<=
fileSize
&&
fileSize
<
6
*
1024
*
1024
)
{
outputQuality
=
0.60d
;
}
else
if
(
4
*
1024
*
1024
<=
fileSize
&&
fileSize
<
5
*
1024
*
1024
)
{
outputQuality
=
0.35d
;
}
else
if
(
3
*
1024
*
1024
<=
fileSize
&&
fileSize
<
4
*
1024
*
1024
)
{
outputQuality
=
0.65d
;
}
else
if
(
2
*
1024
*
1024
<=
fileSize
&&
fileSize
<
3
*
1024
*
1024
)
{
outputQuality
=
0.65d
;
}
else
if
(
1.2
*
1024
*
1024
<=
fileSize
&&
fileSize
<
2
*
1024
*
1024
)
{
outputQuality
=
0.75d
;
}
Thumbnails
.
of
(
file
)
.
scale
(
1.0d
)
.
outputQuality
(
outputQuality
)
.
toFile
(
new
File
(
filePath
+
File
.
separator
+
destFilename
));
return
true
;
}
}
catch
(
Exception
e
)
{
log
.
error
(
"图片压缩异常"
+
e
.
getMessage
(),
e
);
}
return
false
;
}
/**
* 将图片压缩到1.2M以下
*
* @param imageUrl
*/
public
static
byte
[]
compressImage
(
String
imageUrl
)
{
try
(
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
())
{
double
outputQuality
=
1.0d
;
URL
url
=
new
URL
(
imageUrl
);
URLConnection
conn
=
url
.
openConnection
();
conn
.
setRequestProperty
(
"Range"
,
"bytes=0-"
);
long
fileSize
=
conn
.
getContentLength
();
log
.
info
(
"fileSize==="
+
fileSize
);
if
(
fileSize
>
1.2
*
1024
*
1024
)
{
if
(
fileSize
>=
10
*
1024
*
1024
)
{
outputQuality
=
0.1d
;
}
else
if
(
8
*
1024
*
1024
<=
fileSize
&&
fileSize
<
10
*
1024
*
1024
)
{
outputQuality
=
0.20d
;
}
else
if
(
7
*
1024
*
1024
<=
fileSize
&&
fileSize
<
8
*
1024
*
1024
)
{
outputQuality
=
0.25d
;
}
else
if
(
6
*
1024
*
1024
<=
fileSize
&&
fileSize
<
7
*
1024
*
1024
)
{
outputQuality
=
0.50d
;
}
else
if
(
5
*
1024
*
1024
<=
fileSize
&&
fileSize
<
6
*
1024
*
1024
)
{
outputQuality
=
0.60d
;
}
else
if
(
4
*
1024
*
1024
<=
fileSize
&&
fileSize
<
5
*
1024
*
1024
)
{
outputQuality
=
0.35d
;
}
else
if
(
3
*
1024
*
1024
<=
fileSize
&&
fileSize
<
4
*
1024
*
1024
)
{
outputQuality
=
0.65d
;
}
else
if
(
2
*
1024
*
1024
<=
fileSize
&&
fileSize
<
3
*
1024
*
1024
)
{
outputQuality
=
0.65d
;
}
else
if
(
1.2
*
1024
*
1024
<=
fileSize
&&
fileSize
<
2
*
1024
*
1024
)
{
outputQuality
=
0.75d
;
}
Thumbnails
.
of
(
url
)
.
scale
(
1.0d
)
.
outputQuality
(
outputQuality
)
.
toOutputStream
(
baos
);
return
baos
.
toByteArray
();
}
else
{
return
HttpUtils
.
downloadBytes
(
imageUrl
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"图片压缩异常"
+
e
.
getMessage
(),
e
);
}
return
null
;
}
/**
* 将图片压缩到1.2M以下
*
* @param imageUrl 图片URl地址
*/
public
static
byte
[]
compressImageByUrl
(
String
imageUrl
)
{
StopWatch
sw
=
new
StopWatch
();
sw
.
start
(
"上传图片压缩文件"
);
byte
[]
bytes
=
compressImage
(
imageUrl
);
sw
.
stop
();
log
.
info
(
"压缩图片路径:{},耗时:{}s"
,
imageUrl
,
sw
.
getTotalTimeSeconds
());
return
bytes
;
}
public
static
void
main
(
String
[]
args
)
throws
FileNotFoundException
{
String
filePath
=
"D:\\"
;
// String filename = "wKgFHWNbGFCAJ3gZAOZukG_6uyQ678.jpg";
// String destFilename = "compressed_" + filename;
// ThumbnailatorUtil.compressImage(filePath, filename, destFilename);
String
url
=
"https://wx.glsx.com.cn/upload/biz/5a9a9ef0-646e-4c9e-8d9e-d07c373d56b7.jpg"
;
FileOutputStream
fos
=
new
FileOutputStream
(
filePath
+
"compressed_"
+
"d07c373d56b7.jpg"
);
compressImage
(
url
);
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
compressImage
(
url
);
byte
[]
compressImageBytes
=
baos
.
toByteArray
();
System
.
out
.
println
(
"compressImageBytes = "
+
Arrays
.
toString
(
compressImageBytes
));
}
}
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileConstants.java
deleted
100644 → 0
View file @
65d25b6
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
fileupload
;
/**
* 上传文件类型常量类
*
* @author yll
*/
public
class
FileConstants
{
/**
* 文件类型
*/
public
static
final
String
EXCEL_TYPE_XLS
=
"xls"
;
public
static
final
String
EXCEL_TYPE_XLSX
=
"xlsx"
;
public
static
final
String
EXCEL_TYPE_COMPLETE
=
"xlsx,xls,csv"
;
public
static
final
String
UPLOAD_FILE_TYPE
=
"txt,doc,docx,pdf,xlsx,xls,pptx,ppt,png,jpg,gif,mp3,mp4,rar,zip"
;
public
static
final
String
PDF_TYPE
=
"pdf"
;
public
static
final
String
PNG_TYPE
=
"png"
;
public
static
final
String
JPG_TYPE
=
"jpg"
;
public
static
final
String
ZIP_TYPE
=
"zip"
;
/**
* 文件服务器地址
*/
public
static
final
String
FILE_UPLOAD_URL
=
"file.upload.string.url"
;
/**
* 文件服务器文件压缩地址
*/
public
static
final
String
FILE_ZIP_URL
=
"file.zip.string.url"
;
}
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileUploadServerData.java
deleted
100644 → 0
View file @
65d25b6
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
fileupload
;
import
lombok.Data
;
import
java.io.Serializable
;
/**
* 上传文件返回结果实体类
*
* @author yll
*/
@SuppressWarnings
(
"serial"
)
@Data
public
class
FileUploadServerData
implements
Serializable
{
public
static
final
int
UPLOAD_SUCCSSS
=
0
;
// "上传文件成功!",
public
static
final
int
UPLOAD_FAILURE
=
1
;
// "上传文件失败!"),
//private static final int UPLOAD_TYPE_ERROR = 2; // "上传文件类型错误!"),
//private static final int UPLOAD_OVERSIZE = 3; // "上传文件过大!"),
//private static final int UPLOAD_ZEROSIZE = 4; // "上传文件为空!"),
//private static final int UPLOAD_NOTFOUND = 5; // "上传文件路径错误!")
private
Integer
status
=
0
;
private
String
message
=
""
;
private
String
url
=
""
;
private
String
systemSign
;
private
String
filePath
;
private
String
param1
;
private
String
param2
;
private
String
param3
;
private
String
secureUrl
;
}
src/main/java/cn/com/glsx/stdinterface/modules/util/fileupload/FileUploadUtil.java
deleted
100644 → 0
View file @
65d25b6
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
fileupload
;
import
com.alibaba.fastjson.JSONArray
;
import
com.glsx.plat.common.utils.DateUtils
;
import
com.glsx.plat.context.utils.PropertiesUtils
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.RestTemplate
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.UUID
;
/**
* 文件上传操作类
*
* @author yll
*/
public
class
FileUploadUtil
{
/**
* 校验文件类型
*
* @param fileType 文件类型
* @return 是否是合法的文件类型
*/
public
static
boolean
verifyFileType
(
String
fileType
)
{
if
(
StringUtils
.
isBlank
(
fileType
))
{
return
false
;
}
List
<
String
>
fileTypes
=
Arrays
.
asList
(
FileConstants
.
UPLOAD_FILE_TYPE
.
split
(
","
));
return
fileTypes
.
contains
(
fileType
);
}
/**
* 将文件上传至文件服务器
*
* @param fileName 文件名 (不需要加文件后缀)
* @param uploadData 长传文件数据
* @param fileType 文件类型
* @return FileUploadServerData
*/
public
static
FileUploadServerData
uploadToFileSystem
(
String
fileName
,
byte
[]
uploadData
,
String
fileType
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<>();
form
.
add
(
"systemSign"
,
"salesexcel"
);
form
.
add
(
"filePath"
,
""
);
form
.
add
(
"param1"
,
fileName
);
form
.
add
(
"param2"
,
""
);
form
.
add
(
"param3"
,
""
);
form
.
add
(
"files"
,
fileType
+
"@"
+
Base64
.
encodeBase64String
(
uploadData
));
return
restTemplate
.
postForObject
(
PropertiesUtils
.
getProperty
(
FileConstants
.
FILE_UPLOAD_URL
),
form
,
FileUploadServerData
.
class
);
}
/**
* 将文件压缩至文件服务器
*
* @param filePathList 文件路径的集合
* @return FileUploadServerData
*/
public
static
FileUploadServerData
zipToFileSystem
(
List
filePathList
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
MultiValueMap
<
String
,
String
>
form
=
new
LinkedMultiValueMap
<>();
form
.
add
(
"systemSign"
,
"salesexcelzip"
);
form
.
add
(
"filePath"
,
""
);
form
.
add
(
"param1"
,
""
);
form
.
add
(
"param2"
,
""
);
form
.
add
(
"param3"
,
""
);
form
.
add
(
"zipfiles"
,
JSONArray
.
toJSON
(
filePathList
).
toString
());
return
restTemplate
.
postForObject
(
PropertiesUtils
.
getProperty
(
FileConstants
.
FILE_ZIP_URL
),
form
,
FileUploadServerData
.
class
);
}
public
static
String
upload
(
InputStream
fis
,
String
savePath
)
throws
IOException
{
StringBuffer
tempFileName
=
new
StringBuffer
(
"pdf"
);
tempFileName
.
append
(
"-"
);
tempFileName
.
append
(
DateUtils
.
format
(
new
Date
(),
"yyMMdd"
));
tempFileName
.
append
(
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
).
substring
(
16
));
tempFileName
.
append
(
".pdf"
);
File
tempPackage
=
new
File
(
savePath
);
if
(!
tempPackage
.
exists
())
{
tempPackage
.
mkdirs
();
}
File
tempFile
=
new
File
(
savePath
,
tempFileName
.
toString
());
byte
[]
tempbytes
=
new
byte
[
1024
];
int
byteread
=
0
;
FileOutputStream
fos
=
new
FileOutputStream
(
tempFile
);
while
((
byteread
=
fis
.
read
(
tempbytes
))
!=
-
1
)
{
fos
.
write
(
tempbytes
,
0
,
byteread
);
fos
.
flush
();
}
fos
.
close
();
fis
.
close
();
return
tempFileName
.
toString
();
}
}
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/DownFile.java
0 → 100644
View file @
825e1cd
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
multithreaded
;
import
cn.hutool.core.date.DateUtil
;
import
org.apache.commons.io.FileUtils
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.Date
;
public
class
DownFile
{
private
URL
fileUrl
;
// 文件下载路径
private
int
threadCount
;
// 文件下载的线程数
private
int
startPos
;
// 每个线程下载文件的开始位置
private
int
size
;
// 每个线程下载文件的长度
private
int
fileLength
;
// 文件总程度
private
String
pathName
;
// 下载的文件路径(包含文件名)
private
Downthread
[]
tDownthreads
;
// 线程数组
public
DownFile
(
URL
url
,
int
threadCount
,
String
pathName
)
throws
IOException
{
fileUrl
=
url
;
this
.
threadCount
=
threadCount
;
this
.
pathName
=
pathName
;
init
();
}
/**
* 初始化
*
* @throws IOException
*/
private
void
init
()
throws
IOException
{
tDownthreads
=
new
Downthread
[
threadCount
];
HttpURLConnection
conn
=
(
HttpURLConnection
)
fileUrl
.
openConnection
();
conn
.
setConnectTimeout
(
5000
);
conn
.
setRequestMethod
(
"GET"
);
conn
.
setRequestProperty
(
"connection"
,
"keep-alive"
);
fileLength
=
conn
.
getContentLength
();
System
.
out
.
println
(
"文件长度"
+
fileLength
);
size
=
fileLength
/
threadCount
;
System
.
out
.
println
(
"每个下载量=="
+
size
);
conn
.
disconnect
();
// 断开链接
}
public
URL
getFileUrl
()
{
return
fileUrl
;
}
public
int
getThreadCount
()
{
return
this
.
threadCount
;
}
/**
* 开始下载
*/
public
void
startDown
()
{
for
(
int
i
=
0
;
i
<
threadCount
;
i
++)
{
try
{
RandomAccessFile
raFile
=
new
RandomAccessFile
(
pathName
,
"rw"
);
tDownthreads
[
i
]
=
new
Downthread
(
i
*
size
,
raFile
,
i
);
tDownthreads
[
i
].
start
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
/**
* 下载线程类
*
* @author luweicheng
*/
class
Downthread
extends
Thread
{
private
int
startPos
;
// 开始的位置
private
InputStream
is
;
private
RandomAccessFile
raFile
;
private
int
length
;
// 下载的文件长度
private
int
flag
;
// 线程标志
public
Downthread
(
int
startPos
,
RandomAccessFile
raFile
,
int
i
)
{
this
.
startPos
=
startPos
;
this
.
raFile
=
raFile
;
flag
=
i
;
}
@Override
public
void
run
()
{
try
{
HttpURLConnection
connection
=
(
HttpURLConnection
)
fileUrl
.
openConnection
();
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestProperty
(
"connection"
,
"keep-alive"
);
connection
.
setConnectTimeout
(
5
*
1000
);
is
=
connection
.
getInputStream
();
is
.
skip
(
startPos
);
raFile
.
seek
(
startPos
);
byte
[]
buf
=
new
byte
[
8
*
1024
];
int
hasread
=
0
;
// 读出的字节数
// 将位置在 startPos - startPos 位置的数据读出写入
while
(
length
<
size
&&
(
hasread
=
is
.
read
(
buf
))
!=
-
1
)
{
raFile
.
write
(
buf
,
0
,
hasread
);
length
+=
hasread
;
System
.
out
.
println
(
"*****线程"
+
flag
+
"下载了*********"
+
length
+
";耗时:"
+
DateUtil
.
formatDateTime
(
new
Date
()));
}
System
.
out
.
println
(
"*******线程"
+
flag
+
"下载完成*********"
+
";耗时:"
+
DateUtil
.
formatDateTime
(
new
Date
()));
}
catch
(
IOException
e
)
{
}
finally
{
try
{
is
.
close
();
raFile
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
String
uri
=
"https://qcwx.glsx.com.cn/upload-dev/biz/eea198c8-1e10-403b-bded-58fb9c3b8151.jpg"
;
String
pathName
=
"D:\\data\\a.jpg"
;
DownFile
downFile
=
new
DownFile
(
new
URL
(
"https://qcwx.glsx.com.cn/upload-dev/biz/eea198c8-1e10-403b-bded-58fb9c3b8151.jpg"
),
5
,
pathName
);
downFile
.
startDown
();
Thread
.
sleep
(
60000
);
}
}
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/MultipleThreadDownloadManager.java
0 → 100644
View file @
825e1cd
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
multithreaded
;
import
cn.hutool.core.date.StopWatch
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.io.FileUtils
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
@Slf4j
public
class
MultipleThreadDownloadManager
implements
Runnable
{
private
String
uri
;
private
File
target
;
public
MultipleThreadDownloadManager
(
String
uri
,
File
target
)
{
this
.
target
=
target
;
this
.
uri
=
uri
;
}
/**
* 开始下载
*/
public
void
start
()
{
if
(
target
.
exists
()
==
false
)
{
try
{
target
.
createNewFile
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
new
Thread
(
this
).
start
();
}
/**
* 根据文件总大小计算线程数量
*
* @param totalSize
* @return
*/
public
int
threadCount
(
int
totalSize
)
{
// if (totalSize < 30 * 2014 * 1024) {
// return 1;
// }
return
30
;
}
@Override
public
void
run
()
{
StopWatch
sw
=
new
StopWatch
();
sw
.
start
();
//获取文件总大小
int
totalSize
=
0
;
try
{
HttpURLConnection
connection
=
(
HttpURLConnection
)
new
URL
(
uri
).
openConnection
();
connection
.
connect
();
int
contentLength
=
connection
.
getContentLength
();
totalSize
=
contentLength
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//将文件分片并分开下载
int
threadCount
=
threadCount
(
totalSize
);
int
perThreadSize
=
totalSize
/
threadCount
;
//每一个线程分到的任务下载量
int
id
=
0
;
int
from
=
0
,
to
=
0
;
while
(
totalSize
>
0
)
{
id
++;
//计算分片
to
=
totalSize
;
from
=
totalSize
-
perThreadSize
;
if
(
from
<
0
)
{
from
=
0
;
}
totalSize
-=
perThreadSize
;
//开始下载
UnitDownloader
downloader
=
new
UnitDownloader
(
from
,
to
,
target
,
uri
,
id
);
new
Thread
(
downloader
).
start
();
}
sw
.
stop
();
log
.
info
(
"耗时:"
+
sw
.
getTotalTimeSeconds
()
+
"s"
);
}
public
static
void
main
(
String
[]
args
)
{
String
uri
=
"https://qcwx.glsx.com.cn/upload-dev/biz/eea198c8-1e10-403b-bded-58fb9c3b8151.jpg"
;
File
target
=
FileUtils
.
getFile
(
"D:\\data\\yang.jpg"
);
MultipleThreadDownloadManager
multipleThreadDownloadManager
=
new
MultipleThreadDownloadManager
(
uri
,
target
);
multipleThreadDownloadManager
.
start
();
try
{
Thread
.
sleep
(
600000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/main/java/cn/com/glsx/stdinterface/modules/util/multithreaded/UnitDownloader.java
0 → 100644
View file @
825e1cd
package
cn
.
com
.
glsx
.
stdinterface
.
modules
.
util
.
multithreaded
;
import
cn.hutool.core.date.DateUtil
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.RandomAccessFile
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.Date
;
public
class
UnitDownloader
implements
Runnable
{
private
int
from
;
private
int
to
;
private
File
target
;
private
String
uri
;
private
int
id
;
public
UnitDownloader
(
int
from
,
int
to
,
File
target
,
String
uri
,
int
id
)
{
this
.
from
=
from
;
this
.
to
=
to
;
this
.
target
=
target
;
this
.
uri
=
uri
;
this
.
id
=
id
;
}
public
int
getFrom
()
{
return
from
;
}
public
int
getTo
()
{
return
to
;
}
@Override
public
void
run
()
{
//download and save data
try
{
HttpURLConnection
connection
=
(
HttpURLConnection
)
new
URL
(
uri
).
openConnection
();
connection
.
setRequestProperty
(
"Range"
,
"bytes="
+
from
+
"-"
+
to
);
connection
.
connect
();
int
totalSize
=
connection
.
getContentLength
();
InputStream
inputStream
=
connection
.
getInputStream
();
RandomAccessFile
randomAccessFile
=
new
RandomAccessFile
(
target
,
"rw"
);
randomAccessFile
.
seek
(
from
);
byte
[]
buffer
=
new
byte
[
1024
*
1024
];
int
readCount
=
inputStream
.
read
(
buffer
,
0
,
buffer
.
length
);
while
(
readCount
>
0
)
{
totalSize
-=
readCount
;
System
.
out
.
println
(
"分片:"
+
this
.
id
+
"的剩余:"
+
totalSize
+
"时间:"
+
DateUtil
.
formatDateTime
(
new
Date
()));
randomAccessFile
.
write
(
buffer
,
0
,
readCount
);
readCount
=
inputStream
.
read
(
buffer
,
0
,
buffer
.
length
);
}
inputStream
.
close
();
randomAccessFile
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/main/resources/mapper/WorkOrderDocsMapper.xml
View file @
825e1cd
...
...
@@ -9,6 +9,7 @@
<result
column=
"workorder_id"
jdbcType=
"INTEGER"
property=
"workOrderId"
/>
<result
column=
"name"
jdbcType=
"VARCHAR"
property=
"name"
/>
<result
column=
"path"
jdbcType=
"VARCHAR"
property=
"path"
/>
<result
column=
"compress_url"
jdbcType=
"VARCHAR"
property=
"compressUrl"
/>
<result
column=
"type"
jdbcType=
"VARCHAR"
property=
"type"
/>
<result
column=
"del_flag"
jdbcType=
"TINYINT"
property=
"delFlag"
/>
<result
column=
"created_date"
jdbcType=
"TIMESTAMP"
property=
"createdDate"
/>
...
...
@@ -20,7 +21,8 @@
<!--
WARNING - @mbg.generated
-->
id, workorder_id, `name`, `path`, `type`, del_flag, created_date, created_by, updated_date, updated_by
id, workorder_id, `name`, `path`, `compress_url`, `type`, del_flag, created_date, created_by, updated_date,
updated_by
</sql>
<select
id=
"selectByOrderId"
resultMap=
"BaseResultMap"
>
...
...
@@ -32,6 +34,14 @@
and type
<![CDATA[ <> ]]>
30
</select>
<select
id=
"selectAllImages"
resultMap=
"BaseResultMap"
>
select
<include
refid=
"Base_Column_List"
/>
from t_workorder_documents
where del_flag = 0
and type
<![CDATA[ <> ]]>
30
</select>
<select
id=
"selectByOrderIdAndType"
resultMap=
"BaseResultMap"
>
select
<include
refid=
"Base_Column_List"
/>
...
...
@@ -54,12 +64,14 @@
order by workorder_id desc
</select>
<insert
id=
"saveOrUpdate"
useGeneratedKeys=
"true"
keyProperty=
"id"
parameterType=
"cn.com.glsx.stdinterface.modules.entity.WorkOrderDocs"
>
INSERT INTO t_workorder_documents (id, workorder_id, `name`, `path`, `type`, del_flag, created_date)
VALUES (#{id}, #{workOrderId}, #{name}, #{path}, #{type}, 0, sysdate()) ON DUPLICATE KEY
<insert
id=
"saveOrUpdate"
useGeneratedKeys=
"true"
keyProperty=
"id"
parameterType=
"cn.com.glsx.stdinterface.modules.entity.WorkOrderDocs"
>
INSERT INTO t_workorder_documents (id, workorder_id, `name`, `path`, `compress_url`, `type`, del_flag, created_date)
VALUES (#{id}, #{workOrderId}, #{name}, #{path}, #{compressUrl}, #{type}, 0, sysdate()) ON DUPLICATE KEY
UPDATE
name = #{name,jdbcType=VARCHAR},
path = #{path,jdbcType=VARCHAR},
compress_url = #{compressUrl,jdbcType=VARCHAR},
updated_date = sysdate()
</insert>
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment