# PDF aus Oracle ins Filesystem schreiben



## Guest (28. Feb 2006)

Hallo,

ich habe folgendes Problem. Ich habe in einer Oracle DB PDF Dateien im Form von BLOBs. Wie kann ich diese aus der DB lesen und ins Filesystem schreiben.
Ich habe es so versucht:

```
Object blob = rowset.getBlob(4);
...
...
// Dann über FileWriter in Datei schreiben...
File pdfFile = new File(url);
fileW = new FileWriter(pdfFile);
bufW = new BufferedWriter(fileW);
out = new PrintWriter(bufW);
out.print(blob);
out.close();
```
Kann mir jemand helfen?


----------



## flanker (28. Feb 2006)

Die Methode _getBlob(int c)_ liefert ein *Blob* und diese hat _byte[] getBytes();_
Damit hast du schonmal Inhalt (byte[]) der PDF-Datei.
Diese array kannst du dann *byte für byte* in deine _pdfFile_ schreiben.


----------



## abollm (28. Feb 2006)

Anonymous hat gesagt.:
			
		

> Hallo,
> 
> ich habe folgendes Problem. Ich habe in einer Oracle DB PDF Dateien im Form von BLOBs. Wie kann ich diese aus der DB lesen und ins Filesystem schreiben.
> [..]



Hier einmal ein von mir erweitertes, modifiziertes und erfolgreich getestetes Beispiel zum Thema:

```
/*
 * Created on 28.02.2006
 * 
 * Author: Mio ?
 *
 * History: Improved and modified by ABollm: Version 1.0 - 28.02.2006
 * 
 * Aufruf wie folgt: java OraLobUtils url user passwd [-write] [-blob] -qf query_file -lf lob_file
 * 
 * Die Datei "query_file" sollte ein SQL-Statement enthalten, das eine einzelnen LOB-Adresse zurückliefert. 
 * Standarmäßig liest die Applikation von der Oracle-DB und schreibt eine Datei "lob_file" auf das Dateisystem. 
 * Die Angabe des Parameters "-write" ist zum Lesen von der Datei "lob_file" und schreibt diese dies in die DB. 
 * Es wird ein CLOB vorausgesetzt, wobei der Parameter "-blob" für binäre Daten benutzt wird.
 * 
 * Ursprünglich für Oracle 8.1.7 erstellt
 * 
 * In der hier vorliegenden Version auch mit Oracle 9.2.0.5 erfolgreich getestet.
 * 
 * TODO :
 * 
 */
package ora;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Blob;
import oracle.sql.BLOB;
import java.sql.SQLException;

import java.sql.Clob;
import oracle.sql.CLOB;

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.StringWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.io.InputStreamReader;

public class OraLobUtils 
{
    final static int bBufLen = 4 * 8192;
    String query;
    String url;  // z.B.: "jdbc:oracle:oci:@ALLFATST";  
                 // auch den entsprechenden Treiber eintragen; z.B. wie hier "oci" 
    //String connectString;
    String user;
    String passwd;
    String outFile;
    Connection conn;

    public OraLobUtils(String url, String user, String passwd, String query, String outFile) throws SQLException {
        this.url = url;
        this.user = user;
        this.passwd = passwd;
        this.query = query;
        this.outFile = outFile;
        this.conn = null;
        // Laden des richtigen JDBC-Treibers
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    }

    public static void main(String[] args)
      throws FileNotFoundException, IOException, SQLException {
        if (args.length < 6) usage();
        int ii = 0;
        String url = args[ii++];
        String user = args[ii++];
        String passwd = args[ii++];
        String queryFile = null;
        String outputFile = null;

        boolean read = true;
        boolean isBinary = false;

        for (; ii < args.length; ii++) {
            if (args[ii].equals("-write"))
                read = false;

            if (args[ii].equals("-blob"))
                isBinary = true;

            if (args[ii].equals("-qf") && ii < args.length - 1)
                queryFile = args[++ii];

            if (args[ii].equals("-lf") && ii < args.length - 1)
                outputFile = args[++ii];

        }

        if (queryFile == null || outputFile == null) usage();

        // Alle Ausgaben
        if (read) {
            BufferedReader freader = new BufferedReader(new FileReader(queryFile));
            StringWriter swriter = new StringWriter();
            int bufLen = 1024;
            char[] cbuf = new char[bufLen];
            int length = -1;
            while ((length = freader.read(cbuf, 0, bufLen)) != -1) {
                swriter.write(cbuf, 0, length);
            }
            freader.close();
            swriter.close();
            String query = swriter.toString();

            OraLobUtils olutils = new OraLobUtils(url, user, passwd, query, outputFile);
            if (isBinary) {
                Blob blob = olutils.getBlob();
                long wrote = olutils.writeBlobToFile(blob);
                System.out.println("Schrieb " + wrote + " Bytes in Datei " + outputFile);
            } else {
                Clob clob = olutils.getClob();
                long wrote = olutils.writeClobToFile(clob);
                System.out.println("Schrieb " + wrote + " Bytes in Datei " + outputFile);
            }
        } else {
            BufferedReader freader = new BufferedReader(new FileReader(queryFile));
            StringWriter swriter = new StringWriter();
            int bufLen = 1024;
            char[] cbuf = new char[bufLen];
            int length = -1;
            while ((length = freader.read(cbuf, 0, bufLen)) != -1) {
                swriter.write(cbuf, 0, length);
            }
            freader.close();
            swriter.close();
            String query = swriter.toString();

            OraLobUtils lutils = new OraLobUtils(url, user, passwd, query, outputFile);
            Clob clob = lutils.getClob();
            InputStream creader = new FileInputStream(outputFile);
            long wrote = lutils.writeToOraClob(clob, creader);
            System.out.println("Schrieb " + wrote + " Bytes von Datei " + outputFile);
        }
    }

    public Clob getClob()
      throws SQLException {
    	conn = DriverManager.getConnection(url, user, passwd);

        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        Clob clob = null;
        if (rs.next()) {
            clob = rs.getClob(1);
        }
        return clob;
    }

    public Blob getBlob()
      throws SQLException {
    	conn = DriverManager.getConnection(url, user, passwd);
    	//conn = ConnUtil.getConnection(connectString);
        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        Blob blob = null;
        if (rs.next()) {
            blob = rs.getBlob(1);
        }
        return blob;
    }

    public long writeClobToFile(Clob clob)
      throws IOException, SQLException {
        long wrote = 0;
        BufferedWriter fwriter = new BufferedWriter(new FileWriter(outFile));
        wrote = readFromClob(clob, fwriter);
        fwriter.close();
        conn.commit();
        conn.close();
        return wrote;
    }

    public long writeBlobToFile(Blob blob)
      throws IOException, SQLException {
        long wrote = 0;
        OutputStream fwriter = new FileOutputStream(outFile);
        wrote = readFromBlob(blob, fwriter);
        fwriter.close();
        conn.commit();
        conn.close();
        return wrote;
    }

    private static void usage() {
        System.err.println("Aufruf: java OraLobUtils url user passwd [-write] [-blob] -qf query_file -lf lob_file");
        System.exit(1);
    }

    public static long writeToOraBlob(Blob blob, InputStream in)
      throws SQLException, IOException {
        BLOB oblob = (BLOB)blob;
        OutputStream out = oblob.getBinaryOutputStream();
        int length = -1;
        long wrote = 0;
        int chunkSize = oblob.getChunkSize();
        byte[] buf = new byte[chunkSize];
        while ((length = in.read(buf)) != -1) {
            out.write(buf, 0, length);
            wrote += length;
        }
        out.close();
        return wrote;
    }

    public long writeToOraClob(Clob clob, InputStream in)
      throws SQLException, IOException {
        CLOB oclob = (CLOB)clob;
        OutputStream out = oclob.getAsciiOutputStream();
        int length = -1;
        long wrote = 0;
        int chunkSize = oclob.getChunkSize();
        byte[] buf = new byte[chunkSize];
        while ((length = in.read(buf)) != -1) {
            out.write(buf, 0, length);
            wrote += length;
        }
        out.close();
        conn.commit();
        return wrote;
    }

    public static long readFromBlob(Blob blob, OutputStream out)
      throws SQLException, IOException {
        InputStream in = blob.getBinaryStream();
        int length = -1;
        long read = 0;
        byte[] buf = new byte[bBufLen];
        while ((length = in.read(buf)) != -1) {
            out.write(buf, 0, length);
            read += length;
        }
        in.close();
        return read;
    }

    public static long readFromClob(Clob clob, Writer out)
      throws SQLException, IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(clob.getAsciiStream()));
        int length = -1;
        long read = 0;
        char[] buf = new char[bBufLen];
        while ((length = in.read(buf, 0, bBufLen)) != -1) {
            out.write(buf, 0, length);
            read += length;
        }
        in.close();
        return read;
    }
}
```


----------

