package name.aspx;import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ImageServlet extends HttpServlet  
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
try
{
String id = (String)req.getParameter("id");
Class.forName("org.gjt.mm.mysql.Driver");
//String url="jdbc:mysql://localhost/jspdev?user=root&password=";
String url="jdbc:mysql://localhost/xiaohuozhisql?user=xiaohuozhisql&password=6737038";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from image where id = " + id);
if(rs.next())
{
InputStream in = rs.getBinaryStream("image");
OutputStream out = res.getOutputStream();
byte[] bytes = new byte[1024];
while(in.read(bytes,0,1024) != -1)
{
out.write(bytes);
}
}
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
} public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
}
}

解决方案 »

  1.   

    你用servlet作。然后用jsp调用servlet就可以了!
    上面那个是我用mysql实现的。完全可以用!你把他换个驱动就行了
    调用的话就调用servelt映谢名加?id=*就可以了!
      

  2.   

    package util.db;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import oracle.sql.BLOB;
    import sun.jdbc.rowset.CachedRowSet;
    import util.db.OpenDbBean;
    import java.sql.*;public class GetPhoto extends HttpServlet {
    //Initialize global variables
    public void init() throws ServletException {
    } //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { String bmh = request.getParameter("bmh");
    String sort = request.getParameter("sort");
    if (bmh == null) {
    bmh = "";
    }
    if (sort == null) {
    sort = "master";
    } Connection conn = null;
    Statement ps = null;
    ResultSet rs = null;
    OpenDbBean db = new OpenDbBean();
    response.setContentType("image/jpeg");
    try {
    ServletOutputStream out = response.getOutputStream();
    String sql = null;
    if (sort.equals("master")) {
    sql = "select * from zs_s_photo where bmh='" + bmh + "'";
    }
    else if (sort.equals("doctor")) {
    sql = "select * from zs_b_photo where bmh='" + bmh + "'";
    }
    System.out.println(sql);
    conn = db.getConnection();
    ps = conn.createStatement();
    rs = ps.executeQuery(sql);
    if (rs.next()) {
    BLOB blob = (BLOB) rs.getBlob("binaryfile");
    int blobsize = (int) blob.length();
    InputStream pi = blob.getBinaryStream();
    byte[] blobbytes = new byte[blobsize];
    int bytesRead = 0;
    while ((bytesRead = pi.read(blobbytes)) != -1) {
    out.write(blobbytes, 0, bytesRead);
    }
    pi.close();
    out.flush();
    }
    else {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    finally {
    try {
    db.CleanConnection(conn, ps, rs);
    }
    catch (SQLException e1) {
    System.out.println(e1.getMessage());
    }
    }
    }}
      

  3.   

    为什么不用在数据库中存储图片路径的方法.
    1.在BusinessBean中读出数据库中的路径
    ArrayList arrImgPath = new ArrayList();
    while(rs.next()){
       arrImgPath.add(rs.getString("Img_Path"));
    }
    request.setAttribute("imgpath",arrImgPath);
    2.在jsp中调用BusinessBean showImg.显示
    <%@ taglib uri="struts-logic" prefix="logic" %>
    <logic:iterate id="element1" name="element" scope="request">
       <html:img  page="/showImg.do"  paramId="img" paramName="element1" border="1"  width="108" height="26" scope="request"/>
    </logic:iterate>
    3.BusinessBean showImg.java:
    String imgPath=request.getParameter("img"); 
    file = new File(imgPath);
    if (!file.isDirectory()&& file.exists()){
       length = (int)file.length();
    }
    res.setContentType("image/jpg");//假设是jpg图
    res.setHeader("Content-Disposition","inline");
    res.setContentLength(length);
    if (length > 0){
    int bufferSize = 32768;
    InputStream in = new FileInputStream(fullpath);
    ServletOutputStream outfile = res.getOutputStream();
    int chunkLen = bufferSize;
    if(chunkLen > length && length > 0)
       chunkLen = length;
    byte ioBuf[] = new byte[chunkLen];
    do{
    chunkLen = in.read(ioBuf);
    if(chunkLen <= 0)
      break;
    outfile.write(ioBuf, 0, chunkLen);
    length -= chunkLen;
    } while(length != 0);
    in.close();
    outfile.flush();
    outfile.close();
    }
      

  4.   

    图片在HTML语言中,要给出的实际上是流。所以你从Oracle中读图片的时候返回的也是一个流对象。
    将该流对象通过servlet,显示到页面就可以了
            String ss="image/"+imageType;
            InputStream in=null;
    response.setContentType(ss);
    ServletOutputStream out = response.getOutputStream();
            Blob myBlob=r.getBlob("bin");
            if(myBlob!=null){
            
             in=myBlob.getBinaryStream();
       byte[] b = new byte[1024];
       int len = 0;
       while((len = in.read(b)) != -1){
      out.write(b,0,len);
      out.flush();
       }
     out.close();
            }else{
            
             FileInputStream fin=new FileInputStream(this.getServletContext().getRealPath("/bdl_html/")+"/no.jpg");
    byte[] b = new byte[1024];
    int len = 0;
    while((len = fin.read(b)) != -1){
       out.write(b,0,len);
       out.flush();
    }
      out.close();
      

  5.   

    用SERVLET设置二进制流,或者自己定义标签库做都是可以的
    看你的方案SERVLET + JSP + JAVABEAN
           或 JAVABEAN+自定义标签+STRUTS