public InputStream getPdfStream(String fileEnd, InputStream fileInput) throws Exception{
String fileType = "";
if("xlsx".equals(fileEnd)) { //xlsx格式的文件转成xls处理
fileType = "xls";
}else if("docx".equals(fileEnd)){ //docx格式的文件转成doc处理
fileType = "doc";
}else {
fileType = fileEnd;
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect(); //连接openoffice
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); //使用StreamOpenOfficeDocumentConverter可以转07版的
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry(); //jar包里的类
DocumentFormat inputFormat = formatReg.getFormatByFileExtension(fileType); //源文件的格式
DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf"); //转成的格式
ByteArrayOutputStream pdfstream = new ByteArrayOutputStream(); //保存转成pdf的流的数组
converter.convert(fileInput, inputFormat, pdfstream, pdfFormat); //将文件流转换成pdf流
InputStream pdfInput = new BufferedInputStream(new ByteArrayInputStream(pdfstream.toByteArray()));//把pdf流转成输入流
pdfstream.flush();
pdfstream.close();
return pdfInput;
} catch(Exception e) {
e.printStackTrace();
} finally {
try{
if(connection != null){
connection.disconnect();
connection = null;
}
}catch(Exception e){}
}
return null;
}