猿问

在简单的 java 程序中下载 zip 文件中的 blob 数据

我无法使用以下代码下载文件,因为我有多种类型的扩展可供下载,并且我想压缩所有文件并下载。如果我运行下面的代码,它会说文件已损坏。请有人帮忙解决这个问题吗?


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();

        }

    }

}


qq_花开花谢_0
浏览 79回答 1
1回答

浮云间

为此不需要前端。使用以下代码解决了问题。公共类 BlobDataExtract { 静态 ZipOutputStream zos = null; 静态字符串 url = "jdbc:oracle:thin:@主机名:1521:SID";public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {    Class.forName("oracle.jdbc.driver.OracleDriver");    Connection conn = DriverManager.getConnection(url, "user", "password");    String sql = "select Blob_Data,ORIG_NM from table";    PreparedStatement stmt = conn.prepareStatement(sql);    ResultSet rs = stmt.executeQuery();    byte[] docBlob = null;    String filename = null;    FileOutputStream fos = new FileOutputStream("C:/Users/test.zip");    zos = new ZipOutputStream(fos);    while (rs.next()) {        docBlob = rs.getBytes("Blob_Data");        filename = rs.getString("ORIG_NM");        try {            zos.putNextEntry(new ZipEntry(filename));            zos.write(docBlob, 0, docBlob.length);        } catch (FileNotFoundException ex) {            System.err.println("A file does not exist: " + ex);        } catch (IOException ex) {            System.err.println("I/O error: " + ex);        }        zos.closeEntry();    }}}
随时随地看视频慕课网APP

相关分类

Java
我要回答