为什么jsp显示从数据库里longraw字段生成的word文档是二进制流?
我在jsp界面想直接显示从数据库里longraw字段生成的word文档,可是显示的是二进制流,而同样的代码从数据库里longraw字段生成的word文档写在磁盘上却是正常的?
jsp代码如下:
<%@ page import="javax.sql.RowSet"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="comm" scope="session" class="ustc.tools.pub.comm" />
<jsp:useBean id="mCtlLongRawBean" scope="session" class="soft.tm.web.CtlLongRawBean" />
<%
RowSet rs=null;
String sSQL="select * from pstar.test_clob where id='97982'";
rs=comm.getRowSet(sSQL);
while(rs.next())
{
byte[] buffer = new byte[8 * 1924];
int bytesRead = 0;
int byteSum = 0;
String myfileName = "bb.doc";
InputStream is = rs.getBinaryStream("clobcol");
OutputStream fos=response.getOutputStream();
while ((bytesRead = is.read(buffer)) != -1)
{
byteSum += bytesRead;
fos.write(buffer, 0, bytesRead);
}
fos.close( );
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment; filename=" + myfileName);
rs.close();
}
//mCtlLongRawBean.ReadLongRawToWord();
%>
写磁盘的代码如下:
public void ReadLongRawToWord(){
try{
int bytesRead = 0;
int byteSum = 0;
byte[] buffer = new byte[8 * 1924];
FileOutputStream fos = new FileOutputStream("c:\\te333t.doc");
PreparedStatement ps = conn.prepareStatement("select * from pstar.test_clob where id='97982'");
ResultSet rs = ps.executeQuery();
if (rs != null)
{
while(rs.next())
{
InputStream is = rs.getBinaryStream("clobcol");
while ((bytesRead = is.read(buffer)) != -1)
{
byteSum += bytesRead;
fos.write(buffer, 0, bytesRead);
}
fos.close( );
}
rs.close( );
}
ps.close( );
}
catch(Exception e) {
System.err.print("文件读失败"+e.getMessage());
}
}