Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b58e6b93
authored
Sep 19, 2025
by
Jony.L
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
发票预览
parent
3a0a1c0e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
7 deletions
+12
-7
computility-module-infra/src/main/java/com/luhu/computility/module/infra/controller/admin/file/FileController.java
+12
-7
No files found.
computility-module-infra/src/main/java/com/luhu/computility/module/infra/controller/admin/file/FileController.java
View file @
b58e6b93
...
...
@@ -31,6 +31,9 @@ import java.io.FileInputStream;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.URL
;
import
java.net.URLEncoder
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
import
static
com
.
luhu
.
computility
.
framework
.
common
.
pojo
.
CommonResult
.
success
;
...
...
@@ -129,20 +132,20 @@ public class FileController {
@Operation
(
summary
=
"PDF 文件预览"
)
@PermitAll
public
void
previewPdf
(
HttpServletResponse
response
,
String
url
)
throws
IOException
{
File
file
=
new
File
(
url
);
if
(!
file
.
exists
())
{
response
.
setStatus
(
HttpServletResponse
.
SC_NOT_FOUND
);
if
(
url
==
null
||
url
.
isEmpty
())
{
response
.
setStatus
(
HttpServletResponse
.
SC_BAD_REQUEST
);
return
;
}
// 设置响应头(浏览器内嵌预览)
response
.
setContentType
(
"application/pdf"
);
// inline 表示浏览器内嵌预览,attachment 表示下载
response
.
setHeader
(
"Content-Disposition"
,
"inline; filename=\""
+
file
.
getName
()
+
"\""
);
response
.
setHeader
(
"Cache-Control"
,
"no-cache, no-store, must-revalidate"
);
// 避免缓存
response
.
setHeader
(
"Content-Disposition"
,
"inline; filename=\""
+
URLEncoder
.
encode
(
"preview.pdf"
,
StandardCharsets
.
UTF_8
.
name
())
+
"\""
);
response
.
setHeader
(
"Cache-Control"
,
"no-cache, no-store, must-revalidate"
);
response
.
setHeader
(
"Pragma"
,
"no-cache"
);
response
.
setHeader
(
"Expires"
,
"0"
);
try
(
InputStream
inputStream
=
new
FileInputStream
(
file
);
// 用 HttpURLConnection 读取远程 PDF 流
try
(
InputStream
inputStream
=
new
URL
(
url
).
openStream
();
OutputStream
os
=
response
.
getOutputStream
())
{
byte
[]
buffer
=
new
byte
[
8192
];
int
bytesRead
;
...
...
@@ -150,6 +153,8 @@ public class FileController {
os
.
write
(
buffer
,
0
,
bytesRead
);
}
os
.
flush
();
}
catch
(
IOException
e
)
{
response
.
setStatus
(
HttpServletResponse
.
SC_BAD_GATEWAY
);
// 502,云端文件无法访问
}
}
...
...
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