我无法使用以下代码下载文件,因为我有多种类型的扩展可供下载,并且我想压缩所有文件并下载。如果我运行下面的代码,它会说文件已损坏。请有人帮忙解决这个问题吗?
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcReadFile {
private static final int BUFFER_SIZE = 4096;
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@xxx:xxxx:xxx";
String user = "xxx";
String password = "xxx";
String filePath = "C:/abc.vsd";
try {
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT DOC_BO FROM table fetch first 10 rows only";
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet result = statement.executeQuery();
if (result.next()) {
Blob blob = result.getBlob("DOC_BO");
InputStream inputStream = blob.getBinaryStream();
OutputStream outputStream = new FileOutputStream(filePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("File saved");
}
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
浮云间
相关分类