我的数据库是SQLServer2000的,数据字段类型是image,里面保存了word,已经把word存到image字段里面了.就是不知道怎么读取然后显示在JSP页面上,请兄弟们帮忙解决.谢谢
<%@ page language="java" contentType="application/msword;charset=UTF-8" %>
<%@ page import="java.io.*,java.sql.*"%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://192.168.1.101:1433;DatabaseName=wic";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(url, "sa", "sa");
stmt = con.createStatement();
String sql = "SELECT SSZS FROM SSJYB WHERE SSJYBH ='Z10-000001-1' AND YPMC='摆件'";
rs = stmt.executeQuery(sql);
response.setContentType("application/msword;charset=UTF-8");
while(rs.next()) {
InputStream in = rs.getBinaryStream("SSZS");
OutputStream ot=response.getOutputStream();
byte[] b = new byte[1024];
int len=0;
while((len=in.read(b,0,b.length))!=-1){
ot.write(b,0,len);
}
if(in!=null){
in.close();
}
if(ot!=null){
ot.close();
}
out.clear();
out = pageContext.pushBody();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
%>
其中SSZS字段就是保存word文档的,SSZS是SQLServer2000的一个image类型的字段.现在就是读取不了,出现乱码.
换过编码了,还是不行.
这些Low level的IO操作,放在Servlet中去。
除了这2句,其他的都可以放在Servlet中去:
<%@ page language="java" contentType="application/msword;charset=UTF-8" %>
<%@ page import="java.io.*,java.sql.*"%>
谢谢到我的博客中提问。
首先你的错误是什么?贴出来看看
其次,你将JSP和Servlet代码混淆了,写在了一起。建议你分开写。比如:
1、你在JSP中使用OutputStream ot=response.getOutputStream(); 是有问题的。
2、response.setContentType("application/msword;charset=UTF-8"); 和JSP标签中的
<%@ page language="java" contentType="application/msword;charset=UTF-8" %>
也是重复的。
[code="java"] InputStream in = rs.getBinaryStream("SSZS");
OutputStream ot=response.getOutputStream();
byte[] b = new byte[1024];
int len=0;
while((len=in.read(b,0,b.length))!=-1){
ot.write(b,0,len);
} [/code]
如果你调试并认为是编码出现问题的话,我分析应该问题是出现在这里。
因为你取出的InputStream是字节流,极易出现一些编码问题。
你可以尝试着换一种方式,使用inputstreamreader.
inputstreamreader的用法:
[url]http://zhou-hong-liang.iteye.com/blog/300048[/url]
inputstreamreader的编码设置:
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); //将UTF-8改为其它编码设置如GBK等试试
报错了没? 如果就是一个乱码问题的话 可以试试 [quote]InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); //将UTF-8改为其它编码设置如GBK等试试[/quote]
换其它编码试试,在Window平台基本上不可能是utf-8编码
[code="java"] InputStream in = rs.getBinaryStream("SSZS");
OutputStream ot=response.getOutputStream();
byte[] b = new byte[1024];
int len=0;
while((len=in.read(b,0,b.length))!=-1){
ot.write(b,0,len);
} [/code]
现在是将数据库输入流的数据直接输出了,你分两步。
先将数据库输入流中的数据在控制台打印下,看看是不是乱码。
如果乱码,那是与数据库交互时出错,
反之,是页面展示问题
[quote]问题补充:
xiaolongfeixiang 写道
................................
你的第1点指出,在JSP中使用OutputStream ot=response.getOutputStream(); 是有问题的 ,请问为什么不能这样子用?[/quote]
你看看错误就知道了吧:
[color=blue]
[b]严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response [/b][/color]