在将文件转成的二进制/base64形式的的会报数据太长,那么这个时候就要先插入一个空的blob值(这里是oracle),代码如下
public class fillUtil{
Connection connDoc = null ;
PreparedStatement stmtDoc = null ;
ResultSet res = null ;
public int saveFill ( InputStream is) {
byte [ ] value = null ;
Driver driver = new OracleDriver ( ) ;
DriverManager . deregisterDriver ( driver) ;
Properties pro = new Properties ( ) ;
pro. put ( "user" , "root" ) ;
pro. put ( "password" , "root" ) ;
connDoc = driver. connect ( "jdbc:oracle:thin:@localhost:1521:XE" , pro) ;
}
try {
connDoc. setAutoCommit ( false ) ;
String sql3 = "insert into files (code,content values(?,?)" ;
stmtDoc = connDoc. prepareStatement ( sql3) ;
stmtDoc. setString ( 1 , 1001 ) ;
stmtDoc. setBlob ( 2 , oracle. sql. BLOB. getEmptyBLOB ( ) ) ;
stmtDoc. executeUpdate ( ) ;
String sql4 = "select content from files where code = ?" ;
stmtDoc = connDoc. prepareStatement ( sql4) ;
stmtDoc. setString ( 1 , requestId) ;
res = stmtDoc. executeQuery ( ) ;
res. next ( ) ;
BLOB contentBlob = ( BLOB) res. getBlob ( 1 ) ;
OutputStream outputstream = contentBlob. getBinaryOutputStream ( ) ;
outputstream. write ( value) ;
outputstream. flush ( ) ;
outputstream. close ( ) ;
connDoc. commit ( ) ;
} catch ( Exception e) {
try {
if ( connDoc != null )
connDoc. rollback ( ) ;
} catch ( SQLException e1) {
e1. printStackTrace ( ) ;
}
e. printStackTrace ( ) ;
return 0 ;
} finally {
try {
res. close ( ) ;
stmtDoc. close ( ) ;
connDoc. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
public static byte [ ] inputStreamToBytes ( InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
byte [ ] buff = new byte [ 4096 ] ;
int index = 0 ;
while ( ( index = inputStream. read ( buff, 0 , 4096 ) ) > 0 ) {
baos. write ( buff, 0 , index) ;
}
return baos. toByteArray ( ) ;
}
public static String byte2Base64StringFun ( byte [ ] b) {
String str = "" ;
try {
BASE64Encoder encoder = new BASE64Encoder ( ) ;
str = Util . null2String ( encoder. encode ( b) ) . replaceAll ( "[\\s*\t\n\r]" , "" ) ;
} catch ( Exception e) {
log. writeLog ( "byte[]转base64报错,错误信息---->" + e. toString ( ) ) ;
}
return str;
}
}