天天看點

如何使用java向mysql存取二進制圖檔

? 濡?浣?浣跨??ava??mysql瀛???浜?杩??跺?劇?? 2007-05-18 18:40

????澶╃???剁???闆???¢?充?绔?涓????劇?????ユ??瀛??ㄥ?ㄦ?版??搴?涓???,??浜?杩??惰??涓???浣跨?ㄨ礬寰?淇?瀛???,?ㄧ?涓???浜??懼???闆ぇ澶?浠?缁????芥??hph?瑰?,?ㄨ?????涓??葷?,棣???瑕?瀛??ㄤ?杩??舵??浠跺?ㄦ?版??搴?涓?瑕???娓?妤?涓??㈠??涓???瀹?

1??? mysql瀛??ㄥぇ瀹歸????浜?杩??舵??浠剁???煎???blob锛??跺???や??劇??杩???浠ュ??????

2??? 瑕????版??搴?瀛??ㄤ?杩??剁????浠朵?瀹?瑕???瑕?瀛??ㄧ???版??杞??㈡??浜?杩??舵?

搴?璇?灏變?澶?璇翠?锛?澶у?剁????浠g??寰?瀹規?????斤????ョ??涓?涓?app绋?搴?锛?褰??堕????ㄨ??ㄦ?版??搴?涓???寤虹??涓?涓??ㄤ?淇?瀛??劇????琛ㄥ???稿?????锛??版???煎?涓?lob

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,?/0 JFIF/0 /0H/0H/0/0 Exif/0/0MM/0*/0/0/0 /0 /0 /0/0/0 /0 /0/0 /0 /0/0/0 /0/0/0b /0 /0/0/0 /0/0/0j (/0 /0/0/0 /0 /0/0 1/0 /0/0/0 /0/0/0r 2/0 /0/0/0 /0/0/0宕?i/0 /0/0/0 /0/0/0/0/0/0/0/0/0H/0/0/0 /0/0/0H/0/0/0 Adobe Photoshop CS Windows/02007:03:18 23:08:15/0/0/0/0/0 ?/0 /0/0/0 /0/0?/0 /0/0/0 /0/0/0?? /0 ........绛?绛?

?跺??灏辨??灏???浠跺??杞??㈡??浜?浜?杩??剁??娴?,?跺?????ュ?頒?sql璇?瑷?涓?,???版??搴????ヤ?寰??垮??跨??涓?娈?ql璇???

如何使用java向mysql存取二進制圖檔

?跺????浠????ュ??涓?涓?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???版??搴????ヤ?杩??跺?劇??

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;

public class UpimgAction extends Action {

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>