小文件:

private boolean loadFile(Connection con,String filename)...{
PreparedStatement pstmt=null;
try...{
File file=new File(filename);
FileInputStream fin=new FileInputStream(file);
long filesize=file.length();
System.out.println("存入文件:"+filename+" 长度:"+filesize);
String sql="insert into filelist (n_file_id,n_file_length,vc_file_name," +
"b_file,c_file)values(filelist_seq.nextval,?,?,empty_blob(),empty_clob())";
// String sql="insert into filelist (n_file_id,n_file_length,n_file_name) values (filelist_seq.nextval,?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setLong(1,filesize);
pstmt.setString(2,filename);
// pstmt.setObject(3,file,Types.BINARY);
pstmt.setBinaryStream(3,fin,(int)filesize);
// pstmt.setBinaryStream(4,fin,(int)filesize);
return pstmt.execute();
// stmt.

}catch(Exception e)...{
e.printStackTrace();
}finally...{
try ...{
pstmt.close();
con.close();

} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}如上所示,用的是pstmt.setBinaryStream(3,fin,(int)filesize); 将文件的输入流直接当成参数传入,剩下的工作就交给
statement来执行了!,仔细观察参数:,(int)filesize 表示要写入的字节数,是一个int型数据,int型数据最大也就65535,
也就是说: 对于大文件,这样的方法是不可取的!
下面是我抄来的大文件上传方法,总的来说,就是先插入空BLOB或空CLOB, 然后再用update 写入数据流
package admit.action;
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import oracle.sql.*;
public class WriteBlob ...{ 

public static void main(String[] args) ...{


try ...{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.100:1521:adminsys","admit","wuyue");
conn.setAutoCommit(false); 
ReadPath readPath=new ReadPath();
ArrayList list = readPath.okpath();
/**//* String ff[]=new String[2];
String fileName = "c://tupian.gif"; */

for(int i=0;i<list.size();i++)...{
System.out.println("路径名="+list.get(i).toString());
File f = new File(list.get(i).toString());
FileInputStream fin = new FileInputStream(f);
BLOB blob = null; 
PreparedStatement pstmt = conn.prepareStatement("insert into javatest(name,context) values(?,empty_blob())");
pstmt.setString(1,""+list.get(i).toString()+"");
pstmt.executeUpdate();
pstmt.close(); 
pstmt = conn.prepareStatement("select context from javatest where name= ? for update");
pstmt.setString(1,""+list.get(i).toString()+"");
ResultSet rset = pstmt.executeQuery();
if (rset.next()) blob = (BLOB) rset.getBlob(1); 
pstmt = conn.prepareStatement("update javatest set context=? where name=?"); 
OutputStream out = blob.getBinaryOutputStream(); 
int count = -1, total = 0;
byte[] data = new byte[(int)fin.available()];
fin.read(data);
out.write(data); 
/**//*
byte[] data = new byte[blob.getBufferSize()]; 另一种实现方法,节省内存
while ((count = fin.read(data)) != -1) {
total += count;
out.write(data, 0, count);
}
*/ 
fin.close();
out.close(); 
pstmt.setBlob(1,blob);
pstmt.setString(2,""+list.get(i).toString()+"");
pstmt.executeUpdate();
pstmt.close(); 
System.out.println("插入成功!!!");
}
conn.commit();
conn.close();

} catch (SQLException e) ...{
System.err.println(e.getMessage());
e.printStackTrace(); 
} catch (IOException e) ...{
System.err.println(e.getMessage());
}
}


}



5236

被折叠的 条评论
为什么被折叠?



