请教:为什么java向oracle中存图片,大于10k就不行

hellevil 2008-12-24 05:37:06
我在研究中遇到如题的问题。具体情况如下:
不考虑try和其他因素的代码。
File file = new File(filePath);
if(!file.exists()){
throw new RuntimeException(file.getAbsolutePath()+" not exists");
}
FileInputStream pic = new FileInputStream(file);
Connection conn = this.getConnection();

PreparedStatement pstmt = conn
.prepareStatement("insert into gphoto values (empty_blob(),?,?,?)");
pstmt.setString(1, "photoName");
pstmt.setString(2, file.getAbsolutePath());
pstmt.setString(3, String.valueOf(file.length()));

pstmt.executeUpdate();

pstmt = null;
pstmt = conn.prepareStatement("update gphoto set photobinary=? where photoresource=?");
pstmt.setBinaryStream(1, pic, pic.available());
pstmt.setString(2, file.getAbsolutePath());
pstmt.executeUpdate();
之前我只是直接存InputStream,但是一旦文件长度超过10k,就会出现错误,报连接超时错误,后来在网上看到别人说先存一个空的blob,然后更新,就变成了现在的代码。现在超过10K已经不会报错了,但是数据库中仍然没有相应的数据。在网上搜索了很多相关的回答,都没有解决问题,请高手指点一下。
...全文
106 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hellevil 2008-12-25
  • 打赏
  • 举报
回复
恩,问的好。不过我已经说我在数据库里面插入了一个空的blob了,看来兄弟没有认真看啊。
经过几个小时的研究,找资料,问题已经初步解决了,下面我把问题和解决都复述一遍,以便后来别像我一样东找西找。
问题:用JAVA向ORACLE中插入图片,假设需要插入图片的字段为BLOB类型。
解决原型:看代码。
//省略包和引用
public class PhotoUtil {

public PhotoUtil() {

}
//传入需要存入的文件路径
public void PhotoSave(String filePath) {
File file = new File(filePath);
if(!file.exists()){
throw new RuntimeException(file.getAbsolutePath()+" not exists");
}
try {
//构造输入流,后面用到
FileInputStream pic = new FileInputStream(file);
Connection conn = this.getConnection();
//关闭连接自动发送,比较重要,不关闭会在select for update模式的查询时报错误
conn.setAutoCommit(false);
Statement st = conn.createStatement();

//首先向数据库中存入一个空的blob,这是存储超过10k图片必须的顺序,直存inputstream也可以,不过图片无法超过10k大小
PreparedStatement pstmt = conn
.prepareStatement("insert into gphoto values (empty_blob(),?,?,?)");
//一个无用字段
pstmt.setString(1, "photoName");
//文件路径
pstmt.setString(2, file.getAbsolutePath());
//文件长度
pstmt.setString(3, String.valueOf(file.length()));

pstmt.executeUpdate();

//查询该条记录
ResultSet rs = st.executeQuery("select photobinary from gphoto where photoresource='"
+file.getAbsolutePath()+"' for update");
while(rs.next()){
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
//用Buffer输入流打开pic输入流
BufferedInputStream bis = new BufferedInputStream(pic);
//以缓存的大小逐步读取文件,并以打印流输出到blob中。
byte[] buff = new byte[1024];
int n=0;
while ((n = bis.read(buff)) != -1) {
ps.write(buff, 0, n);
}
ps.flush();
ps.close();
bis.close();
}

pstmt.close();
conn.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public Connection getConnection() throws SQLException,ClassNotFoundException{
//省略获得连接,这个给出也没有意义,各不相同。
}

public static void main(String[] args){
PhotoUtil util = new PhotoUtil();
util.PhotoSave("d:\\DSC00124.JPG");
System.out.println("done");
}
}
凌霄野鹤 2008-12-24
  • 打赏
  • 举报
回复
你存图片的字段是什么类型的?blob还是clob?

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧