我有一个zip文件存储在Oracle数据库表中,该表由三个不同的xml文件组成。我想做的是在本地文件系统中获取此zip文件。要检索此zip文件,我已在Java代码下面编写了代码:
package com.pack.IOT;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test2 {
public static Connection con;
public static PreparedStatement stmt;
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,uname,pwd);
stmt=con.prepareStatement("select * from FileStore");
ResultSet rs=stmt.executeQuery();
while (rs.next()) {
InputStream is = rs.getBinaryStream(3);
convert(is);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void convert(InputStream is) {
FileOutputStream outputStream = null;
File file = new File("C:\\Users\\nshaik\\Desktop\\IOTZip.zip");
try {
outputStream = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result;
result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
outputStream.write(buf.toByteArray());
System.out.println("File write success....");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
代码运行正常,但是我看到一个错误“存档意外结束”,并且该zip文件应该包含三个不同的文件,但是我只看到一个文件,大小为零。
我不确定我在做什么错,任何人的帮助都将不胜感激。
下面是上述要求的工作代码,
package com.pack.IOT;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test2 {
public static Connection con;
public static PreparedStatement stmt;
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,uname,pwd);
stmt=con.prepareStatement("select * from FileStore");
ResultSet rs=stmt.executeQuery();
while (rs.next()) {
InputStream is = rs.getBinaryStream(3);
convert(is);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void convert(InputStream is) {
FileOutputStream outputStream = null;
File file = new File("C:\\Users\\nshaik\\Desktop\\IOTZip.zip");
try {
outputStream = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result;
result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
outputStream.write(buf.toByteArray());
System.out.println("File write success....");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}