blob字段存储着world内容,并显示在页面上
整体思路:取出blob字段并写入world里,然后通过openoffice将world转化成pdf并显示在页面
需安装openoffice,参考:http://blog.csdn.net/youthon/article/details/32717471
下载Apache_OpenOffice_4.1.3_Win_x86_install_zh-CN.exe
下载jodconverter-2.2.2.zip,并导入相关jar包
commons-cli-1.2.jar
commons-io-1.4.jar
jodconverter-2.2.2.jar
jodconverter-cli-2.2.2.jar
juh-3.0.1.jar
jurt-3.0.1.jar
ridl-3.0.1.jar
slf4j-api-1.5.6.jar
slf4j-jdk14-1.5.6.jar
unoil-3.0.1.jar
xstream-1.3.1.jar
页面显示需引用pdfobject.js
安装完后需要在cmd里执行cd C:\Program Files (x86)\OpenOffice 4\program进入文件夹
然后执行soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard启动服务(不启动不会进行转换)
action:
private String ZW_PATH="E:/Users/testupload/UpFiles/";//world保存路径,可改为配置文件获取
private String ZW_PDF_PATH="E:/Users/admin/apache-tomcat-7/webapps/wz/";//pdf保存路径,可改为配置文件获取
/**
*跳转至文档查看页面
**/
@Action(value = "wendang", results = { @Result(name = "haveZw", location = "wendangForZw.jsp")})
public String toWendang(){
Map zwMap=bgsService.getZw(paramMap);
if(zwMap!= null && zwMap.get("FILEBODY")!=null){
Blob blob=(Blob)zwMap.get("FILEBODY");//获取存储的blob字段
String wh=(String)zwMap.get("WH");//获取文件名
//生成doc文件,返回地址
String zw=convertBlobToString(blob,wh);//将blob字段值写入world并返回存储地址
String pdfPath=ZW_PDF_PATH+wh+".pdf";
office2PDF(zw,pdfPath);//将world转成pdf
request.setAttribute("wh", wh);
return "haveZw";
}
}
util:
/**
* 根据blob获取发文正文
* @param blob blob字段
* @param wh 文件名
* @return
* @throws SQLException
*/
public String convertBlobToString(Blob blob,String wh) throws SQLException{
String path=ZW_PATH+wh+".doc";
FileOutputStream fop = null;
File file;
try {
file = new File(path);
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = blob.getBytes((long)1, (int)blob.length());
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
/**
* world转pdf
* @param sourceFile world文件地址
* @param destFile pdf存放地址
* @return
*/
public int office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到源文件, 则返回-1
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
return 0;
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
页面:
<html>
<div id="test" style="height:400px">
</div>
</html>
<script type="text/javascript" src="${ctx}/static/js/pdfobject.js"></script>
<script type="text/javascript">
var ctx = "${ctx}";//当前地址http://XXXXXXXX:8080
var wh="${wh}";
$(function(){
var success = new PDFObject({ url: ctx+"/"+wh+".pdf"}).embed("test");
});
</script>