当前位置: 技术问答>java相关
从mysql数据库读出并显示图片的问题?(急)
来源: 互联网 发布时间:2015-10-20
本文导语: 我用servlet从mysql数据库读出并在页面显示图片,在IE下运行没有出错信息,可是图片却不能正常显示出来,看到的只是一个带有一个红叉叉的框。这是怎么回事啊 程序如下:import java.io.*; import java.net.*; import java.sql.*...
我用servlet从mysql数据库读出并在页面显示图片,在IE下运行没有出错信息,可是图片却不能正常显示出来,看到的只是一个带有一个红叉叉的框。这是怎么回事啊
程序如下:import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageDisplay extends HttpServlet
{
private Connection conn;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/sino","root","123456");
}
catch(Exception e){
}
}
public void destroy()
{
if (conn!=null)
{ try
{
conn.close();
}
catch (SQLException ignore) { }
}
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try{
// String id=request.getParameter("id");
// int idd=Integer.parseInt(id);
int idd=1;
String sql="select img from tfiles where ID=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,idd);
ResultSet rs=pstmt.executeQuery();
if (rs.next())
{response.setContentType("image/gif");
Blob blob = rs.getBlob("img");
InputStream in = blob.getBinaryStream();
OutputStream out=response.getOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len=in.read(buffer,0,1024))!=-1) {
out.write(buffer,0,len);
}
in.close();
out.flush();
out.close();
}
else{
throw new SQLException("image"+idd+"not found");
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
程序如下:import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageDisplay extends HttpServlet
{
private Connection conn;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/sino","root","123456");
}
catch(Exception e){
}
}
public void destroy()
{
if (conn!=null)
{ try
{
conn.close();
}
catch (SQLException ignore) { }
}
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try{
// String id=request.getParameter("id");
// int idd=Integer.parseInt(id);
int idd=1;
String sql="select img from tfiles where ID=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,idd);
ResultSet rs=pstmt.executeQuery();
if (rs.next())
{response.setContentType("image/gif");
Blob blob = rs.getBlob("img");
InputStream in = blob.getBinaryStream();
OutputStream out=response.getOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len=in.read(buffer,0,1024))!=-1) {
out.write(buffer,0,len);
}
in.close();
out.flush();
out.close();
}
else{
throw new SQLException("image"+idd+"not found");
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
|
你到网上搜索东方一帖子吧!很精典