各位大虾,小弟现在有个问题:
我现在想做一个JSP页面(负责将文件选定并提交,就是一般的文件提交页面),接受提交的JSP页面调用JAVABEAN(将图片存进ORACLE BLOB字段)。然后就是在JSP页面里读取存在BLOB字段里的图片。要是有源码那就非常感谢了。

解决方案 »

  1.   

    下面方法写入图片的二进制流到Oralce数据库:public boolean createProduct(Product entity) {
      boolean flag = false;
      if (entity == null)
        return false;
    try {
    StringBuffer sql = new StringBuffer(
    "insert into PRODUCT_TAB(PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE,PARENTID,REMARK,IMAGE)");
    sql.append(" values(?,?,?,?,?,empty_blob())");
    PreparedStatement pst = con.prepareStatement(sql.toString());
    String id = generatorID();
    pst.setString(1, id);
    pst.setString(2, entity.getProductName() == null ? id : entity
    .getProductName());
    pst.setString(3, entity.getProductType());
    pst.setString(4, entity.getParentId());
    pst.setString(5, entity.getRe());
    pst.executeUpdate();
    pst.close();
    if (entity.getImageFile() != null) {
    /***************************************************************
     * 插入blob二进制数据
     */
    BLOB imgBlob = null;
    pst = con
    .prepareStatement("select image from PRODUCT_TAB where PRODUCT_ID=? for update");
    pst.setString(1, id);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
    imgBlob = (BLOB) rs.getBlob(1);
    }
    InputStream isr = entity.getImageFile();
    pst = con
    .prepareStatement("update PRODUCT_TAB set image=? where PRODUCT_ID=?"); OutputStream out = imgBlob.getBinaryOutputStream();
    int count = -1, total = 0;
    byte[] data = new byte[imgBlob.getBufferSize()]; // 另一种实现方法,节省内存
    while ((count = isr.read(data)) != -1) {
    total += count;
    out.write(data, 0, count);
    }
    isr.close();
    out.close();
    pst.setBlob(1, imgBlob);
    pst.setString(2, id);
    pst.executeUpdate();
    pst.close();
    }
    con.commit();
    con.close();
    flag = true;
    entity = null;
    } catch (FileNotFoundException e) {
    log.info("ProductDAO.createProduct()  FileNotFoundException :"
    + e.getMessage());
    } catch (IOException e) {
    log.info("ProductDAO.createProduct()  IOException :"
    + e.getMessage());
    } catch (SQLException e) {
    log.info("ProductDAO.createProduct() is error :" + e.getMessage());
    }
    return flag;
    }
    下面方法从数据库读出二进制流
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.hf.db.dao.ProductDAO;
    import com.hf.db.entity.Product;public class ShowImage extends HttpServlet {
    private static final long serialVersionUID = 1L; /**
     * Constructor of the object.
     */
    public ShowImage() {
    super();
    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
    public void init() throws ServletException {
    // Put your code here
    }
    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doPost(request,response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("image/jpeg;charset=gb2312"); //设定类型
    String productId=(String)request.getParameter("productId");
    try{
    ProductDAO dao=new ProductDAO();
    Product entity=dao.getProduct(productId);
    long size = entity.getImage().length();
    byte[] bs = entity.getImage().getBytes(1,(int)size);
    OutputStream outs = response.getOutputStream();
    outs.write(bs);
    outs.flush();
    outs.close();
    }catch(Exception e){
    PrintWriter pw=response.getWriter();
    response.setContentType("text/html;charset=gb2312");
    pw.write("无法打开图片");
    pw.close();
    }
    }}jsp中显示图片<td height="130">
    <div align="center">
    <img src="<%=context%>/ShowImage?productId=<%=products[0].getProductId()%>" alt="点击查看大图片" width="90" height="100" border="0" align="middle">/div>                
    </td>