文件流转成pdf流,使用openoffice,适应office2003/2007

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;
    }   

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值