仿豆丁百度文库网页版阅读器完整解决方案
?
?
?
Java平臺要實(shí)現(xiàn)類似豆丁百度文科的文檔在線閱讀,總體思路是講doc docx等文件格式利用openoffice轉(zhuǎn)換成pdf再用swftools轉(zhuǎn)為swf。再用flexpaper組件顯示swf。
此過程涉及軟件(實(shí)測)
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目錄下含多個(gè)要用到的jar文件)
flexpaper1.4.0 (直接復(fù)制過來,項(xiàng)目上運(yùn)行成功,本地運(yùn)行還沒成功)
FlexPaper_1.4.5_flash(網(wǎng)上下載的最新版本沒試過,二選一即可)
附上自己寫的java轉(zhuǎn)換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格式轉(zhuǎn)換
?* @author Administrator
?*
?*/
public classDocConverter {
??? private static final int environment = 1;//環(huán)境 1:windows2:linux (只涉及pdf2swf路徑問題)
??? privateString fileString;
??? privateString outputPath = "";//輸入路徑 ,如果不設(shè)置就輸出在默認(rèn)的位置
??? privateString fileName;
??? private FilepdfFile;
??? private FileswfFile;
??? private FiledocFile;
???
??? publicDocConverter(String fileString) {
?????? ini(fileString);
??? }
???
??? /**
??? ?* 重新設(shè)置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");
??? }
?
??? /**
??? ?* 轉(zhuǎn)為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轉(zhuǎn)換成功,PDF輸出:" + pdfFile.getPath()+ "****");
????????????? } catch(java.net.ConnectException e) {
????????????????? // TODOAuto-generated catch block
????????????????? e.printStackTrace();
????????????????? System.out.println("****swf轉(zhuǎn)換器異常,openoffice服務(wù)未啟動(dòng)!****");
????????????????? throw e;
????????????? } catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
????????????????? e.printStackTrace();
????????????????? System.out.println("****swf轉(zhuǎn)換器異常,讀取轉(zhuǎn)換文件失敗****");
????????????????? throw e;
????????????? } catch(Exception e){
????????????????? e.printStackTrace();
????????????????? throw e;
????????????? }
?????????? } else {
????????????? System.out.println("****已經(jīng)轉(zhuǎn)換為pdf,不需要再進(jìn)行轉(zhuǎn)化****");
?????????? }
?????? } else {
?????????? System.out.println("****swf轉(zhuǎn)換器異常,需要轉(zhuǎn)換的文檔不存在,無法轉(zhuǎn)換****");
?????? }
??? }
???
??? /**
??? ?* 轉(zhuǎn)換成 swf
??? ?*/
??? private voidpdf2swf() throws Exception{
?????? Runtime r = Runtime.getRuntime();
?????? if(!swfFile.exists()){
?????????? if(pdfFile.exists()){
????????????? if(environment ==1){//windows環(huán)境處理
????????????????? 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轉(zhuǎn)換成功,文件輸出:" + swfFile.getPath()+ "****");
???????????????????? if(pdfFile.exists()){
???????????????????????? pdfFile.delete();
???????????????????? }
????????????????????
????????????????? } catch(IOException e) {
???????????????????? e.printStackTrace();
???????????????????? throw e;
????????????????? }
????????????? } else if(environment ==2){//linux環(huán)境處理
????????????????? 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轉(zhuǎn)換成功,文件輸出:" + swfFile.getPath()+ "****");
???????????????????? if(pdfFile.exists()){
???????????????????????? pdfFile.delete();
???????????????????? }
????????????????? } catch(Exception e) {
???????????????????? e.printStackTrace();
???????????????????? throw e;
????????????????? }
????????????? }
?????????? } else {
????????????? System.out.println("****pdf不存在,無法轉(zhuǎn)換****");
?????????? }
?????? } else {
?????????? System.out.println("****swf已經(jīng)存在不需要轉(zhuǎn)換****");
?????? }
??? }
?
??? 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();
??? }
???
???
??? /**
??? ?* 轉(zhuǎn)換主方法
??? ?*/
??? public booleanconver() {
??????
?????? if(swfFile.exists()){
?????????? System.out.println("****swf轉(zhuǎn)換器開始工作,該文件已經(jīng)轉(zhuǎn)換為swf****");
?????????? return true;
?????? }
??????
?????? if(environment ==1){
?????????? System.out.println("****swf轉(zhuǎn)換器開始工作,當(dāng)前設(shè)置運(yùn)行環(huán)境windows****");
?????? } else {
?????????? System.out.println("****swf轉(zhuǎn)換器開始工作,當(dāng)前設(shè)置運(yùn)行環(huán)境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 "";
?????? }
??????
??? }
???
??? /**
??? ?* 設(shè)置輸出路徑
??? ?*/
??? 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();
??? }??
}
?
-------------------------------------------------------------------------------------------------------------------------------------------------------
?
以上即是所有用到的轉(zhuǎn)換需要用到的文件
?
windows安裝過程省略,linux下安裝swftools命令
解壓到目錄后
./configure
make
make install
?
無論在哪個(gè)平臺安裝完以上兩個(gè)軟件后
需要在將openoffice以服務(wù)方式啟動(dòng)
Window 啟動(dòng)方式
cd C:\ProgramFiles\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"–nofirststartwizard
Linux啟動(dòng)
soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard &
?
?
以上是實(shí)現(xiàn)兩個(gè)平臺下的轉(zhuǎn)換,但是會(huì)有openoffice解析不了的字體亂碼問題,或者顯示字體變形等
更高級的web文檔應(yīng)用可以參考QQ郵件,郵件實(shí)現(xiàn)了在線預(yù)覽excel doc pdf等格式文件并可以用鼠標(biāo)點(diǎn)擊選取復(fù)制,期待高手解決.
轉(zhuǎn)載于:https://blog.51cto.com/2342615/702090
總結(jié)
以上是生活随笔為你收集整理的仿豆丁百度文库网页版阅读器完整解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【配置文件】大家来谈谈这个log4j到底
- 下一篇: 菜鸟修炼C语言小设计之——工资统计