Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
email
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈天仲
email
Commits
dd2c5cdc
Commit
dd2c5cdc
authored
Jul 09, 2023
by
chentianzhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载和预览前判断文件是否能到达
parent
3fa5bf89
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
7 deletions
+38
-7
src/main/java/com/zq/email/controller/EmailAttachController.java
+28
-7
src/main/java/com/zq/email/service/impl/EmailAttachServiceImpl.java
+10
-0
No files found.
src/main/java/com/zq/email/controller/EmailAttachController.java
View file @
dd2c5cdc
...
...
@@ -28,6 +28,7 @@ import lombok.extern.slf4j.Slf4j;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.util.Assert
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -69,6 +70,26 @@ public class EmailAttachController {
return
ResultVo
.
success
(
emailAttachService
.
findByContentId
(
vo
.
getContentId
()));
}
@ApiOperation
(
"前端调用下载前,判断文件是否是否可以下载"
)
@AnonymousGetMapping
(
"/preDownload/{id}"
)
public
ResultVo
preDownload
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@PathVariable
(
"id"
)
Long
id
)
{
EmailAttach
emailAttach
=
emailAttachService
.
getById
(
id
);
AssertUtils
.
notNull
(
emailAttach
,
"获取附件信息失败"
);
String
download
=
emailAttachService
.
getDownloadPath
(
emailAttach
);
AssertUtils
.
hasText
(
download
,
"下载地址为空"
);
log
.
info
(
"下载文件地址:"
+
download
);
try
{
byte
[]
bytes
=
HttpUtil
.
downloadBytes
(
download
);
AssertUtils
.
isTrue
(
ArrayUtil
.
isNotEmpty
(
bytes
),
"下载文件失败,文件为空"
);
return
ResultVo
.
success
();
}
catch
(
Exception
e
)
{
log
.
error
(
"下载文件失败:"
+
e
.
getLocalizedMessage
(),
e
);
return
ResultVo
.
fail
(
"下载文件失败,文件可能不存在"
);
}
}
@ApiOperation
(
"pc统一下载附件和移动端旧附件下载"
)
@AnonymousGetMapping
(
"/download/{id}"
)
public
void
download
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@PathVariable
(
"id"
)
Long
id
)
{
...
...
@@ -78,16 +99,18 @@ public class EmailAttachController {
String
download
=
emailAttachService
.
getDownloadPath
(
emailAttach
);
AssertUtils
.
hasText
(
download
,
"下载地址为空"
);
log
.
info
(
"下载文件地址:"
+
download
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
try
{
HttpUtil
.
download
(
download
,
outputStream
,
true
);
byte
[]
bytes
=
outputStream
.
toByteArray
();
byte
[]
bytes
=
HttpUtil
.
downloadBytes
(
download
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename*=UTF-8''"
+
URLEncoder
.
encode
(
emailAttach
.
getAttachName
(),
"UTF-8"
));
response
.
setCharacterEncoding
(
"UTF-8"
);
ServletUtil
.
write
(
response
,
IoUtil
.
toStream
(
bytes
),
"application/octet-stream"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"下载文件失败:"
+
e
.
getLocalizedMessage
(),
e
);
AssertUtils
.
isTrue
(
false
,
"下载文件失败,文件可能不存在"
);
if
(
profile
.
contains
(
"wan"
)){
AssertUtils
.
isTrue
(
false
,
"文件超过安全时间,禁止从外网访问下载"
);
}
else
{
AssertUtils
.
isTrue
(
false
,
"下载文件失败,文件可能不存在"
);
}
}
}
...
...
@@ -101,10 +124,8 @@ public class EmailAttachController {
String
download
=
emailAttachService
.
getDownloadPath
(
emailAttach
);
AssertUtils
.
hasText
(
download
,
"下载地址为空"
);
log
.
info
(
"下载文件地址:"
+
download
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
try
{
HttpUtil
.
download
(
download
,
outputStream
,
true
);
byte
[]
bytes
=
outputStream
.
toByteArray
();
byte
[]
bytes
=
HttpUtil
.
downloadBytes
(
download
);
ServletUtil
.
write
(
response
,
IoUtil
.
toStream
(
bytes
),
"application/octet-stream"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"下载文件失败:"
+
e
.
getLocalizedMessage
(),
e
);
...
...
src/main/java/com/zq/email/service/impl/EmailAttachServiceImpl.java
View file @
dd2c5cdc
...
...
@@ -2,8 +2,10 @@ package com.zq.email.service.impl;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.ArrayUtil
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.http.HttpUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.zq.common.utils.AssertUtils
;
import
com.zq.common.utils.TokenUtils
;
...
...
@@ -167,6 +169,14 @@ public class EmailAttachServiceImpl extends ServiceImpl<EmailAttachMapper, Email
String
downloadPath
=
this
.
getDownloadPath
(
emailAttach
);
String
suffix
=
emailAttach
.
getAttachName
().
substring
(
emailAttach
.
getAttachName
().
lastIndexOf
(
"."
)
+
1
);
AssertUtils
.
hasText
(
suffix
,
"错误的文件名称("
+
emailAttach
.
getAttachName
()+
"),导致获取文件类型失败"
);
log
.
info
(
"wps预览文件地址: "
+
downloadPath
);
try
{
byte
[]
bytes
=
HttpUtil
.
downloadBytes
(
downloadPath
);
AssertUtils
.
isTrue
(
ArrayUtil
.
isNotEmpty
(
bytes
),
"下载文件失败,文件为空"
);
}
catch
(
Exception
e
){
AssertUtils
.
isTrue
(
false
,
"下载文件失败,文件可能不存在"
);
}
FileViewVo
viewVo
=
new
FileViewVo
();
viewVo
.
setFileId
(
IdUtil
.
fastSimpleUUID
());
...
...
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