java预览表格预览文档_Java在线预览文档最佳实践

/**

* 文档预览

*

* @param request

* @param response

* @throws IOException

*/

public void view(HttpServletRequest request, HttpServletResponse response) throws IOException {

try {

// 你的文档地址

String file = "http://view.xdocin.com/doc/preview.docx";

// XDOC文档预览服务地址

String xurl = "http://view.xdocin.com/xdoc";

// 预览参数

Map params = new HashMap();

// 获取预览结果url

params.put("_func", "url");

// 结果格式XML

params.put("_rformat", "xml");

// 文档地址

params.put("_xdoc", file);

// word文档是否以pdf方式显示,默认false

// params.put("_pdf", true);

// 水印文本,显示水印

// params.put("_watermark", "XDOC文档预览");

// 是否允许保存PDF,默认true

// params.put("_saveable", false);

// 是否允许打印PDF,默认true

// params.put("_printable", false);

// 是否允许选择复制内容,默认true

// params.put("_copyable", false);

// 是否显示底部工具条,默认true

// params.put("_toolbar", false);

// 自定义标题

// params.put("_title", "文档预览");

// 预览链接有效期,单位分钟,默认永久有效

// params.put("_expire", 30);

// 组合调用URL

StringBuffer sb = new StringBuffer();

sb.append(xurl);

Iterator it = params.keySet().iterator();

String key;

boolean first = true;

while (it.hasNext()) {

key = it.next();

sb.append(first ? '?' : '&');

sb.append(java.net.URLEncoder.encode(key, "UTF-8"));

sb.append('=');

sb.append(java.net.URLEncoder.encode(params.get(key).toString(), "UTF-8"));

first = false;

}

// 获取预览结果URL,跳转

URL url = new URL(sb.toString());

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setValidating(false);

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(url.openStream());

document.getDocumentElement().normalize();

Element root = document.getDocumentElement();

if (root.getAttribute("success").equals("true")) {

// 预览结果URL

String viewUrl = root.getElementsByTagName("result").item(0).getTextContent();

// 跳转

response.sendRedirect(viewUrl);

} else {

throw new RuntimeException(root.getElementsByTagName("error").item(0).getTextContent());

}

} catch (Exception e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值