word文档写到jsp页面
来源:互联网 发布:ro sql版本 gm账号 编辑:程序博客网 时间:2024/05/22 02:09
在做开发时用户想实现不下载附件的情况下直接浏览附件内容,就是在线阅读。然后想的一个办法就是把附件内容直接通过
l流直接读取到jsp页面。但文档的格式很乱。这次就先做个日记先保存供以后参考。
读取word稳定用到了poi组件所以要下载一个poi的jar包
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String filename = request.getParameter("filename");
filename = new String(filename.getBytes("iso8859-1"),"UTF-8");
StringBuilder fileContent=new StringBuilder();
String fullFileName = getServletContext().getRealPath("/WEB-INF/upload/" + filename);
String perfix=fullFileName.substring(fullFileName.lastIndexOf(".")+1);
if (perfix.equals("txt")) {
FileReader file = new FileReader(fullFileName);
//Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
/*reader = new InputStreamReader(new FileInputStream(file));*/
//InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader reader = new BufferedReader(file);
String lineTxt = null;
while ((lineTxt = reader.readLine()) != null) {
fileContent.append(lineTxt);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}else if(perfix.equals("docx")){
try {
XWPFDocument xwdoc = new XWPFDocument(POIXMLDocument.openPackage(fullFileName));
POIXMLTextExtractor ex = new XWPFWordExtractor(xwdoc);
fileContent.append(ex.getText().trim());
} catch (Exception e) {
e.printStackTrace();
}
}
//String content=String.valueOf(tempString);
request.setAttribute("fileContent", fileContent);
request.getRequestDispatcher("/fileUpload.jsp").forward(request, response);
}
jsp页面
<head>
<base href="<%=basePath%>">
<title>My JSP 'fileUpload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${fileContent}
</body>
</html>
- word文档写到jsp页面
- word文档直接嵌入到jsp页面
- jsp 页面生成word文档
- jsp页面中导出word文档
- 将jsp页面生成word、excel、pdf文档
- 将jsp页面生成word、excel、pdf文档
- [JavaScript]Html/Jsp页面加载Word文档方法
- 请教java在jsp页面显示word文档问题
- JSP生成WORD文档
- JSP生成WORD文档
- 使用js实现页面导出到word文档
- JS获取word文档内容到html页面
- jsp页面数据导出到excel和word
- 将word文档转成html获取数据库中对应值,并将jsp页面打印成word文档。
- jfreechart生成柱状图写到jsp页面并打印
- JSP生成WORD文档 1
- JSP生成WORD文档 1
- [JSP]JSP页面写注释
- 微信小程序开发之扫码 扫一扫 wx.scanCode()
- P2P之UDP穿透NAT的原理与实现
- scrapyd日志太多了,重启scrapyd
- java 循环嵌套组装数据性能和调整方法
- 【转载】Web前端开发推荐阅读书籍
- word文档写到jsp页面
- magento2 重置后台密码
- GTX1080 安装 cuda 8.0 + cuDNN5.1
- IDEA工具的安装与使用
- QQ625324327警惕下这个骗子
- HTML基础--基本概念
- ORACLE 触发器控制用户登录之权限限制
- Android 动画使用的常用场景
- 第9章:进一步的Type