OpenOffice+SWFTools+ FlexPaper实现文件预览

来源:互联网 发布:c语言所有水仙花数程序 编辑:程序博客网 时间:2024/06/05 11:12

准备

1.安装OpenOffice打开cmd进入OpenOffice\program的安装目录OpenOffice服务启动命令:soffice -headless —accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizardOpenOffice查看服务是否启动命令:netstat -ano|findstr "8100"tasklist|findstr "ipd值"OpenOffce 端口:81002安装SWFTools3.配置FlexPaper:flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,flexpaper.js,flexpaper_handler.js ,FlexPaperViewer.swf4.注意点: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();                    }                }            }        }    }}
阅读全文
2 0