程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> java實現插入mysql二進制文件,blob類型,遇到問題及解決辦法

java實現插入mysql二進制文件,blob類型,遇到問題及解決辦法

編輯:關於MYSQL數據庫
首先是數據庫建立要准備的:
我們要把放置二進制字段設置為Blob類型,根據文件的大小選擇合適的Blob類型,一下是各個Blob類型所能容納二進制文件的大小
MySQL的四種BLOB類型
類型 大小(單位:字節)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
一下是具體操作代碼:
復制代碼 代碼如下:
/**
*
* 把二進制文件(該二進制文件可以是本地硬盤路徑,也可以是一個網絡路徑)存入數據庫
* create date:2009-5-13 author:Administrator
*
* @param file
* 可以是本地文件也可以是網絡文件
* @param conn
*/
public void saveBinary(String file, Connection conn) {
// 注意二進制文件寫入數據庫時所用到的類,以及類包裝轉換過程
File f = null;
if (file.toLowerCase().contains("http:"))
f = DownLoadWithUrl.downLoadFile(file);
else
f = new File(file);
if (f != null) {
try {
InputStream is = new FileInputStream(f);
PreparedStatement ps = conn
.prepareStatement("insert into bankVoice(name,text) values (?,?)");
ps.setString(1, file);
int i = is.available();
ps.setBinaryStream(2, is, is.available());
ps.executeUpdate();
System.out.println("二進制文件插入成功");
ps.clearParameters();
ps.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("二進制文件插入時出現異常");
}
}
}

注意在操作時候會出現以下異常,那麼我們只需做一下設置:以我本地為例:進入D:\MySql5.0\mysql-5.0.51b-win32 目錄,有以下文件可以看到:my-large.ini、my-small.ini、my-medium.ini、my-huge.ini
我們把只需把mysql服務現在加載的ini文件中的配置項:max_allowed_packet 改為 16M
即是:max_allowed_packet = 16M 默認的是1M我們改為16M,然後重啟mysql服務器,這樣就不會出現下面的異常了。
復制代碼 代碼如下:
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1048587 > 1047552). You can change this value on the server by setting the max_allowed_packet' variable.
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2632)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2618)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1551)
at com.mysql.jdbc.ServerPreparedStatement.storeStream(ServerPreparedStatement.java:2180)
at com.mysql.jdbc.ServerPreparedStatement.serverLongData(ServerPreparedStatement.java:1199)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1004)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at SaveBinaryToDB.SaveBinaryToDB.saveBinary(SaveBinaryToDB.java:33)
at SaveBinaryToDB.SaveBinaryToDB.main(SaveBinaryToDB.java:17)
/**
* 從數據庫中讀取二進制文件 create date:2009-5-13 author:Administrator
*
* @param file
* @param conn
*/
public void getBinary(String file, Connection conn) {
// 注意二進制文件從數據庫中讀取時所用到的類,以及類的包裝轉換過程
try {
PreparedStatement ps = conn
.prepareStatement("select text from bankVoice where name=?");
ps.setString(1, file);
Blob blob = null;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
blob = (Blob) rs.getBlob("text");
}
FileOutputStream fos = new FileOutputStream("D:\\test1.mp3");
fos.write(blob.getBytes(1, (int) blob.length()));
System.out.println("二進制文件獲得成功");
ps.clearParameters();
ps.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("二進制文件讀取時出現異常");
}
}

package SaveBinaryToDB;
復制代碼 代碼如下:
/**
* 本程序的功能實現網絡下載
* 把指定url的文件下載到本地硬盤
*
*/
import java.io.*;
import java.net.*;
/**
* @todo 將網上獲取的圖像,mp3等文件存儲到本地
*
* @version 1.0
*/
public class DownLoadWithUrl {
public static File downLoadFile(String fromUrl) {
URL url;
File file = null;
try {
// url = new
// URL("http://count.koubei.com/showphone/showphone.php?f=jpg&w=96&h=10&bc=255,255,255&fc=0,0,0&fs=10&fn=arial&phone=NzMwNzIyNTE1%236aWCXtTNZYkxASrj");
url = new URL(fromUrl);
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
// 根據下載文件類型的不同,進行相應的文件命名
file = new File("D:\\forever.mp3");
FileOutputStream out = new FileOutputStream(file);
/*
* 該注釋內的也是一種寫入文件的方法,不過通常下載mp3或者比mp3更小圖片
* 等這些文件用這種帶緩沖的方法寫文件比較慢,所以說小文件下載通常用下面 的寫文件方法就可以了 // byte[] b = new
* byte[102400*3]; // int size = 0; // // while ((size = is.read(b)) !=
* -1) { // out.write(b, 1, size); // // }
*/
int i = 0;
while ((i = is.read()) != -1) {
out.write(i);
}
out.flush();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
/**
* 刪除本地磁盤指定路徑的文件 create date:2009-5-13 author:Administrator
*
* @param file
*/
public static void delFile(String file) {
File f = new File(file);
if (f.exists())
f.delete();
System.out.println(file + "已經被刪除");
}
public static void main(String[] args) {
// delFile("D:\\forever.mp3");
downLoadFile("");
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved