分享

java实现类似百度文库功能

 昵称13755328 2013-09-22

公司需要开发一个类似百度文库功能的管理站,在网上找了好久,主要有两种实现方法,我在这里根据网上一篇文章,总结了一下具体的实现。

首先下载必要的文件。

1、SWF显示组件 flexpaper  下载地址 http://flexpaper./

2、DOC文件转换为PDF文件 openoffice3.2

3、PDF文件转换SWF文件  pdf2swf.exe

4、实现在java类中操作openoffice3.2 的类包  jodconverter-2.2.2


flexpaper可以去上面的官网地址下载,但直接下载的组件会有广告和一些不需要用到的功能,所以最好是自己下载Flex源码进行修改

接下来要通过java类来实现文件类型的转换,在网上直接找到该类的代码。

001package com;
002 
003import java.io.BufferedInputStream;
004import java.io.File;
005import java.io.IOException;
006import java.io.InputStream;
007 
008import com.artofsolving.jodconverter.DocumentConverter;
009import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
010import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
011import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
012 
013/**
014 * doc docx格式转换
015 *
016 * @author Administrator
017 *
018 */
019public class DocConverter {
020    private static final int environment = 1;// 环境 1:windows 2:linux
021                                                // (只涉及pdf2swf路径问题)
022    private String fileString;
023    private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
024    private String fileName;
025    private File pdfFile;
026    private File swfFile;
027    private File docFile;
028 
029    public DocConverter(String fileString) {
030        ini(fileString);
031    }
032 
033    /**
034     * 重新设置file
035     *
036     * @param fileString
037     */
038    public void setFile(String fileString) {
039        ini(fileString);
040    }
041 
042    /**
043     * 初始化
044     *
045     * @param fileString
046     */
047    private void ini(String fileString) {
048        this.fileString = fileString;
049        fileName = fileString.substring(0, fileString.lastIndexOf("."));
050        docFile = new File(fileString);
051        pdfFile = new File(fileName + ".pdf");
052        swfFile = new File(fileName + ".swf");
053    }
054 
055    /**
056     * 转为PDF
057     *
058     * @param file
059     */
060    private void doc2pdf() throws Exception {
061        if (docFile.exists()) {
062            if (!pdfFile.exists()) {
063                OpenOfficeConnection connection = new SocketOpenOfficeConnection(
064                        8100);
065                try {
066                    connection.connect();
067                    DocumentConverter converter = new OpenOfficeDocumentConverter(
068                            connection);
069                    converter.convert(docFile, pdfFile);
070                    // close the connection
071                    connection.disconnect();
072                    System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()
073                            + "****");
074                } catch (java.net.ConnectException e) {
075                    e.printStackTrace();
076                    System.out.println("****swf转换器异常,openoffice服务未启动!****");
077                    throw e;
078                } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
079                    e.printStackTrace();
080                    System.out.println("****swf转换器异常,读取转换文件失败****");
081                    throw e;
082                } catch (Exception e) {
083                    e.printStackTrace();
084                    throw e;
085                }
086            } else {
087                System.out.println("****已经转换为pdf,不需要再进行转化****");
088            }
089        } else {
090            System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
091        }
092    }
093 
094    /**
095     * 转换成 swf
096     */
097    private void pdf2swf() throws Exception {
098        Runtime r = Runtime.getRuntime();
099        if (!swfFile.exists()) {
100            if (pdfFile.exists()) {
101                if (environment == 1) {// windows环境处理
102                    try {
103                        Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
104                        System.out.print(loadStream(p.getInputStream()));
105                        System.err.print(loadStream(p.getErrorStream()));
106                        System.out.print(loadStream(p.getInputStream()));
107                        System.err.println("****swf转换成功,文件输出:"
108                                + swfFile.getPath() + "****");
109                        if (pdfFile.exists()) {
110                            pdfFile.delete();
111                        }
112 
113                    } catch (IOException e) {
114                        e.printStackTrace();
115                        throw e;
116                    }
117                } else if (environment == 2) {// linux环境处理
118                    try {
119                        Process p = r.exec("pdf2swf " + pdfFile.getPath()
120                                + " -o " + swfFile.getPath() + " -T 9");
121                        System.out.print(loadStream(p.getInputStream()));
122                        System.err.print(loadStream(p.getErrorStream()));
123                        System.err.println("****swf转换成功,文件输出:"
124                                + swfFile.getPath() + "****");
125                        if (pdfFile.exists()) {
126                            pdfFile.delete();
127                        }
128                    } catch (Exception e) {
129                        e.printStackTrace();
130                        throw e;
131                    }
132                }
133            } else {
134                System.out.println("****pdf不存在,无法转换****");
135            }
136        } else {
137            System.out.println("****swf已经存在不需要转换****");
138        }
139    }
140 
141    static String loadStream(InputStream in) throws IOException {
142 
143        int ptr = 0;
144        in = new BufferedInputStream(in);
145        StringBuffer buffer = new StringBuffer();
146 
147        while ((ptr = in.read()) != -1) {
148            buffer.append((char) ptr);
149        }
150 
151        return buffer.toString();
152    }
153 
154    /**
155     * 转换主方法
156     */
157    public boolean conver() {
158 
159        if (swfFile.exists()) {
160            System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
161            return true;
162        }
163 
164        if (environment == 1) {
165            System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
166        } else {
167            System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
168        }
169        try {
170            doc2pdf();
171            pdf2swf();
172        } catch (Exception e) {
173            e.printStackTrace();
174            return false;
175        }
176 
177        if (swfFile.exists()) {
178            return true;
179        } else {
180            return false;
181        }
182    }
183 
184    /**
185     * 返回文件路径
186     *
187     * @param s
188     */
189    public String getswfPath() {
190        if (swfFile.exists()) {
191            String tempString = swfFile.getPath();
192            tempString = tempString.replaceAll("\\\\", "/");
193            return tempString;
194        } else {
195            return "";
196        }
197 
198    }
199 
200    /**
201     * 设置输出路径
202     */
203    public void setOutputPath(String outputPath) {
204        this.outputPath = outputPath;
205        if (!outputPath.equals("")) {
206            String realName = fileName.substring(fileName.lastIndexOf("/"),
207                    fileName.lastIndexOf("."));
208            if (outputPath.charAt(outputPath.length()) == '/') {
209                swfFile = new File(outputPath + realName + ".swf");
210            } else {
211                swfFile = new File(outputPath + realName + ".swf");
212            }
213        }
214    }
215 
216    public static void main(String s[]) {
217        DocConverter d = new DocConverter("D:/1.doc");
218        d.conver();
219    }
220}
贴入上面代码前

第一步:先确认OpenOffice是否已经安装,因为在代码中要引入到文件安装的路径

第二步:确认你的项目中引入了jodconverter-2.2.2的jar包

第三步:在DOS中启动OpenOffice的服务

找到OpenOffice的安装路径并执行下面代码

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard

注:我在执行上面代码时,DOS报出了找不到–nofirststartwizard文件的异常,所以我去掉了–nofirststartwizard这一段,执行也成功了。

C:\Program Files\OpenOffice.org 3\program  soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

确认所有准备工作都完成后,在下面代码中变更你pdf2swf.exe的文件位置

1Process p = r.exec("D:/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
最后在main方法传入你要转换的文件路径和文件名,执行就可以生成swf文件了。


在WEB项目中,生成的文件名可以根据自己的要求来变更,最终只用掉用上面的工具类代码就可以了。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多