程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> java完成拔出mysql二進制文件,blob類型,碰到成績及處理方法

java完成拔出mysql二進制文件,blob類型,碰到成績及處理方法

編輯:MySQL綜合教程

java完成拔出mysql二進制文件,blob類型,碰到成績及處理方法。本站提示廣大學習愛好者:(java完成拔出mysql二進制文件,blob類型,碰到成績及處理方法)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成拔出mysql二進制文件,blob類型,碰到成績及處理方法正文


起首是數據庫樹立要預備的:
我們要把放置二進制字段設置為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