package org.springblade.common.utils; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class WordToPdfUtils { // https://srgdjczzxtpt.com:2080/gminio/jczz/upload/20240710/ade086b8d725a08631b62d812a63f6da.docx public static String wordToPdf(String fileName, String pdfPath) throws Exception { // 判断入参是否有空 if (fileName == null || pdfPath == null) { throw new Exception("入参不能为空"); } // 判断文件后缀名是否doc,ppt,xls if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) { // 获取文件流 URL url = new URL(pdfPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // 获取文件名 String orFileName = fileName.substring(0, fileName.lastIndexOf(".")); InputStream inputStream = connection.getInputStream(); long timeMillis = System.currentTimeMillis(); // String filePathPdf = "/data/app/jczz/pdf/" + orFileName + "_" + timeMillis + ".pdf"; String filePathPdf = "D:\\公司\\" + orFileName + "_" + timeMillis + ".pdf"; com.aspose.words.Document doc = new com.aspose.words.Document(inputStream); doc.save(filePathPdf, com.aspose.words.SaveFormat.PDF); return filePathPdf; } else { throw new Exception("文件格式不正确"); } } public static void main(String[] args) throws Exception { wordToPdf(" 测试.docx", "https://srgdjczzxtpt.com:2080/gminio/jczz/upload/20240710/ade086b8d725a08631b62d812a63f6da.docx"); } }