读书人

Java Blob 字段转byte[] 数组有关问题

发布时间: 2012-12-16 12:02:32 作者: rapoo

Java Blob 字段转byte[] 数组问题
最近在做一个功能,就是说从数据库(数据库用的是DB2)里取出blob(blob 里存储的是一个zip文件) 数据,然后写到一个新的zip文件然后在解压此zip文件。但是在做到blob转byte[] 的时候报了个错。
com.ibm.db2.jcc.b.SqlException: [jcc][10120][11936][3.50.152] 操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null

下面是我的代码:

package test.from.mau.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import test.from.mau.ConnectionFactory;

public class UpZip {
public static void main(String[] args) throws Exception {
// String filePath = upZip();
ByteAndFile bf = new ByteAndFile();
// byte[] b = bf.getBytesFromFile(filePath);
byte[] b = getBytesFromDB();
File f = bf.getFileFromBytes(b, "d://a.zip");
}

private static byte[] getBytesFromDB() {
Connection con = null;
byte[] b = null;
Blob blob = null;
try {
if (con == null || con.isClosed()) {
con = ConnectionFactory.getConnection();
}
con.setAutoCommit(false);
PreparedStatement stmt = con
.prepareStatement("SELECT REPORT_DATA FROM RIS.EX_REPORT_DATA_TBL WHERE STUDY_LID = 844 AND EX_FORMAT = 20");
ResultSet rs = stmt.executeQuery();
con.commit();
while (rs.next()) {
blob = (Blob) rs.getBlob("REPORT_DATA");
}
b = blobToBytes(blob);
} catch (SQLException e) {
e.printStackTrace();
}
return b;
}

private static byte[] blobToBytes(Blob blob) {
BufferedInputStream is = null;
byte[] bytes = null;
try {
is = new BufferedInputStream(blob.getBinaryStream());
bytes = new byte[(int) blob.length()];
int len = bytes.length;
int offset = 0;
int read = 0;

while (offset < len
&& (read = is.read(bytes, offset, len - offset)) >= 0) {
offset += read;
}

} catch (Exception e) {
e.printStackTrace();
}
return bytes;
// byte[] b = null;
// try {
// if (blob != null) {
// long in = 0;
// b = blob.getBytes(in, (int) (blob.length()));
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// return b;

}

private static String upZip() throws IOException {
ZipInputStream zip = new ZipInputStream(
new FileInputStream("d://a.zip"));
ZipEntry ze = zip.getNextEntry();
OutputStream out = new FileOutputStream("d://" + ze.getName());


byte[] b = new byte[1024];
while (zip.read(b) != -1) {
out.write(b);
}
out.close();
zip.close();
return "d://" + ze.getName();
}
}



错误是在 blobToBytes() 方法,is = new BufferedInputStream(blob.getBinaryStream()); 这一行报的错。
我也试过网上说的执行命令 $ db2set DB2_RESTRICT_DDF=true 可是,执行命令的时候出现下面的提示:
C:\Users\risex>db2set DB2_RESTRICT_DDF=true
DB21009E This command must be launched from a command window running with full
administrative privileges.

本人第一次用DB2,不熟悉,希望大侠们帮帮兄弟,谢谢了。。。
[解决办法]
跪求高人解答。。。
[解决办法]
我是不是来错了?不该在这个板块提问啊?
[解决办法]
纳尼? 我不该来吗?
[解决办法]
请问楼主,你这个问题解决没,我现在也遇到同样的问题了。。。

读书人网 >J2EE开发

热点推荐