Search code examples
javadownloadzip

Download blob data in a zip file in a simple java program


I'm not able to download the files using the below code as i have multiple types of extensions to download and i want to zip all the files and download. If i run the below code, it says files is corrupted. someone please help on this?

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

Solution

  • No front end is required for this. Issue is resolved using the below code.

    public class BlobDataExtract { static ZipOutputStream zos = null; static String url = "jdbc:oracle:thin:@hostname: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();
        }
    
    }
    

    }