在线实现Word、Excel、PDF在线预览,无法显示

nonoapologize 2015-08-20 11:39:29
openoffice装了,服务也开了,swftools也装了,权限也授了,可是访问页面一直在更新,不知道那地方出问题了,大神求助,谢谢。

项目构架:

onlineView.jsp

<%
String swfFilePath=session.getAttribute("swfpath").toString();
%>
<script type="text/javascript" src="<%=basePath%>js/main/jquery.js"></script>
<script type="text/javascript" src="<%=basePath%>js/main/flexpaper_flash.js"></script>
<script type="text/javascript" src="<%=basePath%>js/main/flexpaper_flash_debug.js"></script>
<script type="text/javascript" src="<%=basePath%>js/main/flexpaper.js"></script>
<script type="text/javascript">
var fp = new FlexPaperViewer(
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('<%=swfFilePath%>'),
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 : 'SinglePage',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'en_US'
}});
</script>
</div>
</body>
</html>

detailKnowledge.jsp

<%
Knowledge knowledge=(Knowledge)request.getAttribute("knowledge");
String filename=PropertiesUtil.getProperty("fileManageUpLoad")+"/"+knowledge.getFilecode();
//调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法
OnlineView d = new OnlineView(filename);
//调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;
d.conver();
//调用getswfPath()方法,打印转换后的swf文件路径
System.out.println(d.getswfPath());
//生成swf相对路径,以便传递给flexpaper播放器
String swfpath = PropertiesUtil.getProperty("fileManageUpLoad")+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));
System.out.println(swfpath);
//将相对路径放入sessio中保存
session.setAttribute("swfpath", swfpath);
out.println("<hr>");
%>
跳转的action
form name="viewForm" id="form_swf" action="<%=basePath%>knowledge/onlineView.html?id=${knowledge.id }" method="POST">
<input type='submit' value='预览' class='BUTTON SUBMIT'/>
</form>

controller:

@Controller
@RequestMapping(value="/knowledge")
public class KnowledgeCtl {
@RequestMapping(value="/onlineView",method=RequestMethod.POST)
public ModelAndView onlineView(@RequestParam("id")String id)
{
ModelAndView model=new ModelAndView("/equipment/onlineView");
return model;
}
}

转换的util:

public class OnlineView {
private static final int environment = 1;// 环境 1:windows 2:linux
private String fileString;// (只涉及pdf2swf路径问题)
private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;

public OnlineView(String fileString) {
ini(fileString);
}

/**
* 重新设置file
* @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}
/**
* 初始化
* @param fileString
*/
private void ini(String fileString) {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
}

/**
* 转为PDF
* @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
} catch (java.net.ConnectException e) {
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
*/
@SuppressWarnings("unused")
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
Process p = r.exec("E:/swftoolsf2swf.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已经存在不需要转换****");
}
}

public static String loadStream(InputStream in) throws IOException {

int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();

while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}

return buffer.toString();
}
/**
* 转换主方法
*/
@SuppressWarnings("unused")
public boolean conver() {

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) {
e.printStackTrace();
return false;
}
if (swfFile.exists()) {
return true;
} else {
return false;
}
}
/**
* 返回文件路径
* @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}

}
/**
* 设置输出路径
*/
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
if (!outputPath.equals("")) {
String realName = fileName.substring(fileName.lastIndexOf("/"),
fileName.lastIndexOf("."));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + ".swf");
} else {
swfFile = new File(outputPath + realName + ".swf");
}
}
}




}

...全文
1962 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
SSHorSSM 2016-12-01
  • 打赏
  • 举报
回复
首先是OpenOffice的进程关闭的问题,原因比较多,大多数是文件转换失败,导致服务崩溃。 如文件过大,字符集不识别等。 通过两个批处理文件,监控soffice.exe进程,如果没有,则重新启动。office转换pdf的方法没有返回值,在某些情况下 转换失败 无法判断是否转换成功,在出现上面的情况下,就需要规定转换时间,在规定时间内必须返回,如无返回,则认为转换失败。
xin_wenjie 2015-12-15
  • 打赏
  • 举报
回复
请问这个问题解决;了吗 现在我也遇到了 谢谢
nonoapologize 2015-09-16
  • 打赏
  • 举报
回复
onlineView.jsp 的js最后添加 swfobject.embedSWF("knowledge/FlexPaperViewer.swf","cb","500","500","9.0.0","knowledge/expressInstall.swf", fp); 只支持xls文件预览
jonesmiller 2015-09-02
  • 打赏
  • 举报
回复
直接去openoffice官网论坛找support解决吧。



________________
C# PDF convert
C# PDF to Word convert
春春 2015-08-21
  • 打赏
  • 举报
回复
把那个flash放根目录试试
nonoapologize 2015-08-21
  • 打赏
  • 举报
回复
转换没有问题,在后台有相应的输出文件,也没有报错信息,就是一直在刷新的状态
General-M 2015-08-21
  • 打赏
  • 举报
回复
确定文件有没有转换成功,二确定openoffice有没有获取到你转换好的文件
rumlee 2015-08-21
  • 打赏
  • 举报
回复
先确定是转换的有问题,还是查看有问题。 一步一步调试一下就好了。
nonoapologize 2015-08-21
  • 打赏
  • 举报
回复
放到根目录是404,他访问的的目录是knowledge下的,所以必须放在那个目录

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧