我在研究中遇到如题的问题。具体情况如下:
不考虑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已经不会报错了,但是数据库中仍然没有相应的数据。在网上搜索了很多相关的回答,都没有解决问题,请高手指点一下。
不考虑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已经不会报错了,但是数据库中仍然没有相应的数据。在网上搜索了很多相关的回答,都没有解决问题,请高手指点一下。
解决方案 »
- 网络延迟造成的问题帮忙看下
- JSP开发的论坛中有关用户登录状态,及记住登录状态的问题
- 急问,高分求解JSP页面传值的问题
- EL表达非常奇怪的一个问题,求救!!!!!
- 在struts里如何在页面显示外接的C程序?C程序怎样和Struts接口?
- 请问如何实现JSP页面的翻页功能~~~多谢
- 404问题
- 如何单击一个button按钮后去执行一个函数,不好意思这个问题也问(:->
- 使用jspSmartUpload 出现的问题,望高手指教!
- 请问struts.xml中如何配置jsp文件的存放路径?
- 在struts2前加filter出现的问题
- 使用 htmlparser 包获取网页中的 超链信息 和 超链标题
经过几个小时的研究,找资料,问题已经初步解决了,下面我把问题和解决都复述一遍,以便后来别像我一样东找西找。
问题:用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");
}
}