1、转换文件(docx~pdf)
1.引入pom依赖
< dependency> < groupId> com. aspose< / groupId> < artifactId> aspose- words< / artifactId> < version> 20.12 .0 < / version>
< / dependency>
2.读取docx文档数据-转换
String pdfPath = "testConvert.pdf" ;
String fileName = "test.docx" ;
Document doc = new Document ( fileName) ;
doc. save ( pdfPath, SaveFormat. PDF) ;
3.读取pdf文档数据-响应
FIle pdfFile
FileInputStream fis = new FileInputStream ( pdfFile) ;
response. setCharacterEncoding ( "UTF-8" ) ;
response. setContentType ( "application/pdf; charset=UTF-8" ) ;
String disposition = "attachment; filename=" + URLEncoder. encode ( "fileName" , "UTF-8" ) ;
response. setHeader ( "Content-Disposition" , disposition) ;
response. setContentLength ( ( int ) pdfFile. length ( ) ) ;
try ( FileInputStream fileInputStream = new FileInputStream ( pdfFile) ; OutputStream outputStream = response. getOutputStream ( ) ) { byte[ ] buffer = new byte[ 4096 ] ; int bytesRead; while ( ( bytesRead = fileInputStream. read ( buffer) ) != - 1 ) { outputStream. write ( buffer, 0 , bytesRead) ; }
}