package com.baidu.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();
}
}
<%@ 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>
<%@ 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 style="position:absolute;left:10px;top:10px;">
<a id="viewerPlaceHolder" style="width:1000px;height:480px;display:block"></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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有