﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-深蓝的天空下,有你有我...-随笔分类-JAVA</title><link>http://www.blogjava.net/liuyxit/category/48876.html</link><description>共享酸、甜、苦、辣</description><language>zh-cn</language><lastBuildDate>Wed, 24 Aug 2011 09:08:32 GMT</lastBuildDate><pubDate>Wed, 24 Aug 2011 09:08:32 GMT</pubDate><ttl>60</ttl><item><title>Eclipse常用插件安装地址</title><link>http://www.blogjava.net/liuyxit/archive/2011/08/16/356621.html</link><dc:creator>三刀流の逆风</dc:creator><author>三刀流の逆风</author><pubDate>Tue, 16 Aug 2011 04:16:00 GMT</pubDate><guid>http://www.blogjava.net/liuyxit/archive/2011/08/16/356621.html</guid><wfw:comment>http://www.blogjava.net/liuyxit/comments/356621.html</wfw:comment><comments>http://www.blogjava.net/liuyxit/archive/2011/08/16/356621.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liuyxit/comments/commentRss/356621.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liuyxit/services/trackbacks/356621.html</trackback:ping><description><![CDATA[Eclipse常用插件安装地址
http://subclipse.tigris.org/update_1.6.x/
http://propedit.sourceforge.jp/eclipse/updates/
http://flexformatter.googlecode.com/svn/trunk/FlexFormatter/FlexPrettyPrintCommandUpdateSite
<img src ="http://www.blogjava.net/liuyxit/aggbug/356621.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liuyxit/" target="_blank">三刀流の逆风</a> 2011-08-16 12:16 <a href="http://www.blogjava.net/liuyxit/archive/2011/08/16/356621.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql,sqlserver,oracle三种数据库的大对象存取</title><link>http://www.blogjava.net/liuyxit/archive/2011/06/19/352605.html</link><dc:creator>三刀流の逆风</dc:creator><author>三刀流の逆风</author><pubDate>Sat, 18 Jun 2011 18:02:00 GMT</pubDate><guid>http://www.blogjava.net/liuyxit/archive/2011/06/19/352605.html</guid><wfw:comment>http://www.blogjava.net/liuyxit/comments/352605.html</wfw:comment><comments>http://www.blogjava.net/liuyxit/archive/2011/06/19/352605.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liuyxit/comments/commentRss/352605.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liuyxit/services/trackbacks/352605.html</trackback:ping><description><![CDATA[mysql 大对象存取:
类型一般应该用mediumblod,
blob只能存2的16次方个byte,
mediumblod是24次方,
一般来说够用了.longblob是32次方有些大.

MYSQL默认配置只能存1M大小的文件,要修改配置,WIN版本的在mysql.ini文件中
修改max_allowed_packet,net_buffer_length等几个参数,或直接SET GLOBAL varName=value.
linux版本可以在启动参数后加-max_allowed_packet=xxM等几个参数.

MYSQL存大对象最好直接就setBinaryStream,又快又方便.
而不要先插入空再造型成BLOB然后再setBlob

例子:
import java.sql.*;
import java.io.*;
public class DBTest {

  
   static String driver = "org.gjt.mm.mysql.Driver";
   static String url = "jdbc:mysql://localhost:3306/test";
   static String user = "root";
   static String passwd = "passwd";
   public static void main(String[] args) throws Exception {
     Connection conn = null;
     try {
       Class.forName(driver);
       conn = DriverManager.getConnection(url,user,passwd);
      
       int op = 1;
       //插入
       if (op == 0) {
         PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
         ps.setString(1, "aaa.exe");
         InputStream in = new FileInputStream("d:/aaa.exe");
         ps.setBinaryStream(2,in,in.available());
         ps.executeUpdate();
         ps.close();
       }
       else {
         //取出
         PreparedStatement ps = conn.prepareStatement("select * from   tb_file where filename = ?");
         ps.setString(1, "aaa.exe");
         ResultSet rs = ps.executeQuery();
         rs.next();
         InputStream in = rs.getBinaryStream("filecontent");
         System.out.println(in.available());
         FileOutputStream out = new FileOutputStream("d:/bbb.exe");
         byte[] b = new byte[1024];
         int len = 0;
         while ( (len = in.read(b)) != -1) {
           out.write(b, 0, len);
           out.flush();
         }
         out.close();
         in.close();
         rs.close();
         ps.close();
       }
     }
     catch (Exception ex) {
       ex.printStackTrace(System.out);
     }
     finally {
       try {conn.close();}
       catch (Exception ex) { }
     }
   }
}


sqlserver 大对象存取没有什么多说的,只要是image类型就行了,注意这是column类型,有人以为它只能存
图象.image是文件镜象的意思.
import java.sql.*;
import java.io.*;
public class DBTest {


   static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
   static String url = "jdbc:microsoft:sqlserver://192.168.0.202:9999999999;DatabaseName=dddd";
   static String user = "sa";
   static String passwd = "ps";
   public static void main(String[] args) throws Exception {
     Connection conn = null;
     try {
       Class.forName(driver);
       conn = DriverManager.getConnection(url,user,passwd);
       int op = 0;
       //插入
       if (op == 0) {
         PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
         ps.setString(1, "aaa.exe");
         InputStream in = new FileInputStream("d:/aaa.exe");
         ps.setBinaryStream(2,in,in.available());
         ps.executeUpdate();
         ps.close();
       }
       else {
         //取出
         PreparedStatement ps = conn.prepareStatement("select * from   tb_file where filename = ?");
         ps.setString(1, "aaa.exe");
         ResultSet rs = ps.executeQuery();
         rs.next();
         InputStream in = rs.getBinaryStream("filecontent");
         System.out.println(in.available());
         FileOutputStream out = new FileOutputStream("d:/bbb.exe");
         byte[] b = new byte[1024];
         int len = 0;
         while ( (len = in.read(b)) != -1) {
           out.write(b, 0, len);
           out.flush();
         }
         out.close();
         in.close();
         rs.close();
         ps.close();
       }
     }
     catch (Exception ex) {
       ex.printStackTrace(System.out);
     }
     finally {
       try {conn.close();}
       catch (Exception ex) { }
     }
   }
}



ORACLE的大对象存储有些变态,要无论是Blob,还是CLOB都要求先插入一个空值,然后
查询并锁定这一条记录,获取对Lob的引用再进行填充,网上有太多的例子.我个人认为
这种方法垃圾得连写都不想写了,你可以自己去搜索一下.
这种特别的操作既增加操作的复杂度,又违反了JDBC接口的规范,所以我极力反对这样
使用,如果你和我有同样的观点.那么我提供另一种通用的方法.就是你不用LOB而用
oracle的LONG RAW来代替它们.这样就可以象其它对象一样操作了:

create table tb_file(filename varchar2(255),filecontent LONG RAW);


import java.sql.*;
import java.io.*;

public class BlobTest {

   static String driver = "oracle.jdbc.driver.OracleDriver";
   static String url = "jdbc:oracle:thin:@localhost:1521:test";
   static String user = "system";
   static String passwd = "passwd";
   public static void main(String[] args) throws Exception {
     Connection conn = null;
     try {
       Class.forName(driver);
       conn = DriverManager.getConnection(url, user, passwd);
       int op = 1;
       //插入
       if (op == 0) {
         PreparedStatement ps = conn.prepareStatement("insert into tb_file values (?,?)");
         ps.setString(1, "aaa.exe");
         InputStream in = new FileInputStream("d:/aaa.exe");
         ps.setBinaryStream(2,in,in.available());
         ps.executeUpdate();
         ps.close();
       }
       else {
         //取出
         PreparedStatement ps = conn.prepareStatement("select * from   tb_file where filename = ?");
         ps.setString(1, "aaa.exe");
         ResultSet rs = ps.executeQuery();
         rs.next();
         InputStream in = rs.getBinaryStream("filecontent");
         System.out.println(in.available());
         FileOutputStream out = new FileOutputStream("d:/bbb.exe");
         byte[] b = new byte[1024];
         int len = 0;
         while ( (len = in.read(b)) != -1) {
           out.write(b, 0, len);
           out.flush();
         }
         out.close();
         in.close();
         rs.close();
         ps.close();
       }
     }
     catch (Exception ex) {
       ex.printStackTrace(System.out);
     }
     finally {
       try {
         conn.close();
       }
       catch (Exception ex) {}
     }
   }
}
转自：http://dev.csdn.net/author/axman/1ca2ede425e44dba9ac20c2e262e4fb8.html
 <img src ="http://www.blogjava.net/liuyxit/aggbug/352605.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liuyxit/" target="_blank">三刀流の逆风</a> 2011-06-19 02:02 <a href="http://www.blogjava.net/liuyxit/archive/2011/06/19/352605.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]inputStream与byte[]互转。</title><link>http://www.blogjava.net/liuyxit/archive/2011/06/19/inputStream_byte.html</link><dc:creator>三刀流の逆风</dc:creator><author>三刀流の逆风</author><pubDate>Sat, 18 Jun 2011 18:01:00 GMT</pubDate><guid>http://www.blogjava.net/liuyxit/archive/2011/06/19/inputStream_byte.html</guid><wfw:comment>http://www.blogjava.net/liuyxit/comments/352604.html</wfw:comment><comments>http://www.blogjava.net/liuyxit/archive/2011/06/19/inputStream_byte.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liuyxit/comments/commentRss/352604.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liuyxit/services/trackbacks/352604.html</trackback:ping><description><![CDATA[import   java.io.ByteArrayOutputStream; 
import   java.io.File; 
import   java.io.FileInputStream; 
import   java.io.FileNotFoundException; 
import   java.io.FileOutputStream; 
import   java.io.IOException; 
import   java.io.InputStream; 
import   java.io.OutputStream; 

public   class   FileByteUtil   { 
public   static   void   main(String[]   args)   throws   Exception   { 
File   file=new   File( "f:/test.doc "); 
byte[]   fileByte   =   file2byte(file); 
byte2file(fileByte,   "f:/test2.doc "); 
} 

public   static   byte[]   file2byte(File   f)   throws   Exception   { 
  return   file2byte(f.getPath()); 
} 

public   static   byte[]   file2byte(String   f)   throws   Exception   { 
try   { 
InputStream   in   =   new   FileInputStream(f); 
byte[]   tmp   =   new   byte[512]; 
ByteArrayOutputStream   out   =   new   ByteArrayOutputStream(); 
int   bytesRead   =   in.read(tmp); 
while   (bytesRead   !=   -1)   { 
out.write(tmp,   0,   bytesRead); 
bytesRead   =   in.read(tmp); 
} 
return   out.toByteArray(); 
}   catch   (Exception   e)   { 
e.printStackTrace(); 
} 
return   null; 
} 

//   writes   byte   []   to   a   file 
public   static   void   byte2file(byte[]   data,   String   fn)   throws   Exception   { 
try   { 
OutputStream   out   =   new   FileOutputStream(fn); 
out.write(data); 
out.flush(); 
}   catch   (FileNotFoundException   e)   { 
throw   e; 
}   catch   (IOException   e)   { 
throw   e; 
} 
} 

}<img src ="http://www.blogjava.net/liuyxit/aggbug/352604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liuyxit/" target="_blank">三刀流の逆风</a> 2011-06-19 02:01 <a href="http://www.blogjava.net/liuyxit/archive/2011/06/19/inputStream_byte.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>