- 引入依賴
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.0</version>
</dependency>
- application.properties配置
jodconverter.local.enabled=true
jodconverter.local.port-numbers=8100
- controller代碼
@Controller
public class MyController {
// 第一步:轉換器直接注入
@Autowired
private DocumentConverter converter;
@Autowired
private HttpServletResponse response;
@RequestMapping("toPdfFile")
public String toPdfFile() {
//該編碼方式只適用於window10本地安裝openoffice,不適用docker鏡像的openoffice,因爲鏡像基於linux系統,你的操作文件/目錄在window,和鏡像不在一臺機器,所以就會報404,url錯誤
File file = new File("D:/aa.doc");//需要轉換的文件
try {
File newFile = new File("D:/obj-pdf");//轉換之後文件生成的地址
if (!newFile.exists()) {
newFile.mkdirs();
}
//文件轉化
converter.convert(file).to(new File("D:/obj-pdf/hello.pdf")).execute();
//使用response,將pdf文件以流的方式發送的前段
ServletOutputStream outputStream = response.getOutputStream();
InputStream in = new FileInputStream(new File("D:/obj-pdf/hello.pdf"));// 讀取文件
// copy文件
int i = IOUtils.copy(in, outputStream);
System.out.println(i);
in.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return "This is to pdf";
}
}
-
下載openoffice
4.1 window10版openoffice(該方式可用上述的controller代碼測試)
http://www.openoffice.org/download/
下載可自定義安裝目錄,也可使用默認的路徑在:
執行如下命令啓動:
soffice -headless -accept=“socket,host=0.0.0.0,port=8100;urp;” -nofirststartwizard4.2 如果基於docker的openoffice鏡像
version: "3.3" services: openoffice: image: "rafaeltuelho/openoffice3-daemon" ports: - 8100:8100 command: /opt/openoffice.org3/program/soffice -headless -nofirststartwizard -accept="socket,host=0.0.0.0,port=8100;urp;"
代碼如下
public void preview(Long fileId, HttpServletResponse response) {
FileEntity fileData = fileMapper.selectOne(new QueryWrapper<FileEntity>().eq("file_id", fileId));
String fileName = fileData.getFileName();
byte[] fileByte=FastDFSClient.getFileByte(fileData);
//獲得文件名後綴
String suffix = "";
if (StringUtils.isNotBlank(fileName) && fileName.contains(".")) {
suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
}
if (StringUtils.isBlank(suffix)) {
throw new OutOfBusinessException("文件預覽失敗");
}
String[] suffixs = new String[]{"DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX", "TXT"};
if (Arrays.asList(suffixs).contains(suffix)) {
File tempFile = null;
try {
tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + suffix);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(fileByte, 0, fileByte.length);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
String filePath = tempFile.getPath();
//轉換之後的文件
File pdfFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
//打開OpenOffice連接
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(tempFile, pdfFile);
connection.disconnect();
filePath = pdfFile.getPath();
} catch (Exception e) {
e.printStackTrace();
throw new OutOfBusinessException("文件轉換失敗");
} finally {
try {
//發生exception時, connection不會自動切斷, 程序會一直掛着
if (connection != null) {
connection.disconnect();
connection = null;
}
} catch (Exception e) {
}
}
response.setContentType("application/pdf");
//將文件寫入輸出流,顯示在界面上,實現預覽效果
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
byte[] byt = new byte[fis.available()];
fis.read(byt);
writeBytesToOut(response.getOutputStream(), byt, 4096);
tempFile.delete();
pdfFile.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//預覽
response.setContentType(fileData.getFileType());
try {
writeBytesToOut(response.getOutputStream(), fileByte, 4096);
} catch (IOException e) {
e.printStackTrace();
}
}
}