看下下面的示例
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;public class photo extends HttpServlet
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf); try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
conn=DriverManager.getConnection(url);
pstmt=conn.prepareStatement("select photo from person where id=?");
}catch(SQLException e){System.err.println("Something is error!");}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
os=new DataOutputStream(response.getOutputStream());
is=rs.getBinaryStream("photo");
}catch(SQLException e){}
while((length=is.read(buffer))!=-1)
{
os.write(buffer,0,length);
os.flush();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
public void destroy()
{
try{
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e){}
}
}
在JSP里的调用方式:
<img src="/homepage/servlet/photo?id=<%=id%>">
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;public class photo extends HttpServlet
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf); try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
conn=DriverManager.getConnection(url);
pstmt=conn.prepareStatement("select photo from person where id=?");
}catch(SQLException e){System.err.println("Something is error!");}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
os=new DataOutputStream(response.getOutputStream());
is=rs.getBinaryStream("photo");
}catch(SQLException e){}
while((length=is.read(buffer))!=-1)
{
os.write(buffer,0,length);
os.flush();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
public void destroy()
{
try{
rs.close();
pstmt.close();
conn.close();
}catch(SQLException e){}
}
}
在JSP里的调用方式:
<img src="/homepage/servlet/photo?id=<%=id%>">
还想请教:如果image对象中存的是word或者excel文档呢?
即改images/*为Application/msword(Application/msexcel)试试。在ASP里可以,JSP里没有用过,应该也可以的。
import java.io.*;
import java.sql.*;import javax.servlet.*;
import javax.servlet.http.*;public class OpenExcel extends javax.servlet.http.HttpServlet {public void service(HttpServletRequest request, HttpServletResponse response) {
try {
byte b[] = new byte[102400];
int length = 0;
InputStream in = null; //连接数据库
Connection conDB = (new ConnectDB()).connectDB("auth");
String sql = " SELECT FILE_WORD FROM WORD_TABLE WHERE FILE_ID='01'"; PreparedStatement stmt = conDB.prepareStatement(sql);
ResultSet rs = null;
rs = stmt.executeQuery(); while (rs.next()) {
in = rs.getBinaryStream(1);
length = in.read(b);
}
response.setContentType("Application/msword");
DataOutputStream os = null;
os = new DataOutputStream(response.getOutputStream());
if (length != -1) {
os.write(b, 0, length);
os.flush();
}
} catch (Exception e) {
System.out.println("e: " + e);
}
}
}
在jsp中调用<img src=/servlet/OpenWord>,还是不能正常显示,请教应该怎样处理?
还有:javascript来创建相应的com对象怎么用呢?
javascript来创建相应的com对象我也不会拉,不好意思
内码转换?不清楚
链接我也做过,仍是乱码。
我用的环境:
sybase数据库,win2000 server,Websphere Application Server3.5
会不会是环境的问题呢?
response.setContentType("Application/msword"); //(Application/msexcel)
可以通过执行servlet在浏览器中打开word和excel了。
谢谢苯笨和草中宝!