Commit 7d44a1f7 authored by 徐俊's avatar 徐俊

xujun

parent 136cd6cb
......@@ -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);
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment