blob字段存储world内容在页面上显示

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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值