<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    談笑有鴻儒,往來無白丁

    在恰當的時間、地點以恰當的方式表達給恰當的人...  閱讀的時候請注意分類,佛曰我日里面是談笑文章,其他是各個分類的文章,積極的熱情投入到寫博的隊伍中來,支持blogjava做大做強!向dudu站長致敬>> > 我的微博敬請收聽
    前幾天突然看到學校音樂站上的圖片原來是存儲在數據庫上的,是二進制而不是使用路徑保存的,在網上招了找發現大多介紹的都是hph方式,在這里做個總結,首先要存儲二進制文件在數據庫中要搞清楚下面幾個內容:
    1 mysql存儲大容量的二進制文件的格式是blob,其實除了圖片還可以存別的
    2 要向數據庫存儲二進制的文件一定要把要存儲的數據轉換成二進制流
    廢話就不多說了,大家看看代碼很容易明白,先來看一個app程序,當然首先您要在數據庫中先建立一個用于保存圖片的表和相應的列,數據格式為blob
    package com.lizhe;
    import java.io.*;
    import java.sql.*;
    public class PutImg {
    public void putimg() {
    try {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
    Connection conn = DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    //stmt.execute("insert into imgt (id) values (5)");
    stmt.close();
    PreparedStatement pstmt = null;
    String sql = "";
    File file = new File("c:\blog.jpg");
    InputStream photoStream = new FileInputStream(file);
    //sql = " UPDATE imgt SET img = ? ";

    sql = "INSERT INTO imgtable (img) VALUES (?)";

    pstmt = conn.prepareStatement(sql);
    pstmt.setBinaryStream(1, photoStream, (int) file.length());
    pstmt.executeUpdate();
    pstmt.close();
    conn.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String args[]){
    PutImg pi=new PutImg();
    pi.putimg();
    }
    }

    InputStream photoStream = new FileInputStream(file);
    可以很清楚的看到我們首先把一個圖片文件(當然也可以是別的什么文件)轉換成了一個二進制輸入流
    pstmt.setBinaryStream(1, photoStream, (int) file.length());
    這個方法建議大家去查一下API文檔,第一個參數是通配符位置沒的說,第二個參數是流,這和以往的string類型的參數不太一樣,我剛看到的時候也覺得豁然開朗了,但是到這里還沒完,不同于以往的字符串參數,這里我們還需要第三個參數來設置這個流的長度,這里也就是這個文件的長度,導出數據庫中的sql,一切都清楚了
    INSERT INTO `m_diy` VALUES (2,? JFIF HH?? ExifMM* b j ( 1 r 2 ?i H H Adobe Photoshop CS Windows2007:03:18 23:08:15 ? ??? ? ........等等
    其實就是將文件先轉換成了二進制的流,然后插入到了sql語言中,向數據庫寫入了很長很長的一段sql語句



    然后我們再來寫一個app程序將這個文件讀出來,存儲成一個圖片文件
    package com.lizhe;
    import java.io.*;
    import java.sql.*;
    class GetImg {

    private static final String URL = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk";
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    private File file = null;

    public void blobRead(String outfile, int picID) throws Exception {
    FileOutputStream fos = null;
    InputStream is = null;
    byte[] Buffer = new byte[4096];
    try {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    conn = DriverManager.getConnection(URL);
    pstmt = conn.prepareStatement("select img from imgt where id=?");
    pstmt.setInt(1, picID); // 傳入要取的圖片的ID
    rs = pstmt.executeQuery();
    rs.next();
    file = new File(outfile);
    if (!file.exists()) {
    file.createNewFile(); // 如果文件不存在,則創建
    }
    fos = new FileOutputStream(file);
    is = rs.getBinaryStream("img");
    int size = 0;

    while ((size = is.read(Buffer)) != -1) {
    // System.out.println(size);
    fos.write(Buffer, 0, size);
    }
    } catch (Exception e) {
    System.out.println( e.getMessage());
    } finally {
    // 關閉用到的資源
    fos.close();
    rs.close();
    pstmt.close();
    conn.close();
    }
    }
    public static void main(String[] args) {
    try {
    GetImg gi=new GetImg();
    gi.blobRead("c:/getimgs/1.jpg", 5);
    } catch (Exception e) {
    System.out.println("[Main func error: ]" + e.getMessage());
    }
    }
    }
    這里需要注意的是
    is = rs.getBinaryStream("img");
    img是數據庫中相應的列名,其實和rs.getString()方法差不多,只不過這個方法是讀取二進制流的
    最后在帖兩個bs系統上用的文件給大家參考
    通過struts的action向數據庫寫入二進制圖片
    /*
    * Generated by MyEclipse Struts
    * Template path: templates/java/JavaClass.vtl
    */
    package com.lizhe.struts.action;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;
    import com.lizhe.struts.form.UpimgForm;
    /**
    * MyEclipse Struts
    * Creation date: 05-18-2007
    *
    * XDoclet definition:
    * @struts.action path="/upimg" name="upimgForm" input="/userhomepage.jsp"
    * @struts.action-forward name="userhome" path="/userhomepage.jsp" redirect="true" contextRelative="true"
    */
    public class UpimgAction extends Action {
    /*
    * Generated Methods
    */
    /**
    * Method execute
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return ActionForward
    * @throws IOException
    * @throws FileNotFoundException
    */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
    UpimgForm upimgForm = (UpimgForm) form;// TODO Auto-generated method stub

    FormFile file=upimgForm.getFile();
    InputStream is=file.getInputStream();

    try {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    String url = "jdbc:mysql://localhost/blog?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
    Connection conn = DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    //stmt.execute("insert into img (id) values (5)");
    stmt.close();
    PreparedStatement pstmt = null;
    String sql = "";
    //File file = new File("c:\blog.jpg");
    //InputStream photoStream = new FileInputStream(file);
    //sql = " UPDATE imgt SET img = ? ";

    sql = "INSERT INTO img (img) VALUES (?)";

    pstmt = conn.prepareStatement(sql);
    pstmt.setBinaryStream(1, is, (int) file.getFileSize());
    pstmt.executeUpdate();
    pstmt.close();
    conn.close();
    } catch (Exception e) {
    e.printStackTrace();
    }

    return mapping.findForward("userhomepage");
    }
    }
    和app的方式幾乎是一樣的
    第二個文件是通過jsp將數據庫中的圖片顯示在頁面上
    這個有些不同
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.util.*"%>
    <%@ page import="java.text.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.awt.*"%>
    <html>
    <body>
    <%
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    String url="jdbc:mysql://localhost/img?user=root&password=root";
    Connection con = DriverManager.getConnection(url);
    String sql = "select * from imgt where id=5";
    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery(sql);
    if(rs.next()) {
    InputStream in = rs.getBinaryStream("img");
    ServletOutputStream op = response.getOutputStream();
    int len;
    byte[] buf=new byte[1024];
    while((len= in.read(buf))!=-1) {
    op.write(buf, 0, len);
    }
    op.close();
    in.close();
    }

    rs.close();
    stmt.close();
    con.close();
    %>
    </body>
    </html>
    posted on 2007-06-01 08:38 壞男孩 閱讀(4377) 評論(2)  編輯  收藏 所屬分類: java命令學習

    FeedBack:
    # re: 如何通過java或jsp向數據庫存取二進制圖片
    2007-12-07 22:35 | 江濤
    謝謝,很及時。  回復  更多評論
      
    # re: 如何通過java或jsp向數據庫存取二進制圖片
    2014-12-05 20:26 | 額鵝鵝鵝
    額哇哇哇  回復  更多評論
      
    主站蜘蛛池模板: 欧美好看的免费电影在线观看| 国产成人精品日本亚洲18图| 岛国大片免费在线观看| 三年片在线观看免费西瓜视频| 亚洲日韩在线中文字幕综合| 亚洲精品中文字幕无码AV| 国产成人A亚洲精V品无码| 国产精品冒白浆免费视频| 午夜福利不卡片在线播放免费| 国产一精品一AV一免费| 欧洲精品码一区二区三区免费看| 中文字幕无码精品亚洲资源网久久| 亚洲伊人tv综合网色| 亚洲精品字幕在线观看| 亚洲av午夜精品一区二区三区| 好爽…又高潮了毛片免费看 | 亚洲欧洲精品成人久久曰影片 | 亚洲成av人片不卡无码久久 | 亚洲成a人无码亚洲成av无码| 亚洲四虎永久在线播放| 亚洲色精品88色婷婷七月丁香 | 亚洲色偷偷综合亚洲AV伊人蜜桃| 亚洲最大的成网4438| 久久精品国产亚洲一区二区| 亚洲国产成人爱av在线播放| 国产成人精品免费直播| 日本免费的一级v一片| 岛国大片免费在线观看| 久久久久国色AV免费看图片| ww4545四虎永久免费地址| 精品无码国产污污污免费网站| 国产高清不卡免费视频| 曰批全过程免费视频在线观看无码| 久久国产乱子伦精品免费午夜| 一级毛片免费不卡直观看| 免费国产va在线观看| 免费很黄无遮挡的视频毛片| 免费无毒a网站在线观看| 激情吃奶吻胸免费视频xxxx| 国产午夜亚洲精品不卡| 特级毛片aaaa级毛片免费|