posts - 27,  comments - 37,  trackbacks - 0
          首先寫個上傳頁面:upload.html
           1<html>
           2    <head>
           3        <title>Welcome</title>
           4    </head>
           5    <body>
           6    <form action="insert2.jsp" method="post">
           7        圖片名稱<input type="text" name="name" size="80"><br>
           8        文件<input type="file" name="file"><br>
           9        <input type="submit" name="submit" value="提交">
          10    </form>
          11        <center>Welcome</center>
          12    </body>
          13</html>
          14

          然后寫blob數據的輸入程序(insert2.jsp)
           1<%@ page language="java" contentType="text/html;charset=gb2312"%>
           2<%@page import="java.sql.*"%>
           3<%@page import="java.io.*"%>
           4<%@page import="java.util.*"%>
           5<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
           6<html>
           7<head>
           8<title>BLOB</title>
           9</head>
          10<body bgcolor="#FFFFFF">
          11<%
          12String name=request.getParameter("name");
          13String file=request.getParameter("file");
          14Connection conn;
          15String strConn;
          16request.setCharacterEncoding("gb2312");
          17try{
          18out.print(file+"<br>");
          19File files=new File(file); //定義一個file
          20FileInputStream str=new FileInputStream(files);//文件流
          21Class.forName("org.gjt.mm.mysql.Driver").newInstance();//注冊MYSQL驅動
          22conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","root");//數據庫連接
          23String sql="insert into sample(name,image) values(?,?)";
          24PreparedStatement pstmt=conn.prepareStatement(sql)編;//預譯
          25pstmt.setString(1,name);
          26pstmt.setBinaryStream(2,str,(int)files.length());
          27try{
          28    pstmt.executeUpdate();
          29}
          30catch(SQLException e){
          31    out.print(e.getMessage());
          32}
          33pstmt.close();
          34conn.close();
          35
          36out.print("111111");
          37}
          38    catch(SQLException e){
          39    out.print(e.getMessage());
          40}
          41catch(IOException e){
          42    out.print(e.getMessage());
          43}
          44
          %>
          45<href="readblob2.jsp?name=<%=name%>">查看圖片</a>
          46
          47Write your content here
          48asdfasdfasdfsadf
          49</body>
          50</html>
          51

          現在在寫個readblob把寫如數據庫中的blob字段輸出到瀏覽器中:(readblob2.jsp)

          <%@ page language="java" %>
          <%@page import="java.sql.*"%>
          <%@page import="java.io.*"%>
          <%@page import="java.util.*"%>
          <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
          <html>
          <head>
          <title>Lomboz JSP</title>
          </head>
          <body bgcolor="#FFFFFF">
          <%
          String name=request.getParameter("name");
          out.print(name
          +"<br>");
          Connection conn;
          String strConn;
          ResultSet rs;
          Statement stmt;
          try{
          Class.forName(
          "org.gjt.mm.mysql.Driver").newInstance();
          conn
          =DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","root");
          stmt
          =conn.createStatement();
          String sql="select * from sample where name='"+name+"'";
          rs
          =stmt.executeQuery(sql);


          ServletOutputStream op 
          = response.getOutputStream();
          if(rs.next())
          {
              Blob b 
          = rs.getBlob("image");
              
          long size = b.length();
              
          //out.print(size);
              
          byte[] bs = b.getBytes(1,(int)size);
              response.setContentType(
          "image/jpeg");
              response.reset();
              op.write(bs);
              op.flush();
              op.close();
              
          }
          rs.close();
          conn.close();
          stmt.close();
          }catch(SQLException e){
              out.print(e.getMessage());
          }
          catch(IOException e){
              out.print(e.getMessage());
          }
          %>
          </body>
          </html>



           小時候家的對面有一座山,山的上面就是藍天,所以總是幻想著有一天站到山頂用手摸一下藍天……
          posted on 2008-10-20 13:16 丫丫 閱讀(920) 評論(0)  編輯  收藏 所屬分類: jspmysql
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(1)

          隨筆分類(25)

          隨筆檔案(27)

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 怀柔区| 岱山县| 石台县| 理塘县| 阿城市| 抚州市| 德令哈市| 长葛市| 阿鲁科尔沁旗| 莆田市| 黄陵县| 明光市| 衡阳市| 永昌县| 三明市| 运城市| 凌海市| 隆化县| 五家渠市| 长治市| 满城县| 中宁县| 永德县| 浦江县| 旬阳县| 颍上县| 师宗县| 潢川县| 涿州市| 资兴市| 凉山| 霍邱县| 呈贡县| 苗栗县| 信阳市| 德阳市| 六枝特区| 民乐县| 乌海市| 临朐县| 松溪县|