读书人

Java中怎么实现在jsp页面在线预览文

发布时间: 2013-07-04 11:45:32 作者: rapoo

在线等,Java中如何实现在jsp页面在线预览文档
RT
文档上传做好了
在数据库中存的是上传文档的路径
现在项目需求是要点击文档名称在jsp页面中打开该文档,类似百度文库那种的预览,
文档格式目前有 PPT WORD PDF三种
在网上找了
很多使用poi 来解析的
但目前只做到了解析文字,且解析出来后,打开很慢,这样肯定不行
格式和图片表格 都解析不出来
很纠结
还有几十 用flexpaper 来转换成swf文件在预览显示
但是还是没能搞定,有些软件还不是免费或者开源的,不能随便用商业版的软件
越加纠结

谁有做好的Demo 啊
在线等啊
大侠
[解决办法]
没那么简单,最好是后台批处理把所上传文档都统一转为 PDF 或某种格式,前台预览时直接打开该PDF。

转换过程建议用工具,不要限于Java实现;自己用POI之类的进行解析实现效率太低了。
[解决办法]
得用到第三方插件,否则没办法做.我们公司都是用第三方插件,比如excel在IE中显示.估计你在搜索中没有例子,我都搜了个遍了
给你一个excel的例子,只能在IE中用,其他浏览器不支持.
重要的是这句
<OBJECT id="Spreadsheet1" classid="clsid:0002E559-0000-0000-C000-000000000046" name="Spreadsheet1" style="width:100%;height:421px" >

IPA : OWCVBA11.CHM 自己去下载玩玩吧


<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Untitled Page</title>


<script language="javascript">


function document.onkeydown(){
if(event.keyCode == 116){
event.keyCode = 0;
event.returnValue = false;
return;
}
}


if(window.Event){
document.captureEvents(Event.MOUSEUP);
}

function nocontextmenu(){
event.cancelBubble = true
event.returnValue = false;
return false;
}

function norightclick(e) {
if (window.Event){
if (e.which == 2
[解决办法]
e.which == 3)
return false;
}else if(event.button == 2
[解决办法]
event.button == 3) {
event.cancelBubble = true


event.returnValue = false;
return false;
}
}

document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others


function deleteRecord(){
var Spreadsheet1 = document.all("Spreadsheet1");
//var rowNum = Spreadsheet1.ActiveCell.Row;
//Spreadsheet1.Cells.Rows(rowNum).Delete();
var rowNums = Spreadsheet1.Selection.Rows;
rowNums.Delete();
}

function undo(){

var Spreadsheet1 = document.all("Spreadsheet1");
if(Spreadsheet1.CanUndo){
Spreadsheet1.Undo();
}else{
alert("无法执行撤销操作!");
}
}

function clearAll(){
var Spreadsheet1 = document.all("Spreadsheet1");
Spreadsheet1.Range("A2:DD500").Cells.Clear();
}

function Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel){
var cmContextMenu = new Array();
cmContextMenu[0] = ["撤销操作(&U)", "undo"];
cmContextMenu[1] = null;
cmContextMenu[2] = ["删除行记录(&D)", "deleteRecord"];
cmContextMenu[3] = ["清空所有记录(&A)", "clearAll"];
Menu.Value = cmContextMenu;
//Cancel.Value = true;
}

function Spreadsheet1_CommandExecute(Command, Succeeded){
if(Command == 'undo') undo();
if(Command == 'deleteRecord') deleteRecord();
if(Command == 'clearAll') clearAll();
}

function Spreadsheet1_BeforeKeyDown(KeyCode, Shift, Cancel){
//屏蔽剪切复制
if((Shift == 2) && (KeyCode == 88
[解决办法]
KeyCode == 67)) Cancel.Value = true;
}

function onload() {

var Spreadsheet1 = document.all("Spreadsheet1");
Spreadsheet1.EnableUndo = false;
//设置显示工具栏
Spreadsheet1.DisplayToolbar = false;

//设置调整大小
Spreadsheet1.ActiveWindow.EnableResize = false;

//alert(arr.length);

//显示“命令和选项”窗口
Spreadsheet1.AllowPropertyToolbox = false;

//设置单个sheet属性
Spreadsheet1.DisplayWorkbookTabs = true;

var aa = eval("Spreadsheet1"+"_BeforeContextMenu");


//调用事件
Spreadsheet1.attachEvent("BeforeContextMenu",aa);
Spreadsheet1.attachEvent("CommandExecute",Spreadsheet1_CommandExecute);
Spreadsheet1.attachEvent("BeforeKeyDown",Spreadsheet1_BeforeKeyDown);


Spreadsheet1.ActiveSheet.Cells.Clear();
Spreadsheet1.ActiveSheet.Cells(2, 1).Value = "Car";
Spreadsheet1.ActiveSheet.Cells(3, 1).Value = "Sport-Utility";
Spreadsheet1.ActiveSheet.Cells(4, 1).Value = "Truck";
Spreadsheet1.ActiveSheet.Cells(5, 1).Value = "Minivan";


Spreadsheet1.ActiveSheet.Cells(1, 2).Value = "1998";
Spreadsheet1.ActiveSheet.Cells(2, 2).Value = 0.2;
Spreadsheet1.ActiveSheet.Cells(3, 2).Value = 0.06;
Spreadsheet1.ActiveSheet.Cells(4, 2).Value = 0.17;
Spreadsheet1.ActiveSheet.Cells(5, 2).Value = 0.13;

Spreadsheet1.ActiveSheet.Cells(1, 3).Value = "1999";
Spreadsheet1.ActiveSheet.Cells(2, 3).Value = 0.38;
Spreadsheet1.ActiveSheet.Cells(3, 3).Value = 0.82;
Spreadsheet1.ActiveSheet.Cells(4, 3).Value = 0.28;
Spreadsheet1.ActiveSheet.Cells(5, 3).Value = 0.62;

Spreadsheet1.ActiveSheet.Cells(1, 4).Value = "2000";
Spreadsheet1.ActiveSheet.Cells(2, 4).Value = 0.42;
Spreadsheet1.ActiveSheet.Cells(3, 4).Value = 0.12;
Spreadsheet1.ActiveSheet.Cells(4, 4).Value = 0.55;
Spreadsheet1.ActiveSheet.Cells(5, 4).Value = 0.25;


//设置保护区域
var shtCurrent = Spreadsheet1.ActiveSheet;

var rngUsed = shtCurrent.UsedRange;

var rngCurRegion = Spreadsheet1.Range("a2").CurrentRegion;

alert(rngUsed.Rows.Count);
//alert(rngUsed.Columns.Count);
//Spreadsheet1.Cells.Rows(2).Delete();

//shtCurrent.range(shtCurrent.Cells(5, 1),shtCurrent.Cells(5, 4)).Locked = false;


//shtCurrent.Protection.Enabled = true;

Spreadsheet1.EnableUndo = true;

//alert(Spreadsheet1.XMLData);

//alert(Spreadsheet1.ActiveWorkbook.XmlMaps(1).MapData);


}

</script>

</head>

<body onload="return onload()" >

<form id="form1" runat="server">
<div>
</div>
</form>

<OBJECT id="Spreadsheet1" classid="clsid:0002E559-0000-0000-C000-000000000046" name="Spreadsheet1" style="width:100%;height:421px" >

<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td bgcolor="#336699" height="25" width="10%"> </td>
<td bgcolor="#666666" width="85%">
<font face="宋体" color="white" size="4">
<b>  缺少 Microsoft Office Web Components</b>
</font>
</td>
</tr>
<tr>
<td bgcolor="#cccccc" width="15"> </td><td bgcolor="#cccccc" width="500px"><br>
<font face="宋体" size="2">此网页要求 Microsoft Office Web Components。
<p align="center">


<a href="C:/IUware Online/Microsoft Office Professional Enterprise Edition 2003/files/owc11/setup.exe"> 单击此处安装 Microsoft Office Web Components。
</a>.
</p>
</font>
<p>
<font face="宋体" size="2"> 此网页同时要求 Microsoft Internet Explorer 5.01 或更高版本。</p>
<p align="center"/><a href="http://www.microsoft.com/windows/ie/default.htm"> 单击此处安装最新的 Internet Explorer</a>.
</font><br> 
</td>
</tr>
</table>

</OBJECT>
</body>

</html>


[解决办法]
1.用openoffice把pdf转换成.swf
2.jsp中使用flexpaper打开.swf文件
[解决办法]
引用:
1.用openoffice把pdf转换成.swf
2.jsp中使用flexpaper打开.swf文件


正确。
我说一下之前我是怎么处理的:
1.用户上传了word,xls,ppt,pdf这类的原始文档后,在后台处理,如果不是pdf格式,都用openoffice转换成pdf格式
2.在第一步中得到的pdf文件,可以通过 pdf2swf.exe 这个工具转换成相应的swf
3.在jsp中用 flexpaper可以预览上一步生成的swf

OK
[解决办法]
我们项目都是第三方工具 付费的
------解决方案--------------------


请问你的问题解决了吗? 我也要解决这个问题,能不能帮帮我?
[解决办法]
参考openOffice swftools 以前做过
发你链接http://www.cnblogs.com/star-studio/archive/2011/12/09/2281807.html

[解决办法]
应该也有一些weboffice的插件吧,公司用的商业版的金格控件,兼容什么的都不错
[解决办法]
不要那么麻烦吧,java是可以实现的啊。我做过的哦:


window.onload = function() {

document.getElementById("divwrapped").innerHTML = "<iframe width='100%' height='100%' src='ajax_pageload.action?path=" + encodeURIComponent(filepath) + "' />"; };


然后在后台:(只写关键代码哦)
response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// 设置下载头信息
try {
TransmitFile.downLoad(response, strUrl);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();



/**
* 下载文件
* @param config
* @param response
* @param downLoadFileName
*/
public static void downLoad(HttpServletResponse response,String filePath){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
String filename=filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis =new BufferedInputStream(new FileInputStream(filePath));
bos=new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesread);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();


} catch (IOException e) {
e.printStackTrace();
}
}


}


[解决办法]
flexpaper这个插件,我个人觉得还是挺好用的,之前有用过。
[解决办法]
判断类型 若为office文件
1.通过 openoffice 将文件转换为 pdf
2.通过 swftools 讲转换的pdf文件转化为swf格式
3.通过flex网页浏览

[解决办法]
顶1L,我的建议是如果要求不是特别严格,统一转成pdf文档,前台页面用adobe reader的插件来在线预览pdf文档,我们刚做完这样的需求
[解决办法]
nb插件 AC_FL_RunContent

读书人网 >Java Web开发

热点推荐