从数据库取图片(库中有多张不同的图片,但显示的是同一张)  

从数据库取图片(库中有多张不同的图片,但显示的是同一张)  

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="com.jspsmart.upload.*,java.io.*,java.util.*,java.sql.*,java.servlet.*;"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档 </title>
</head>

<body>
<%//---------------显示全部图片---------------------
Connection con=null;
   
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String url="jdbc:odbc:magic";
con=DriverManager.getConnection(url,"sa","aa");
Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from userPhoto");
    while(rs.next())
{%>

<p align="center">
<table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474">
    <tr>
      <td width="150"> <div align="left"> <p> <small> <font face="Verdana">姓名:&nbsp; </font> </small> </td>
      <td width="324"> <small> <font face="Verdana">
        <%String n=rs.getString("userName");
out.print(n);
%>
      <br> </font> </small> </td>
    </tr>
    <tr>
      <td> <small> <font face="Verdana">照片:&nbsp; </font> </small> </td>
      <td> <small>
<img src="ShowImage?username= <%=n%>" width="200" height="80">
</td>
     
    </tr>
    <tr>
      <td> <small> <font face="Verdana">年龄: </font> </small> </td>
      <td> <small> <font face="Verdana"> <%=rs.getString("age")%> </font> </small> </td>
    </tr>
   
  </table>
  <%}%>
</p>
</body>
</html>

ShowImage.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
public class ShowImage extends HttpServlet {
//驱动类名
    private String driver_class= "sun.jdbc.odbc.JdbcOdbcDriver";
    //连接字符串
    private String connect_string="jdbc:odbc:magic";
    Connection conn = null;
    ResultSet rs = null;
    Statement stmt = null;
    private String SQLString = ""; //定义查询语句
    private InputStream in = null; //定义输入流
    private int len = 10 * 1024 * 1024; //定义字符数组长度
    public void init() throws ServletException {
       
           
       
    }  public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String username= new String("");
        try {
        username = codeToString(request.getParameter("username"));
            SQLString = "select userPhoto from userPhoto where userName='" + username+"'";
    Class.forName(driver_class);
            conn = DriverManager.getConnection(connect_string,"sa","aa");
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SQLString);
     
        //将图片流读入字符数组中,并显示到客户端
       
            if (rs.next()) {
                in = rs.getBinaryStream("userPhoto");
                response.reset(); //返回在流中被标记过的位置
                response.setContentType("image/jpg"); //或gif等
                OutputStream toClient = response.getOutputStream();
                byte[] P_Buf = new byte[len];
                int i;
                while ((i = in.read(P_Buf)) != -1) {
                    toClient.write(P_Buf, 0, i);
                }
                in.close();
                toClient.flush(); //强制清出缓冲区
                toClient.close();
            } else {


            }
            rs.close();
        } catch (Exception e) {

        }
    }
    //清除所有资源
    public void destroy() {
        try {
            conn.close();
        } catch (SQLException e) {
            System.err.println("aq.executeUpdate:" + e.getMessage());

        }
    }
    //处理中文字符串的函数
    public String codeToString(String str)
{
  String s=str;
try
    {
    byte tempB[]=s.getBytes("ISO-8859-1");
    s=new String(tempB);
    return s;
  }
  catch(Exception e)
  {
  return s;
  } 
}
}
应该是客户端(浏览器)的缓冲问题——你虽然是不同的图像,但是URL都是一样的。