Taking the contents of a Blob and writing it to a file

Discussion in 'Java' started by MattC, Oct 22, 2005.

  1. MattC

    MattC Guest

    I have a java.sql.Blob object that represents an XML file. I would like
    to right the Blob to a file on the file system. Can someone tell me an
    easy way to do this?
    MattC, Oct 22, 2005
    #1
    1. Advertising

  2. MattC

    Roedy Green Guest

    On 22 Oct 2005 08:31:12 -0700, "MattC" <>
    wrote or quoted :

    >I have a java.sql.Blob object that represents an XML file. I would like
    >to right the Blob to a file on the file system. Can someone tell me an
    >easy way to do this?


    use Blob.getBytes. From there see
    http://mindprod.com/applets/fileio.html
    for how you might decode it, and write it as a string, or write it as
    raw bytes.

    XML is a character string so it properly belonged in a Clob, not a
    Blob.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 23, 2005
    #2
    1. Advertising

  3. MattC

    steve Guest

    On Sun, 23 Oct 2005 18:28:01 +0800, Roedy Green wrote
    (in article <>):

    > On 22 Oct 2005 08:31:12 -0700, "MattC" <>
    > wrote or quoted :
    >
    >> I have a java.sql.Blob object that represents an XML file. I would like
    >> to right the Blob to a file on the file system. Can someone tell me an
    >> easy way to do this?

    >
    > use Blob.getBytes. From there see
    > http://mindprod.com/applets/fileio.html
    > for how you might decode it, and write it as a string, or write it as
    > raw bytes.
    >
    > XML is a character string so it properly belonged in a Clob, not a
    > Blob.
    >
    >
    >


    you have to be a bit careful!!.
    if it's in a clob and you are on oracle and your NLS language is set
    incorrectly , your database will translate the content of it's clobs to a
    different codepage, which might include conversion from single byte to
    multibyte character sets.


    sometimes , i might want to have chinese characters on my oracle database, i
    generally write them to a blob , so that the database does not tanslate and
    interpret them as single byte, instead of multibyte.


    that said:
    this is the code to get a blob , the "photo" is stored in the content field
    of the database.
    just modify the "bo" to a file stream


    public static byte[] getPhoto(long photoId) throws Exception {
    static final int MAXBUFSIZE = 4096;
    Section section = null;
    PreparedStatement st = null;
    ResultSet rset = null;

    try {
    String sql =
    "Select content from photo_store where deleted=0 and
    id=?";
    st = c.prepareStatement(sql);
    st.setLong(1, photoId); // Bind the photo id

    ////Oracle
    rset = st.executeQuery(); // Execute Query

    oracle.sql.BLOB blob = null;

    if (rset.next()) /*this is always true*/ {
    blob = (oracle.sql.BLOB) rset.getObject(1);
    }

    if (blob != null) {
    BufferedInputStream bis =
    new BufferedInputStream(blob.getBinaryStream());
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    byte[] buf = new byte[MAXBUFSIZE];
    int n = 0;

    while ((n = bis.read(buf, 0, MAXBUFSIZE)) != -1) {
    bo.write(buf, 0, n);
    }

    bo.flush();
    bo.close();
    bis.close();

    buf = null;

    return bo.toByteArray();
    }
    } catch (Exception ex) {
    Error_stuff.handleError(ex, -1, -1);

    // ex.printStackTrace();
    } finally {
    if (st != null) {
    try {
    st.close();
    } catch (Exception ex1) {
    Error_stuff.handleError(ex1, -1, -1);
    }
    }
    }

    return null;
    }
    steve, Oct 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maury

    Writing a BLOB by UPDATE

    Maury, Jul 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,495
    =?Utf-8?B?RWx0b24gVw==?=
    Jul 8, 2005
  2. MattC
    Replies:
    4
    Views:
    3,905
    Thomas Hawtin
    Dec 30, 2005
  3. Pounce
    Replies:
    0
    Views:
    430
    Pounce
    Jul 31, 2006
  4. Fred

    Re: SQLITE Blob writing error

    Fred, Nov 14, 2008, in forum: C Programming
    Replies:
    3
    Views:
    310
    Ben Bacarisse
    Nov 14, 2008
  5. Jim Cain
    Replies:
    1
    Views:
    206
    Yukihiro Matsumoto
    Jul 18, 2003
Loading...

Share This Page