OpenOffice+SWFTools+ FlexPaper实现文件预览

准备

1.安装OpenOffice
打开cmd
进入OpenOffice\program的安装目录
OpenOffice服务启动命令:soffice -headless —accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
OpenOffice查看服务是否启动命令:
netstat -ano|findstr "8100"
tasklist|findstr "ipd值"
OpenOffce 端口:8100

2安装SWFTools

3.配置FlexPaper:
flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,flexpaper.js,flexpaper_handler.js ,FlexPaperViewer.swf

4.注意点:
OpenOffice对纯文本文件不是很兼容,先renameTo *.odt格式

主要代码

/**
 * 文件预览功能
 * 
 * @author pc
 *
 */
public class PreviewUtil {

    protected final static Logger _logger = LoggerFactory.getLogger(PreviewUtil.class);

    /*
     * OFFICE-PDF
     */
    public static String converPDF(String filePath) throws Exception {
        // 源文件
        File primaryFile = new File(filePath);
        // 转换后的pdf文件
        File pdfFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
        // 如果存在进行转换
        // OPENOFFICE 8100
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
        try {
            // START
            connection.connect();
        } catch (ConnectException e) {
            _logger.error("获取OPENOFFICE连接失败:",e);
            throw new Exception("获取OPENOFFICE连接失败:" + e.getMessage());
        }
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(primaryFile, pdfFile);
        connection.disconnect();
        // STOP
        return filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf";
    }

    /*
     * 判断系统类型
     */
    public static int validateOSType() {
        String os = System.getProperty("os.name");
        // windows
        if (os.toLowerCase().startsWith("win")) {
            return PreviewConstant.WINDOWS_ENVIRONMENT;
        } else if (os.toLowerCase().startsWith("linux")) {
            // Linux
            return PreviewConstant.LINUX_ENVIRONMENT;
        }
        return 0;
    }

    /*
     * PDF-SWF
     */
    public static String converSWF(String filePath) throws Exception {
        // 新起运行库
        Runtime runtime = Runtime.getRuntime();
        File pdfFile = new File(filePath);
        String swfPath = "webroot/share/upload/"+filePath.substring(filePath.lastIndexOf("/")+1,filePath.lastIndexOf(".")) + ".swf";
        if (pdfFile.exists()) {
            // WINDOWS下启动SWFTOOLS进程
            if (validateOSType() == 1) {
                try {
                    Process process = runtime
                            .exec(PreviewConstant.WINDOWS_SWFTOOLS_URL + filePath + " -o " + swfPath + " -f -T 9");
                    new DoOutput(process.getInputStream()).start();
                    new DoOutput(process.getErrorStream()).start();
                    try {
                        // 阻塞当前进程  等待process执行完成
                        process.waitFor();
                    } catch (InterruptedException e) {
                        _logger.error("转换失败:",e);
                    }
                    return swfPath;
                } catch (IOException e) {
                    _logger.error("WINDOWS SWFTOOLS进行转换失败:",e);
                    throw new Exception("WINDOWS SWFTOOLS进行转换失败:" + e.getMessage());
                }
            } else if (validateOSType() == 2) {
                // LINUX下启动SWFTOOLS进程
                try {
                    Process process = runtime
                            .exec(PreviewConstant.LINUX_SWFTOOLS_URL + " " + filePath + " -o " + swfPath + " -f -T 9");
                    new DoOutput(process.getInputStream()).start();
                    new DoOutput(process.getErrorStream()).start();
                    try {
                        // 阻塞当前进程,直到cmd执行完
                        process.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //删除PDF文件
                    pdfFile.delete();
                    return swfPath;
                } catch (IOException e) {
                    _logger.error("LINUX SWFTOOLS进行转换失败:",e);
                    throw new Exception("LINUX SWFTOOLS进行转换失败:" + e.getMessage());
                }
            } else if (validateOSType() == 0) {
                throw new Exception("请确认当前操作系统类型");
            }
        }else{
            throw new Exception("PDF-SWF失败:为找到PDF文件!");
        }
        throw new Exception("文件转换失败!");
    }

    /*
     * 读取转换时cmd进程的标准输出流和错误输出流,这样做是因为如果不读取流,进程将死锁
     */
    private static class DoOutput extends Thread {
        public InputStream is;

        public DoOutput(InputStream is) {
            this.is = is;
        }

        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
            String str = null;
            try {
                while ((str = br.readLine()) != null)
                    ;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值