Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-fb
Overview
Overview
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
王衍超
cloud-fb
Commits
8514b7b9
Commit
8514b7b9
authored
Jun 21, 2021
by
shiwenbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提供一个根据文件id批量下载文件的接口
parent
3a524c53
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
0 deletions
+114
-0
DinasCommonController.java
...in/java/com/xyst/dinas/biz/web/DinasCommonController.java
+114
-0
No files found.
backend/xyst.dinas.biz/src/main/java/com/xyst/dinas/biz/web/DinasCommonController.java
View file @
8514b7b9
package
com
.
xyst
.
dinas
.
biz
.
web
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.UUID
;
import
java.util.zip.CRC32
;
import
java.util.zip.CheckedOutputStream
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
import
org.json.JSONObject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.beecode.bap.attachment.AttachmentInfo
;
import
com.beecode.bap.attachment.exception.AttachmentDataDownLoadException
;
import
com.beecode.bap.attachment.service.AttachmentService
;
import
com.beecode.bap.department.service.DepartmentService
;
import
com.beecode.bap.staff.Staff
;
import
com.beecode.bap.staff.service.StaffService
;
...
...
@@ -40,6 +61,11 @@ public class DinasCommonController {
@Autowired
public
PlanningCycleService
planningCycleService
;
@Autowired
public
AttachmentService
attachmentService
;
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
/**
* @Description: 根据部门id查询全公司及其该部门的子部门(包括该部门自身)
...
...
@@ -200,4 +226,92 @@ public class DinasCommonController {
@DateTimeFormat
(
pattern
=
"yyyy-MM-dd"
)
@PathVariable
Date
endTime
)
{
return
planningCycleService
.
getPlanningCycleListByDay
(
startTime
,
endTime
);
}
@RequestMapping
(
value
=
"files/all"
,
method
=
RequestMethod
.
GET
)
public
ResponseEntity
<
Resource
>
downloadAllFiles
(
@RequestParam
(
name
=
"fileIds"
)
List
<
UUID
>
fileIds
,
@RequestHeader
(
name
=
HttpHeaders
.
USER_AGENT
,
required
=
false
)
String
userAgent
)
{
List
<
AttachmentInfo
>
attachmentInfos
=
new
ArrayList
<
AttachmentInfo
>();
for
(
int
i
=
0
;
i
<
fileIds
.
size
();
i
++)
{
Optional
<
AttachmentInfo
>
attachmentInfo
=
attachmentService
.
find
(
fileIds
.
get
(
i
));
if
(
attachmentInfo
.
isPresent
())
{
attachmentInfos
.
add
(
attachmentInfo
.
get
());
}
}
if
(
attachmentInfos
!=
null
&&
attachmentInfos
.
size
()
>
0
)
{
ZipOutputStream
zipOutputStream
=
null
;
try
{
ByteArrayOutputStream
arrayOutputStream
=
new
ByteArrayOutputStream
();
CheckedOutputStream
checkedOutputStream
=
new
CheckedOutputStream
(
arrayOutputStream
,
new
CRC32
());
zipOutputStream
=
new
ZipOutputStream
(
checkedOutputStream
);
for
(
AttachmentInfo
attachmentInfo
:
attachmentInfos
)
{
if
(
fileIds
!=
null
&&
!
fileIds
.
contains
(
attachmentInfo
.
getId
()))
{
continue
;
}
ZipEntry
zipEntry
=
new
ZipEntry
(
attachmentInfo
.
getFileName
());
zipOutputStream
.
putNextEntry
(
zipEntry
);
InputStream
inputStream
=
attachmentService
.
getInputStream
(
attachmentInfo
.
getId
());
int
count
=
-
1
;
byte
[]
temp
=
new
byte
[
1024
];
try
{
while
((
count
=
inputStream
.
read
(
temp
))
!=
-
1
)
{
zipOutputStream
.
write
(
temp
,
0
,
count
);
}
zipOutputStream
.
closeEntry
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"数据下载异常: 数据读写异常"
,
e
);
throw
new
AttachmentDataDownLoadException
(
"数据下载异常: 数据读写异常"
,
e
);
}
finally
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"数据下载异常: 流关闭异常!"
,
e
);
throw
new
AttachmentDataDownLoadException
(
"数据下载异常: 流关闭异常!"
,
e
);
}
}
}
zipOutputStream
.
flush
();
zipOutputStream
.
finish
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
add
(
"charset"
,
"utf-8"
);
String
fileName
=
new
String
(
"附件.zip"
.
getBytes
(
"utf-8"
),
"iso-8859-1"
);
if
(
isIE
(
userAgent
))
{
fileName
=
URLEncoder
.
encode
(
fileName
,
"UTF-8"
);
}
headers
.
add
(
"Content-Disposition"
,
"attachment;filename=\""
+
fileName
+
"\""
);
Resource
resource
=
new
ByteArrayResource
(
arrayOutputStream
.
toByteArray
());
return
ResponseEntity
.
ok
().
headers
(
headers
).
contentType
(
MediaType
.
APPLICATION_OCTET_STREAM
)
.
body
(
resource
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"数据下载异常: 创建流错误!"
,
e
);
throw
new
AttachmentDataDownLoadException
(
"数据下载异常: 创建流错误!"
,
e
);
}
finally
{
try
{
if
(
zipOutputStream
!=
null
)
{
zipOutputStream
.
close
();
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"数据下载异常: 流关闭异常!"
,
e
);
throw
new
AttachmentDataDownLoadException
(
"数据下载异常: 流关闭异常!"
,
e
);
}
}
}
else
{
logger
.
warn
(
"没有找到要下载的文件! "
);
}
return
null
;
}
/**
* 判断是否是ie
*
* @return
*/
private
boolean
isIE
(
String
userAgent
)
{
if
(
userAgent
!=
null
)
{
userAgent
=
userAgent
.
toLowerCase
();
return
userAgent
.
contains
(
"msie"
)
||
userAgent
.
contains
(
"trident"
)
||
userAgent
.
contains
(
"edge"
);
}
return
false
;
}
}
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