problems with writing from file to blob

Discussion in 'Java' started by Pounce, Jul 31, 2006.

  1. Pounce

    Pounce Guest

    I`m writing from file to blob. Than I need to get blob content again
    and write it to file. When I open this file, I see the wrong symbols.
    Can anybody help me?
    Here is my code:


    ////////////////////////////
    to update file content
    ///////////////////////////


    private void updateFileContent(File serverFile, java.io.File
    clientContentFile) throws MainException
    {
    PreparedStatement preparedStatement = null;
    try
    {


    connection = connect();
    String query = "update panta_file_substance\n" +
    "set content = ?\n" +
    "where\n" +
    " file_id = ?"
    preparedStatement = getPreparedStatement
    (query, connection);


    int fileLenght = (int) clientContentFile.length();
    InputStream is = new FileInputStream(clientContentFile);
    preparedStatement.setBinaryStream(1, is, fileLenght);
    preparedStatement.setLong( 2,
    serverFile.getId().longValue() );
    int result = preparedStatement.executeUpdate();
    is.close();
    }
    catch (SQLException e)
    { throw new MainException(e.getMessage(), e); }


    catch (FileNotFoundException e)
    { throw new MainException(e.getMessage(), e); }


    catch (IOException e)
    { throw new MainException(e.getMessage(), e); }


    finally
    {
    disconnect(null, preparedStatement, null);
    }
    }


    //////////////////////////////////////////////////////////////////////////

    to get file
    /////////////////////////////////////////////////////////////////////////



    public static void getFile(Connection connection) throws
    SQLException
    {
    String query = "select \n" +
    " panta_file_substance.content \n" +
    "from \n" +
    " panta_file_substance\n" +
    "where \n" +
    " file_id=?";
    PreparedStatement preparedStatement = connection.
    prepareStatement(ServerSQLProjectDAO.getFileContent());
    preparedStatement.setLong(1, 7801295236381635588l);
    ResultSet resultSet = preparedStatement.executeQuery();
    Blob content = null;
    if (resultSet.next())
    {
    content = (Blob) resultSet.getBlob("CONTENT");
    }
    java.io.File contentffFile = new java.io.File("C:\\Temp\\new");

    try
    {
    OutputStream os = new FileOutputStream(contentffFile);
    int bufferSize = 2048;
    byte[] array = new byte[bufferSize];
    BufferedOutputStream bos = new BufferedOutputStream(os,
    bufferSize);


    InputStream is = content.getBinaryStream();
    BufferedInputStream bis = new BufferedInputStream(is);


    int nBytes;


    while ((nBytes = bis.read(array)) != -1)
    bos.write(array, 0, nBytes);


    bos.close();
    bis.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }


    }
     
    Pounce, Jul 31, 2006
    #1
    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. HNguyen
    Replies:
    4
    Views:
    2,405
    HNguyen
    Dec 21, 2004
  2. Maury

    Writing a BLOB by UPDATE

    Maury, Jul 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,498
    =?Utf-8?B?RWx0b24gVw==?=
    Jul 8, 2005
  3. MattC
    Replies:
    2
    Views:
    9,137
    steve
    Oct 24, 2005
  4. Fred

    Re: SQLITE Blob writing error

    Fred, Nov 14, 2008, in forum: C Programming
    Replies:
    3
    Views:
    313
    Ben Bacarisse
    Nov 14, 2008
  5. Nick Keighley

    Re: SQLITE Blob writing error

    Nick Keighley, Nov 17, 2008, in forum: C Programming
    Replies:
    1
    Views:
    367
    Nick Keighley
    Nov 17, 2008
Loading...

Share This Page