仿豆丁百度文库网页版阅读器完整解决方案
?
?
?
Java平臺要實現類似豆丁百度文科的文檔在線閱讀,總體思路是講doc docx等文件格式利用openoffice轉換成pdf再用swftools轉為swf。再用flexpaper組件顯示swf。
此過程涉及軟件(實測)
openoffice3.2(window)
swftools-2011-01-23-1815.exe(window)
openoffice3.1.1(linux)
swftools-2011-01-23-1815.tar.gz(linux)
jar包jodconverter-2.2.2(里面lib目錄下含多個要用到的jar文件)
flexpaper1.4.0 (直接復制過來,項目上運行成功,本地運行還沒成功)
FlexPaper_1.4.5_flash(網上下載的最新版本沒試過,二選一即可)
附上自己寫的java轉換Demo DocConverter.java
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
?
importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
?
?
/**
?* doc docx格式轉換
?* @author Administrator
?*
?*/
public classDocConverter {
??? private static final int environment = 1;//環境 1:windows2:linux (只涉及pdf2swf路徑問題)
??? privateString fileString;
??? privateString outputPath = "";//輸入路徑 ,如果不設置就輸出在默認的位置
??? privateString fileName;
??? private FilepdfFile;
??? private FileswfFile;
??? private FiledocFile;
???
??? publicDocConverter(String fileString) {
?????? ini(fileString);
??? }
???
??? /**
??? ?* 重新設置file
??? ?* @param fileString
??? ?*/
??? public voidsetFile(String fileString){
?????? ini(fileString);
??? }
???
??? /**
??? ?* 初始化
??? ?* @param fileString
??? ?*/
??? private voidini(String fileString) {
?????? this.fileString =fileString;
?????? fileName =fileString.substring(0, fileString.lastIndexOf("."));
?????? docFile = newFile(fileString);
?????? pdfFile = newFile(fileName + ".pdf");
?????? swfFile = newFile(fileName + ".swf");
??? }
?
??? /**
??? ?* 轉為PDF
??? ?* @param file
??? ?*/
??? private voiddoc2pdf() throws Exception{
?????? if(docFile.exists()){
?????????? if(!pdfFile.exists()){
????????????? OpenOfficeConnection connection = newSocketOpenOfficeConnection(8100);
????????????? try {
????????????????? connection.connect();
????????????????? DocumentConverter converter = newOpenOfficeDocumentConverter(connection);??
????????????????? converter.convert(docFile, pdfFile);?
????????????????? // close theconnection??
????????????????? connection.disconnect();
????????????????? System.out.println("****pdf轉換成功,PDF輸出:" + pdfFile.getPath()+ "****");
????????????? } catch(java.net.ConnectException e) {
????????????????? // TODOAuto-generated catch block
????????????????? e.printStackTrace();
????????????????? System.out.println("****swf轉換器異常,openoffice服務未啟動!****");
????????????????? throw e;
????????????? } catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
????????????????? e.printStackTrace();
????????????????? System.out.println("****swf轉換器異常,讀取轉換文件失敗****");
????????????????? throw e;
????????????? } catch(Exception e){
????????????????? e.printStackTrace();
????????????????? throw e;
????????????? }
?????????? } else {
????????????? System.out.println("****已經轉換為pdf,不需要再進行轉化****");
?????????? }
?????? } else {
?????????? System.out.println("****swf轉換器異常,需要轉換的文檔不存在,無法轉換****");
?????? }
??? }
???
??? /**
??? ?* 轉換成 swf
??? ?*/
??? private voidpdf2swf() throws Exception{
?????? Runtime r = Runtime.getRuntime();
?????? if(!swfFile.exists()){
?????????? if(pdfFile.exists()){
????????????? if(environment ==1){//windows環境處理
????????????????? try {
???????????????????? Process p = r.exec("D:/TOOLS/SWFTools/pdf2swf.exe" +
??????????????????????????? pdfFile.getPath()+ " -o " + swfFile.getPath()+ " -T 9");
???????????????????? System.out.print(loadStream(p.getInputStream()));
???????????????????? System.err.print(loadStream(p.getErrorStream()));
???????????????????? System.out.print(loadStream(p.getInputStream()));
???????????????????? System.err.println("****swf轉換成功,文件輸出:" + swfFile.getPath()+ "****");
???????????????????? if(pdfFile.exists()){
???????????????????????? pdfFile.delete();
???????????????????? }
????????????????????
????????????????? } catch(IOException e) {
???????????????????? e.printStackTrace();
???????????????????? throw e;
????????????????? }
????????????? } else if(environment ==2){//linux環境處理
????????????????? try {
???????????????????? Process p = r.exec("pdf2swf" +
??????????????????????????? pdfFile.getPath()+ " -o " + swfFile.getPath()+ " -T 9");
???????????????????? System.out.print(loadStream(p.getInputStream()));
???????????????????? System.err.print(loadStream(p.getErrorStream()));
???????????????????? System.err.println("****swf轉換成功,文件輸出:" + swfFile.getPath()+ "****");
???????????????????? if(pdfFile.exists()){
???????????????????????? pdfFile.delete();
???????????????????? }
????????????????? } catch(Exception e) {
???????????????????? e.printStackTrace();
???????????????????? throw e;
????????????????? }
????????????? }
?????????? } else {
????????????? System.out.println("****pdf不存在,無法轉換****");
?????????? }
?????? } else {
?????????? System.out.println("****swf已經存在不需要轉換****");
?????? }
??? }
?
??? staticString loadStream(InputStream in) throwsIOException {
?
?????? int ptr= 0;
?????? in = newBufferedInputStream(in);
?????? StringBuffer buffer = newStringBuffer();
?
?????? while((ptr = in.read()) != -1 ) {
?????? buffer.append((char)ptr);
?????? }
?
?????? returnbuffer.toString();
??? }
???
???
??? /**
??? ?* 轉換主方法
??? ?*/
??? public booleanconver() {
??????
?????? if(swfFile.exists()){
?????????? System.out.println("****swf轉換器開始工作,該文件已經轉換為swf****");
?????????? return true;
?????? }
??????
?????? if(environment ==1){
?????????? System.out.println("****swf轉換器開始工作,當前設置運行環境windows****");
?????? } else {
?????????? System.out.println("****swf轉換器開始工作,當前設置運行環境linux****");
?????? }
?????? try {
?????????? doc2pdf();
?????????? pdf2swf();
?????? } catch(Exception e) {
?????????? // TODOAuto-generated catch block
?????????? e.printStackTrace();
?????????? return false;
?????? }
??????
?????? if (swfFile.exists()){
?????????? return true;
?????? } else {
?????????? return false;
?????? }
??? }
???
??? /**
??? ?* 返回文件路徑
??? ?* @param s
??? ?*/
??? publicString getswfPath(){
?????? if(swfFile.exists()){
?????????? String tempString = swfFile.getPath();
?????????? tempString = tempString.replaceAll("\\\\", "/");
?????????? returntempString;
?????? } else {
?????????? return "";
?????? }
??????
??? }
???
??? /**
??? ?* 設置輸出路徑
??? ?*/
??? public voidsetOutputPath(String outputPath){
?????? this.outputPath =outputPath;
?????? if(!outputPath.equals("")){
?????????? String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
?????????? if(outputPath.charAt(outputPath.length())== '/'){
????????????? swfFile = newFile(outputPath + realName + ".swf");
?????????? } else {
????????????? swfFile = newFile(outputPath + realName + ".swf");
?????????? }
?????? }
??? }
???
??? public static voidmain(String s[]) {
?????? DocConverter d = newDocConverter("D:/data/FTP/midlongforecast/midweatherforecast/20010202.xml");
?????? d.conver();
??? }??
}
?
-------------------------------------------------------------------------------------------------------------------------------------------------------
?
以上即是所有用到的轉換需要用到的文件
?
windows安裝過程省略,linux下安裝swftools命令
解壓到目錄后
./configure
make
make install
?
無論在哪個平臺安裝完以上兩個軟件后
需要在將openoffice以服務方式啟動
Window 啟動方式
cd C:\ProgramFiles\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"–nofirststartwizard
Linux啟動
soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard &
?
?
以上是實現兩個平臺下的轉換,但是會有openoffice解析不了的字體亂碼問題,或者顯示字體變形等
更高級的web文檔應用可以參考QQ郵件,郵件實現了在線預覽excel doc pdf等格式文件并可以用鼠標點擊選取復制,期待高手解決.
轉載于:https://blog.51cto.com/2342615/702090
總結
以上是生活随笔為你收集整理的仿豆丁百度文库网页版阅读器完整解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【配置文件】大家来谈谈这个log4j到底
- 下一篇: 菜鸟修炼C语言小设计之——工资统计