请问怎么从数据库读取word显示在JSP页面上
我的数据库是SQLServer2000的,数据字段类型是image,里面保存了word,已经把word存到image字段里面了.就是不知道怎么读取然后显示在JSP页面上,...
我的数据库是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类型的字段.现在就是读取不了,出现乱码.
换过编码了,还是不行. 展开
<%@ 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类型的字段.现在就是读取不了,出现乱码.
换过编码了,还是不行. 展开
2个回答
展开全部
package com.xaccp.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xaccp.daoImpl.UsersDaoImpl;
import com.xaccp.entity.Users;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter(userName);
String password=request.getParameter(password);
Users u=new Users();
u.setUserName(userName);
u.setPassword(password);
UsersDaoImpl dao=new UsersDaoImpl();
if(dao.checkLogin(u))
{
Users LoginUser=dao.findUsersByName(userName);
HttpSession session=request.getSession();
session.setAttribute(LoginUser, LoginUser);
response.sendRedirect(/addressBook/SelectServlet);
}
else
{
response.sendRedirect(/addressBook/index.jsp);
}
}
}
上面这上登录验证的Servlet
下面上配置web.xml配置文件
servlet
descriptionThis is the description of my J2EE component/description
display-nameThis is the display name of my J2EE component/display-name
servlet-nameLoginServlet/servlet-name
servlet-classcom.xaccp.servlet.LoginServlet/servlet-class
/servlet
下满上登录页面的form
form name=login action=loginServlet method=post onsubmit=return checkLongin()
TABLE width=100%
style=cellpadding: 0px; cellspacing: 0px; margin-top: 0px; margin-Left: 0px
style=table-layout: fixed;WORD-BREAK: break-all; WORD-WRAP: break-word
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%用户名:/TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=text name=userName value=%=userName %//TD
TD style=color:#4c4743;line-height:160%;a href=register.jsp注册/a/TD
/TR
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%密nbsp;nbsp;码/TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=password name=password //TD
/TR
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%input type=submit value=提交 //TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=reset value=重置 //TD
/TR
/TABLE
/td
td width=47 valign=top background=image/flower/m_bg.jpgnbsp;/td
/tr
/table
/form
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xaccp.daoImpl.UsersDaoImpl;
import com.xaccp.entity.Users;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter(userName);
String password=request.getParameter(password);
Users u=new Users();
u.setUserName(userName);
u.setPassword(password);
UsersDaoImpl dao=new UsersDaoImpl();
if(dao.checkLogin(u))
{
Users LoginUser=dao.findUsersByName(userName);
HttpSession session=request.getSession();
session.setAttribute(LoginUser, LoginUser);
response.sendRedirect(/addressBook/SelectServlet);
}
else
{
response.sendRedirect(/addressBook/index.jsp);
}
}
}
上面这上登录验证的Servlet
下面上配置web.xml配置文件
servlet
descriptionThis is the description of my J2EE component/description
display-nameThis is the display name of my J2EE component/display-name
servlet-nameLoginServlet/servlet-name
servlet-classcom.xaccp.servlet.LoginServlet/servlet-class
/servlet
下满上登录页面的form
form name=login action=loginServlet method=post onsubmit=return checkLongin()
TABLE width=100%
style=cellpadding: 0px; cellspacing: 0px; margin-top: 0px; margin-Left: 0px
style=table-layout: fixed;WORD-BREAK: break-all; WORD-WRAP: break-word
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%用户名:/TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=text name=userName value=%=userName %//TD
TD style=color:#4c4743;line-height:160%;a href=register.jsp注册/a/TD
/TR
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%密nbsp;nbsp;码/TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=password name=password //TD
/TR
TR
TD style=color:#4c4743;line-height:160%; valign=top
width=30%input type=submit value=提交 //TD
TD style=color:#4c4743;line-height:160%; valign=topinput
type=reset value=重置 //TD
/TR
/TABLE
/td
td width=47 valign=top background=image/flower/m_bg.jpgnbsp;/td
/tr
/table
/form
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
换个编码方式试试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询