進修Java模仿完成百度文檔在線閱讀。本站提示廣大學習愛好者:(進修Java模仿完成百度文檔在線閱讀)文章只能為提供參考,不一定能成為您想要的結果。以下是進修Java模仿完成百度文檔在線閱讀正文
這個思緒是我參考網上而來,代碼是我完成。
采取Apache上面的OpenOffice將資本文件轉化為pdf文件,然後將pdf文件轉化為swf文件,用FlexPaper閱讀。
ok,
A、下載OpenOffice (轉換資本文件)
B、下載JodConverter(挪用OpenOffice)
C、下載Swftools(Pdf2Swf)
D、下載 FlexPaper(閱讀swf文件)
下載以後,先別急裝置,請看完這篇博文
1、先看我們的MyEclipse工程構造
2、將我們下載上去的jodconverter-2.2.2.zip解壓以後將一切的jar文件拷貝到百度Doc的lib上面去
3、在WebRoot上面新建FlexPaper文件夾,將解壓後的FlexPaper全體拷貝到FlexPaper中去
4、新建BaiDuServlet.java文件
package com.百度.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import javax.imageio.stream.FileImageInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* @Author:NuoYan
* @Date:2015-2-2 下晝2:24:58
* TODO: 1、第一步,起首獲得到須要檢查的文件
* 2、第二部,將獲得的文件(doc,xls,txt,ppt,03/07版本轉化為PDF),這一步須要挪用OpenOffice
* 3、第三部,將資本文件轉換好的PDF文件轉換為swf文件,應用FlexPaperViewer.swf停止閱讀檢查
*/
public class BaiDuServlet extends HttpServlet {
private File sourceFile;// 要轉化的源文件
private File pdfFile;// pdf中央文件對象
private File swfFile;// swf目的文件對象
private String filePath;// 用來保留文件途徑
private String fileName;// 不包含後綴名的文件名
public File getSourceFile() {
return sourceFile;
}
public void setSourceFile(File sourceFile) {
this.sourceFile = sourceFile;
}
public File getPdfFile() {
return pdfFile;
}
public void setPdfFile(File pdfFile) {
this.pdfFile = pdfFile;
}
public File getSwfFile() {
return swfFile;
}
public void setSwfFile(File swfFile) {
this.swfFile = swfFile;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String saveFileName = request.getParameter("savFile");
System.out.println(saveFileName);
String webPath = request.getRealPath("/");
filePath = webPath + "reader\\" + saveFileName;
fileName = filePath.substring(0, filePath.lastIndexOf("."));
// 創立三個文件對象
sourceFile = new File(filePath);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
System.out.println(pdfFile);
System.out.println(swfFile);
// 1、將源文件轉化為pdf格局文件
src2pdf();
try {
// 2、將pdf文件轉化為swf文件
pdf2swf();
} catch (Exception e) {
e.printStackTrace();
}
// 將轉化好的文件綁定到session上去
request.getSession().setAttribute("swfName", swfFile.getName());
System.out.println(swfFile.getName());
// 重定向到預覽頁面
response.sendRedirect(request.getContextPath() + "/reader/baseFile.jsp");
}
/**
* @Author:NuoYan
* @Date:2015-2-2 下晝2:28:22 TODO://源文件轉化為PDF文件
*/
private void src2pdf() {
if (sourceFile.exists()) {
// 假如不存在,須要轉份為PDF文件
if (!pdfFile.exists()) {
// 啟用OpenOffice供給的轉化辦事
OpenOfficeConnection conn = new SocketOpenOfficeConnection(8100);
// 銜接OpenOffice辦事器
try {
conn.connect();
// 樹立文件轉換器對象
DocumentConverter converter = new OpenOfficeDocumentConverter(
conn);
converter.convert(sourceFile, pdfFile);
// 斷開鏈接
conn.disconnect();
System.out.println("轉換勝利");
} catch (ConnectException e) {
e.printStackTrace();
}
} else {
System.out.println("曾經存在PDF文件,不須要在轉換!!");
}
} else {
System.out.println("文件途徑不存在!!!");
}
}
/**
* @Author:NuoYan
* @Date:2015-2-2 下晝2:28:32
* @throws Exception
* TODO:PDF轉化為SWF文件
*/
private void pdf2swf() throws Exception {
if (!swfFile.exists()) {
if (pdfFile.exists()) {
String command = "C:\\Pdf2swf\\pdf2swf.exe "
+ pdfFile.getPath() + " -o " + swfFile.getPath()
+ " -T 9";
System.out.println("轉換敕令:" + command);
// Java挪用內部敕令,履行pdf轉化為swf
Runtime r = Runtime.getRuntime();
Process p = r.exec(command);
System.out.println(loadStream(p.getInputStream()));
System.out.println("swf文件轉份勝利!!!");
System.out.println(swfFile.getPath());
} else {
System.out.println("不存在PDF文件");
}
}
}
private static String loadStream(InputStream in) throws Exception {
int len = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((len = in.read()) != -1) {
buffer.append((char) len);
}
return buffer.toString();
}
}
5、修正index.jsp
<%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>百度文庫在線預覽</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="<%=request.getContextPath()%>/BaiDuServlet?savFile=1234.xls">在線預覽</a> </body> </html>
6、編寫baseFile.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在線浏覽</title>
<script type="text/javascript" src="../FlexPaper/js/flexpaper_flash.js"></script>
<style type="text/css">
html,body{height: 100%;}
body {
margin: 0;padding: 0;overflow: auto;
}
#flashContent { display:none; }
</style>
</head>
<body>
<div >
<a id="viewerPlaceHolder" ></a>
<script type="text/javascript">
var fp = new FlexPaperViewer(
'../FlexPaper/FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('../reader/<%=(String)session.getAttribute("swfName")%>'),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
PrintPaperAsBitmap : false,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}});
</script>
</div>
</body>
</html>
留意baseFile.jsp中的代碼,不會你可以參考這裡
/**************************************************************************************/
7、到這裡就完成,須要留意的是:
(1)、swftools-2013-04-09-1007.exe文件裝置途徑不要太深,否則Java挪用內部敕令不克不及履行
(2)、
2.1、白色1標志途徑不克不及錯,我就犯這個毛病了
2.2、白色標志2還可以寫http://127.0.0.1:8080/百度Doc/reader/...
(3)、啟動OpenOffice的敕令,不是直接雙擊啟動的。官網啟動方法,應用cd敕令翻開裝置目次!
裝置完openoffice後
A.裝置辦事
cd C:\Program Files (x86)\OpenOffice4\program
這一步你可以看你的OpenOffice裝置哪裡
履行
soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
B.檢查能否裝置勝利
2.1檢查端口對應的pid
netstat -ano|findstr "8100"
2.2檢查pid對應的辦事法式名
tasklist|findstr "ipd值"
後果圖示:
以上就是本文的全體內容,願望對年夜家的進修有所贊助。