还有,前提是图片要包含在JSP页面内显示

解决方案 »

  1.   

    我是把图片存到数据库中,我用的是servlet
    把代码帖出来,看看对你有没有帮助;
    javabean:
    mport java.io.*;public class dbo {
      public dbo() {
      }  public InputStream getBytes() {    Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        InputStream stream = null;
        File f = null;
        byte context[] = new byte[100000];
        try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          con = DriverManager.getConnection("jdbc:odbc:test");
          f = new File("C://aa.jpg");
          stream = new FileInputStream(f);
          //pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");
          //pst.setBinaryStream(1,stream,(int)f.length());
          pst = con.prepareStatement("SELECT COL10 FROM MyTable");
          rs = pst.executeQuery();
          if (rs.next()) {
            stream = rs.getBinaryStream(1);
          }
          return stream;
        }
        catch (Exception e) {
          System.out.println(e);
          return stream;
        }
        finally {
          try {
            /*
            if (rs != null) {
              rs.close();
            }
            if (pst != null) {
              pst.close();
            }
            if (con != null) {
              pst.close();
            }
    */
          }
          catch (Exception e) {      }
        }  }  public static void main(String args[]) {  }
    }/*
     写入数据库
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        InputStream stream = null;
        File f=null;
        try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          con = DriverManager.getConnection("jdbc:odbc:test");
          f = new File("C://aa.jpg");
          stream = new FileInputStream(f);
          pst = con.prepareStatement("INSERT INTO MyTable (COL10) VALUES(?)");
          pst.setBinaryStream(1,stream,(int)f.length());
          pst.execute();
        }
        catch (Exception e) {
          System.out.println(e);
        }
        finally {
          try {
            if (rs != null) {
              rs.close();
            }
            if (pst != null) {
              pst.close();
            }
            if (con != null) {
              pst.close();
            }
          }
          catch (Exception e) {      }
        }
     */
      

  2.   

    jsp and servlet
    jsp:
    <%@ page contentType="text/html; charset=GBK" %>
    <html>
    <head>
    <title>
    jsp1
    </title>
    </head>
    <body bgcolor="#ffffff">
      dfdfd
    <img src="servlet1?id=1"/>
    dfdef
    </body>
    </html>servlet:package index;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    //import java.util.*;
    import index.dbo;public class Servlet1
        extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";  //Initialize global variables
      public void init() throws ServletException {
      }  //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
        //response.setContentType("image/gif;");
        dbo d = new dbo();
        java.io.InputStream pic = null;
        java.io.OutputStream pw = null;
        try{
          //request.getParameter("id");
          response.setContentType("image/gif;name=doskt.jpg");
          pic=d.getBytes();
          pw=response.getOutputStream();
          byte[] b = new byte[4096];
          int bt = pic.read(b);
          while (bt != -1) {
            pw.write(b, 0, bt);
            bt = pic.read(b);
          }
        }
        catch(Exception e){
          System.out.println("doGet:" + e);
        }
        finally{
          if(pic!=null){
            pic.close();
          }
        }    /*out.println("<html>");
             out.println("<head><title>Servlet1</title></head>");
             out.println("<body bgcolor=\"#ffffff\">");
         out.println("<p>The servlet has received a GET. This is the reply.</p>");
             out.println("</body></html>");*/
      }  //Clean up resources
      public void destroy() {
      }
    }