!!!(@_@)文件上传为何不能大于4k啊???错在那里了??? Oracle数据库中STRING类型字段不能超过4K,超过了应该使用CLOB,我没有仔细看你的异常信息(实在太乱了,SWEAT),如果你是使用STRING类型,那么请改为CLOB存储超过4K字节的数据。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 忘了说了,字段类型是Blob,(里面放的文档,doc,pdf等) 如果是Oracle中字段类型为varchar或varchar2(具体二者有什么区别,我还不太知道),那么最大长度是不能大于4k的,也即4XXX(4千多,具体忘了)。如果你要上传超过4K,那就用Blob吧,它可放任何二进制数据。如果是文本,也可用Clob,至于用法,CSDN上就有很多。 byte[] buffer....//文件内容int len=buffer.length;ByteArrayInputStream in=new ByteArrayInputStream(buffer);pstmt = conn.prepareStatement("insert into ITEM_PIC(ITEM_ID,PIC_NUM,PIC)values(?,?,?)");pstmt.setString(1, item_id);pstmt.setInt(2,pic_num);pstmt.setBinaryStream(3, in, len);pstmt.execute();这样操作的,有错误么??? 你这么写能不能成功我不知道,我现在也没法子试验。不过我们用过的成果的法子是先用 empty_blob() 将lob字段清空,然后select取得lob字段对应的 BinaryStream,然后对这个流进行操作。这种做法是从Oracle的官方文档里面弄下来的,好像是因为Weblogic 的DataSource 和Oracle自己的sql API 的问题,不管怎么样,反正是成功的方法吧。下面是我们用过的代码,希望对你有帮助: public void writeBlob(String tableName, String blobColumnName, String primaryKeyName, int id, byte[] blobContent) { try { ServiceLocator sl = new ServiceLocator(); DataSource ds = sl.getDataSource(JNDINames.GENERAL_DATASOURCE); Connection con = ds.getConnection(); Statement st = con.createStatement(); st.setQueryTimeout(120); String updateSql = "update " + tableName.trim() + " set " + blobColumnName + " = empty_blob()" + " where " + primaryKeyName.trim() + " = " + id; String querySql = "select " + blobColumnName.trim() + " from " + tableName.trim() + " where " + primaryKeyName.trim() + " = " + id; //System.err.println( "update sql = " + updateSql); //System.err.println( " query sql = " + querySql); st.executeUpdate( updateSql); ResultSet rs = st.executeQuery( querySql); if ( rs.next()){ OracleThinBlob blob = (OracleThinBlob)rs.getBlob( blobColumnName.trim()); OutputStream os = blob.getBinaryOutputStream(); ByteArrayInputStream byteStream = new ByteArrayInputStream( blobContent); byte[] line = new byte[ blob.getBufferSize()]; //确保存入的字节总数不变 int readTimes = blobContent.length / blob.getBufferSize(); int lastByteCount = blobContent.length % blob.getBufferSize(); for ( int i = 1; i <= readTimes+1; i++){ byteStream.read( line); if ( i <= readTimes ){ os.write( line, 0, line.length); }else{ os.write( line, 0, lastByteCount); } } os.flush(); byteStream.close(); os.close(); }else{ Debug.debug("Lob 写操作: session bean 中查询得到的ResultSet没有纪录。"); throw new EJBException(); } } catch (IOException ex) { Debug.debug(ex); throw new EJBException(ex); }catch (SQLException ex) { Debug.debug(ex); throw new EJBException(ex); }catch (ServiceLocatorException ex) { Debug.debug(ex); throw new EJBException(ex); } }//这段代码有些地方有问题的,请不要见笑 // String url = "jdbc:oracle:thin:@localhost:1521:oradb"; String url = "jdbc:oracle:oci8:@oradb";用下面的url(需要安装oracle的客户端) 我是受到启发了,看看这里啊http://www.csdn.net/develop/article/18/18987.shtm可惜我早没有看到这篇文档。 如何通过正则表达式切取字符串 java socket疑问 struts简单问题,进来抢分啊!!!!! 关于ActionMessage的初始化问题 用apache VFS实现FTP文件上传 ssh架构中,单表查询通过,多表联合查询总出现错误 原来一切正常,但是自从在SQL数据库里加了一个触发器后,就报错了,搞不懂??? tomcat5.0.19我安装了,Down之后就不能再启动 J2EE安装问题 单例模式 双重检测机制答疑 oracle9i是否支持ejb? 用过JBoss-4.0.0DR2来帮帮忙呀,怎样配置才能让jboss可用呀?第一次用,不会弄!
int len=buffer.length;
ByteArrayInputStream in=new ByteArrayInputStream(buffer);
pstmt = conn.prepareStatement("insert into ITEM_PIC(ITEM_ID,PIC_NUM,PIC)values(?,?,?)");
pstmt.setString(1, item_id);
pstmt.setInt(2,pic_num);
pstmt.setBinaryStream(3, in, len);
pstmt.execute();
这样操作的,有错误么???
不过我们用过的成果的法子是先用 empty_blob() 将lob字段清空,然后select取得lob字段对应的 BinaryStream,然后对这个流进行操作。
这种做法是从Oracle的官方文档里面弄下来的,好像是因为Weblogic 的DataSource 和Oracle自己的sql API 的问题,不管怎么样,反正是成功的方法吧。
下面是我们用过的代码,希望对你有帮助: public void writeBlob(String tableName, String blobColumnName, String primaryKeyName, int id, byte[] blobContent) {
try {
ServiceLocator sl = new ServiceLocator();
DataSource ds = sl.getDataSource(JNDINames.GENERAL_DATASOURCE);
Connection con = ds.getConnection();
Statement st = con.createStatement();
st.setQueryTimeout(120);
String updateSql =
"update " + tableName.trim() +
" set " + blobColumnName + " = empty_blob()" +
" where " + primaryKeyName.trim() + " = " + id;
String querySql =
"select " + blobColumnName.trim() +
" from " + tableName.trim() +
" where " + primaryKeyName.trim() + " = " + id;
//System.err.println( "update sql = " + updateSql);
//System.err.println( " query sql = " + querySql);
st.executeUpdate( updateSql);
ResultSet rs = st.executeQuery( querySql);
if ( rs.next()){
OracleThinBlob blob = (OracleThinBlob)rs.getBlob( blobColumnName.trim());
OutputStream os = blob.getBinaryOutputStream(); ByteArrayInputStream byteStream = new ByteArrayInputStream( blobContent); byte[] line = new byte[ blob.getBufferSize()];
//确保存入的字节总数不变
int readTimes = blobContent.length / blob.getBufferSize();
int lastByteCount = blobContent.length % blob.getBufferSize();
for ( int i = 1; i <= readTimes+1; i++){
byteStream.read( line);
if ( i <= readTimes ){
os.write( line, 0, line.length);
}else{
os.write( line, 0, lastByteCount);
}
}
os.flush();
byteStream.close();
os.close();
}else{
Debug.debug("Lob 写操作: session bean 中查询得到的ResultSet没有纪录。");
throw new EJBException();
}
}
catch (IOException ex) {
Debug.debug(ex);
throw new EJBException(ex);
}catch (SQLException ex) {
Debug.debug(ex);
throw new EJBException(ex);
}catch (ServiceLocatorException ex) {
Debug.debug(ex);
throw new EJBException(ex);
}
}//这段代码有些地方有问题的,请不要见笑
String url = "jdbc:oracle:oci8:@oradb";
用下面的url(需要安装oracle的客户端)
http://www.csdn.net/develop/article/18/18987.shtm
可惜我早没有看到这篇文档。