需求:基于test.jsp从oracle的blob字段中批量下载邮件文件内容,将其写入rar压缩文件中,当前程序能够执行。
问题:打开下载的压缩文件temp_email.rar时,报错,不可预料的压缩文件末端。压缩文件中只有一条记录对应的文件,标示记录文件大小为0,但是压缩文件有几k大小。
请帮忙:看一下几个输入输出流的种类是否正确?
关闭时机是否正确?
还有什么可能原因?
不胜感激!!!
test.jsp相关代码如下:
//数据库连接
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@197.25.122.191:1521:test"; //orcl为你的数据库的SID  
String user = "test";
String password = "test"; Connection conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rs = null; //服务器本地压缩文件信息
String tempZipName = "temp_email.rar";
String tempZipFile = localTempPath + tempZipName; File tempFile = new File(tempZipFile);
FileOutputStream tempoutstream = new FileOutputStream(tempFile);
ZipOutputStream zipout = new ZipOutputStream(tempoutstream);

//循环下载,并保存为本地zip文件
for (int i = 0; i < emailNum; i++) { String emailInfo = emailInfoSet[i];
int idIndex = emailInfo.indexOf(" ");
emailId = Integer.parseInt(emailInfo.substring(0, idIndex));
String fileName = emailInfo.substring(idIndex, emailInfo.length()).trim() + "_" + emailInfo.substring(0, idIndex).trim();
//获得文件的编号id,并转换为整型
String sql = "select EMAIL_FILE from S_EMAIL_FILE WHERE EMAIL_ID=" + emailId + "";
//要执行查询的SQL语句
rs = stmt.executeQuery(sql); if (rs.next()) { java.sql.Blob blob = rs.getBlob("EMAIL_FILE"); InputStream blobins = blob.getBinaryStream(); String emailName = fileName + "." + fileType;

zipout.setLevel(1);
zipout.putNextEntry(new ZipEntry(emailName));
byte[] tempbytes = new byte[1024];
int filelen = 0; while ((filelen = blobins.read(tempbytes)) != -1) {
tempoutstream.write(tempbytes, 0, filelen);
} blobins.close();
//blobins.close();
zipout.closeEntry(); } } zipout.flush();
zipout.close();

tempoutstream.flush();
tempoutstream.close(); rs.close();
stmt.close();
conn.commit();
conn.setAutoCommit(true);