jfinal pageOffice openOffice 待继续

pageoffice

// 文件编辑
public void documentEdit() throws Exception {
    HttpServletRequest request = getRequest();
    HttpServletResponse response = getResponse();
    String id = getPara("id");
    Upload upload = uploadService.findById(id);
    String file_path = upload.getStr("file_path");
    String file_name = upload.getStr("file_name");
    String src = file_path + File.separator +  file_name;
    src = "D:"+src.replace("\\\\","\\").replace("\\","\\\\");
    System.out.println("文件路径:"+src);
    String file_type = src.substring(src.lastIndexOf(".")+1);

    PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
    poCtrl.setServerPage(getRequest().getContextPath()+"/poserver.zz"); //此行必须
    poCtrl.addCustomToolButton("保存","Save",1);
    poCtrl.setSaveFilePage("documentSave?id="+id);//如要保存文件,此行必须
    OpenModeType openModeType = null;
    if ("doc".equals(file_type) || "docx".equals(file_type)) {
        openModeType = OpenModeType.docNormalEdit;
    }else if ("xls".equals(file_type) || "xlsx".equals(file_type)) {
        openModeType = OpenModeType.xlsNormalEdit;
    }else {
        renderJsonError1("只能在线编辑word和excel文档!", true);
    }
    poCtrl.webOpen(src, openModeType, getSessionUser().getUserNo()); //打开文件
    poCtrl.setTagId("PageOfficeCtrl1"); //此行必须
    setAttr("id", id);
    setAttr("pageOffice",poCtrl.getHtmlCode("PageOfficeCtrl1")); // 在 html 代码的 body 中需要出现 office 界面的位置插入下面的代码
}

// 文件保存
public void documentSave() {
    String id = getPara("id");
    HttpServletRequest request = getRequest();
    HttpServletResponse response = getResponse();
    FileSaver fs=new FileSaver(request,response);

    Upload upload = uploadService.findById(id);
    String file_path = upload.getStr("file_path");
    String src = file_path + File.separator ;
    src = "D:"+src.replace("\\\\","\\").replace("\\","\\\\");
    src = src +  fs.getFileName();
    fs.saveToFile(src);
    fs.close();
    renderNull();
}

前端

访问文件编辑的那个页面:
<script src="${rootpath}/jquery.min.js"></script>
<script src="${rootpath}/pageoffice.js" id="po_js_main"></script>
<a href="javascript:POBrowser.openWindowModeless('${rootpath}/broker/document/documentEdit?id=${item.id}', 'fullscreen=yes;')">在线编辑</a>

documentEdit
<div style="width:100%; height:100%;">
  ${pageOffice}
</div>
<script type="text/javascript">
    function Save() {
        document.getElementById("PageOfficeCtrl1").WebSave();
        // if (document.getElementById("PageOfficeCtrl1").CustomSaveResult == "ok") {
        //     document.getElementById("PageOfficeCtrl1").Alert('保存成功!');
        // }
        document.getElementById("PageOfficeCtrl1").Alert('保存成功!');
    }
</script>

openoffice 在线预览

  public void previewFile() throws IOException {
	HttpServletResponse response = getResponse();
	String file_path = getPara("file_path");
	String file_name = getPara("file_name");
	String id = getPara("id");
	if (StringUtil.isEmpty(file_path)) {
		Upload upload = uploadService.findById(id);
		file_path = upload.getStr("file_path");
		file_name = upload.getStr("file_name");
	}
	String src = file_path + File.separator +  file_name;
	// src = new String(src.getBytes("ISO-8859-1"),"UTF-8");
	System.out.println("文件路径:"+src);
	File file = new File(src);
	String pdf_file_path = src.substring(0,src.lastIndexOf("."))+".pdf";
	File pdfFile = new File(pdf_file_path);
	String host = AppConfig.properties.getProperty("openOfficeHost");
	Integer port = Integer.valueOf(AppConfig.properties.getProperty("openOfficePort"));
	OpenOfficeConnection connection = new SocketOpenOfficeConnection(host,port);
	connection.connect();
	DocumentConverter converter = "localhost".equals(host) || "127.0.0.1".equals(host)
			|| "0:0:0:0:0:0:0:1".equals(host) ? new OpenOfficeDocumentConverter(connection): new StreamOpenOfficeDocumentConverter(connection);
	converter.convert(file, pdfFile);
	if(pdfFile.exists()) {
		BufferedInputStream br = new BufferedInputStream(new FileInputStream(pdfFile));
		byte[] buf = new byte[1024];
		int len=0;
		response.reset();
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition","inline; filename=" + java.net.URLEncoder.encode(pdfFile.getName(), "UTF-8"));
		OutputStream out = response.getOutputStream();
		while ((len = br.read(buf)) > 0){
			out.write(buf, 0, len);
		}
		connection.disconnect();
		br.close();
		out.close();
		renderNull();
	}else {
		response.sendError(404,"file not found!");
		return;
	}
}
posted @ 2021-04-21 19:18  五字妹妹实在是棒  阅读(191)  评论(0)    收藏  举报
返回顶部