简体   繁体   中英

convert clob to blob

I have a DB which stores PDF files into CLOB column. I want to write a java program to retrive it. Just using Clob java class to retrive does not work. I think i need to convert CLOB datatype to BLOB and then write into a file. Can anyone tell me how to convert CLOB into BLOB using java program.


Here is the my db

DOC_ID NOT NULL VARCHAR2(60) 
DOC_NAME NOT NULL VARCHAR2(500) 
DOC_TYPE NOT NULL VARCHAR2(100) 
DOC_CONTENT CLOB() 

I am sending the DOC_ID to get the Doc_CONTENT . DOC_CONTENT will store in my local drive as doc_name


Here is my java code

PreparedStatement stmnt = conn.prepareStatement("select DOC_CONTENT, DOC_NAME from documentum_docs_queue where doc_id=" + documentid);

        ResultSet rs = stmnt.executeQuery();    
        Clob aBlob = null;
        String filename = "filename";
        while (rs.next()) {             
            try {                   
                aBlob = rs.getClob(1);  
                filename = rs.getString(2);
                //System.out.println("filename-------------- " + filename);
            } catch (Exception ex) {
                ex.printStackTrace();                   
                byte[] bytes = rs.getBytes(1);
            }
        }
        rs.close();
        stmnt.close();

        File blobFile = new File("c:/temp/writer/"+filename);
        //System.out.println("blobFile-------------- " + blobFile);
        FileOutputStream outStream = new FileOutputStream(blobFile);
        InputStream inStream = aBlob.getAsciiStream();

        int length = -1;
        int size = (int) aBlob.length();
        byte[] buffer = new byte[size];

        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
            outStream.flush();
        }

        inStream.close();
        outStream.close();

Conversion from CLOB to BLOB will not help. CLOB is a text format. It means, that database automaticaly converts stored data into database character set. PDF is a binary format, so the files are probably corrupted by storing into a database.

It is necessary to store files into BLOB columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM