Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
Y
yn-health-science
Project
Project
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
徐俊
yn-health-science
Commits
7d44a1f7
Commit
7d44a1f7
authored
3 months ago
by
徐俊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
xujun
parent
136cd6cb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
WordToPdfConverter.java
...in/java/com/yiboshi/science/utils/WordToPdfConverter.java
+54
-0
No files found.
science-admin/src/main/java/com/yiboshi/science/utils/WordToPdfConverter.java
View file @
7d44a1f7
...
...
@@ -180,4 +180,58 @@ public class WordToPdfConverter {
String
lowerFileName
=
fileName
.
toLowerCase
();
return
lowerFileName
.
endsWith
(
".doc"
)
||
lowerFileName
.
endsWith
(
".docx"
);
}
/**
* 从远程URL下载Word文档并转换为PDF
* @param wordUrl Word文档的URL地址
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public
static
boolean
convertUrlToPdf
(
String
wordUrl
,
String
pdfPath
)
{
if
(
wordUrl
==
null
||
wordUrl
.
trim
().
isEmpty
())
{
logger
.
error
(
"Word文档URL为空"
);
return
false
;
}
InputStream
inputStream
=
null
;
try
{
// 创建URL连接
java
.
net
.
URL
url
=
new
java
.
net
.
URL
(
wordUrl
);
java
.
net
.
HttpURLConnection
conn
=
(
java
.
net
.
HttpURLConnection
)
url
.
openConnection
();
conn
.
setRequestMethod
(
"GET"
);
conn
.
setConnectTimeout
(
5000
);
conn
.
setReadTimeout
(
5000
);
// 检查响应码
if
(
conn
.
getResponseCode
()
!=
java
.
net
.
HttpURLConnection
.
HTTP_OK
)
{
logger
.
error
(
"下载Word文档失败,HTTP响应码: {}"
,
conn
.
getResponseCode
());
return
false
;
}
// 读取文件内容
inputStream
=
conn
.
getInputStream
();
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
4096
];
int
bytesRead
;
while
((
bytesRead
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
outputStream
.
write
(
buffer
,
0
,
bytesRead
);
}
// 转换为PDF
byte
[]
wordBytes
=
outputStream
.
toByteArray
();
return
convertToPdf
(
wordBytes
,
pdfPath
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"从URL下载Word文档失败: {}"
,
wordUrl
,
e
);
return
false
;
}
finally
{
if
(
inputStream
!=
null
)
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"关闭输入流失败"
,
e
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
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