基本步骤如下:
第一步:下载需要的jar包(本人喜欢去www.jar114.com)
第二步:把它放到你的classpath路径下面。或者放到lib目录下(web程序)
第三步:编写读取程序(ReadWord.java)
前提是你的E盘下有个Strust.doc文件。
import java.io.File;
import java.io.FileInputStream;
import org.textmining.text.extraction.WordExtractor;
public class WordReader {
public static String readDoc(String doc) throws Exception {
// 创建输入流读取doc文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
return text;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
String text = WordReader.readDoc("E:/Strust.doc");
System.out.println(text);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
这样就简单的完成了从word中读取数据了。你可以把它生成一个txt的文件。
在jsp页面读取word文档内容
用word比较麻烦,而且代码很多会变形。
建议。就直接把word转成html。然后jsp调用出来显示,这样格式不会变。
或者保存进数据库,然后调用出来放到xml模版里直接生成html
打字不容易,感觉ok就给个好评采纳吧,谢谢。。。。。
你说的转成html格式的我也试了,里面的图片显示不出来啊
你图片有没有传到网站空间,路径对不对。
请问怎么从数据库读取word显示在JSP页面上
还是给你个例子来的比较实际,sql操作台二进制数据
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*,java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*"%>
<html>
<body>
<%
/*
drop table imagetable;
create table imagetable
(
id int not null,
image image,
primary key (id)
)
*/
/*
//================ 一 、将文件写入到数据库的大字段中begin=====================
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url ="jdbc:microsoft:sqlserver://127.0.0.1:1433;DataBaseName=zl";
Connection conn= DriverManager.getConnection(url,"sa","1234");
File file = new File("e:/1.jpg");
FileInputStream is=new FileInputStream(file);
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO imagetable (id,image)" + "VALUES (?, ?)");
stmt.setInt(1, 1);
stmt.setBinaryStream(2, is,(int)file.length());
stmt.executeUpdate();
stmt.close();
is.close();
out.println("恭喜你,你成功加入一张图片!");
//===============将文件写入到数据库的大字段中end=========================
*/
/*
//====================== 二、jsp显示服务器硬盘图片示例 begin==============
FileInputStream is=new FileInputStream("e:/1.jpg");
response.reset();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len=is.read(buffer))>0){
sos.write(buffer,0,len);
}
sos.flush();
sos.close();
//=======================jsp显示服务器硬盘图片示例 end===================
*/
//===================== 三、将数据库的大字段图片还原到本地,并在网页上显示begin==============
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost:3306/test?user=root&password=eastsoftweb";
Connection conn= DriverManager.getConnection(url);
java.io.File file = new File("d:/temp/db.jpg");
FileOutputStream os=new FileOutputStream(file);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select nid,image from imagetable where nid=1");
rs.next();
byte[] buffer=rs.getBytes(2);
stmt.close();
os.write(buffer);
os.flush();
os.close();
out.println("query end");
//网页上显示
response.reset();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
sos.write(buffer);
sos.flush();
sos.close();
//======================将数据库的大字段图片还原到本地,并在网页上显示end===================
/*
//======================四、生成缩略图begin==============================
File file = new File("d:/temp/1.JPG");
String newurl="d:/temp/2.jpg"; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=200;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0; //得到源图长
int tempsize;
float tempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
//========================生成缩略图end================================
*/
%>
jsp中如何读取word里面的内容
手写一个,希望对你有帮助
<%@page contentType="text/html;charset=utf-8"inport="java.io.*"%>
<%
try{
InputSteam in=getServletContext().getResourceAsStrean("/test.doc");
String file="";
int temp=0;
while((temp=in.read())!=-1)
{
file+=(char)temp;
}
in.close;
out.println(new String(file.getBytes("iso-8859-1")));
out.flush();
}
catch(Exception e)
{
e.printStackTrance();
}
%>
请问怎么从数据库读取word显示在JSP页面上
换个编码方式试试
Java如何从oracle中读取word文档,并保持原格式显示到JSP页面中
网上这样的东西很多,http://www.360doc.com/content/06/0303/11/4131_74876.shtml
jsp读取word,ppt,pdf
把PDF文件写入response流里面就可以了!
方法有很多,这里给个独立又简单的例子:
Java代码
1.package com.zhaipuhong.j2se.pdf;
2.
3.import java.io.IOException;
4.import java.util.Date;
5.
6.import javax.servlet.ServletException;
7.import javax.servlet.http.HttpServlet;
8.import javax.servlet.http.HttpServletRequest;
9.import javax.servlet.http.HttpServletResponse;
10.
11.import com.lowagie.text.Document;
12.import com.lowagie.text.DocumentException;
13.import com.lowagie.text.Paragraph;
14.import com.lowagie.text.pdf.PdfWriter;
15.import com.lowagie.text.pdf.BaseFont;
16.import com.lowagie.text.pdf.PdfPTable;
17.import com.lowagie.text.pdf.PdfPCell;
18.import java.awt.Color;
19.
20.public class PdfServlet extends HttpServlet {
21.
22. private static final long serialVersionUID = -6033026500372479591L;
23.
24. public void doGet (HttpServletRequest request, HttpServletResponse response)
25. throws IOException, ServletException {
26.
27. // step 1 建立文档对象
28. Document document = new Document();
29. try {
30. //设置文档相应类型
31. response.setContentType("application/pdf");
32. PdfWriter.getInstance(document, response.getOutputStream());
33.
34.
35. // step 3 打开文档
36. document.open();
37. //支持中文
38. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
39. com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
40. Paragraph pragraph=new Paragraph("你好", FontChinese);
41.
42.
43. // step 4 向文档中添加内容
44. document.add(pragraph);
45. document.add(new Paragraph(" Hello World !"));
46. document.add(new Paragraph("Date 时间"+new Date().toString()));
47. document.add(new Paragraph(new Date().toString()));
48. document.add(new Paragraph(new Date().toString()));
49.
50.
51.
52. PdfPTable table = new PdfPTable(3);
53. PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
54. cell.setColspan(3);
55. table.addCell(cell);
56. table.addCell("1.1");
57. table.addCell("2.1");
58. table.addCell("3.1");
59. table.addCell("1.2");
60. table.addCell("2.2");
61. table.addCell("3.2");
62. cell = new PdfPCell(new Paragraph("cell test1"));
63. cell.setBorderColor(new Color(255, 0, 0));
64. table.addCell(cell);
65. cell = new PdfPCell(new Paragraph("cell test2"));
66. cell.setColspan(2);
67. cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
68. table.addCell(cell);
69. document.add(table);
70.
71. }catch(DocumentException de) {
72. de.printStackTrace();
73. System.err.println("document: " + de.getMessage());
74. }
75.
76. // step 5: 关闭文档对象
77. document.close();
78. }
79.
80. //支持中文
81. public Paragraph getChineseString(String chineseString){
82. Paragraph pragraph=null;
83. BaseFont bfChinese = null;
84. try {
85. bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
86. BaseFont.NOT_EMBEDDED);
87. com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
88. 12, com.lowagie.text.Font.NORMAL);
89. pragraph = new Paragraph(chineseString, FontChinese);
90. }
91. catch (Exception ex) {
92. ex.printStackTrace();
93. }
94. return pragraph;
95. }
96.}
jsp读取word不显示表格
完全不可能嘛
转载请注明出处51数据库 » jsp读取word jsp读取word内容
花式撸管华北区总冠军