mysql插入二进制文件,blob类型,遇到问题及解决办法

  • A+
所属分类:数据库

mysql插入二进制文件blob类型,遇到问题及解决办法

首先是数据库建立要准备的:
我们要把放置二进制字段设置为Blob类型,根据文件的大小选择合适的Blob类型,一下是各个Blob类型所能容纳二进制文件的大小
MySQL的四种BLOB类型
类型 大小(单位:字节)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
一下是具体操作代码:

  1. /** 
  2. * 把二进制文件(该二进制文件可以是本地硬盘路径,也可以是一个网络路径)存入数据库 
  3. * create date:2009-5-13 author:Administrator 
  4. * @param file 
  5. * 可以是本地文件也可以是网络文件 
  6. * @param conn 
  7. */
  8. public void saveBinary(String file, Connection conn) {
  9. // 注意二进制文件写入数据库时所用到的类,以及类包装转换过程 
  10. File f = null;
  11. if (file.toLowerCase().contains("http:"))
  12. f = DownLoadWithUrl.downLoadFile(file);
  13. else
  14. f = new File(file);
  15. if (f != null) {
  16. try {
  17. InputStream is = new FileInputStream(f);
  18. PreparedStatement ps = conn
  19. .prepareStatement("insert into bankVoice(name,text) values (?,?)");
  20. ps.setString(1, file);
  21. int i = is.available();
  22. ps.setBinaryStream(2, is, is.available());
  23. ps.executeUpdate();
  24. System.out.println("二进制文件插入成功");
  25. ps.clearParameters();
  26. ps.close();
  27. is.close();
  28. catch (Exception e) {
  29. e.printStackTrace();
  30. System.out.println("二进制文件插入时出现异常");
  31. }
  32. }
  33. }

注意在操作时候会出现以下异常,那么我们只需做一下设置:以我本地为例:进入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服务器,这样就不会出现下面的异常了。

  1. 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.
  2. at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2632)
  3. at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2618)
  4. at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1551)
  5. at com.mysql.jdbc.ServerPreparedStatement.storeStream(ServerPreparedStatement.java:2180)
  6. at com.mysql.jdbc.ServerPreparedStatement.serverLongData(ServerPreparedStatement.java:1199)
  7. at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1004)
  8. at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
  9. at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
  10. at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
  11. at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
  12. at SaveBinaryToDB.SaveBinaryToDB.saveBinary(SaveBinaryToDB.java:33)
  13. at SaveBinaryToDB.SaveBinaryToDB.main(SaveBinaryToDB.java:17)
  14. /** 
  15. * 从数据库中读取二进制文件 create date:2009-5-13 author:Administrator 
  16. * @param file 
  17. * @param conn 
  18. */
  19. public void getBinary(String file, Connection conn) {
  20. // 注意二进制文件从数据库中读取时所用到的类,以及类的包装转换过程 
  21. try {
  22. PreparedStatement ps = conn
  23. .prepareStatement("select text from bankVoice where name=?");
  24. ps.setString(1, file);
  25. Blob blob = null;
  26. ResultSet rs = ps.executeQuery();
  27. if (rs.next()) {
  28. blob = (Blob) rs.getBlob("text");
  29. }
  30. FileOutputStream fos = new FileOutputStream("D:\\test1.mp3");
  31. fos.write(blob.getBytes(1, (int) blob.length()));
  32. System.out.println("二进制文件获得成功");
  33. ps.clearParameters();
  34. ps.close();
  35. fos.close();
  36. catch (Exception e) {
  37. e.printStackTrace();
  38. System.out.println("二进制文件读取时出现异常");
  39. }
  40. }

package SaveBinaryToDB;

  1. /** 
  2. * 本程序的功能实现网络下载 
  3. * 把指定url的文件下载到本地硬盘 
  4. */
  5. import java.io.*;
  6. import java.net.*;
  7. /** 
  8. * @todo 将网上获取的图像,mp3等文件存储到本地 
  9. * @version 1.0 
  10. */
  11. public class DownLoadWithUrl {
  12. public static File downLoadFile(String fromUrl) {
  13. URL url;
  14. File file = null;
  15. try {
  16. // url = new 
  17. // 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"); 
  18. url = new URL(fromUrl);
  19. URLConnection uc = url.openConnection();
  20. InputStream is = uc.getInputStream();
  21. // 根据下载文件类型的不同,进行相应的文件命名 
  22. file = new File("D:\\forever.mp3");
  23. FileOutputStream out = new FileOutputStream(file);
  24. /* 
  25. * 该注释内的也是一种写入文件的方法,不过通常下载mp3或者比mp3更小图片 
  26. * 等这些文件用这种带缓冲的方法写文件比较慢,所以说小文件下载通常用下面 的写文件方法就可以了 // byte[] b = new 
  27. * byte[102400*3]; // int size = 0; // // while ((size = is.read(b)) != 
  28. * -1) { // out.write(b, 1, size); // // } 
  29. */
  30. int i = 0;
  31. while ((i = is.read()) != -1) {
  32. out.write(i);
  33. }
  34. out.flush();
  35. is.close();
  36. catch (Exception e) {
  37. // TODO Auto-generated catch block 
  38. e.printStackTrace();
  39. }
  40. return file;
  41. }
  42. /** 
  43. * 删除本地磁盘指定路径的文件 create date:2009-5-13 author:Administrator 
  44. * @param file 
  45. */
  46. public static void delFile(String file) {
  47. File f = new File(file);
  48. if (f.exists())
  49. f.delete();
  50. System.out.println(file + "已经被删除");
  51. }
  52. public static void main(String[] args) {
  53. // delFile("D:\\forever.mp3"); 
  54. downLoadFile("");
  55. }
  56. }
图片引用自网络