0

I have a JSF form where the user uploads data. Now I need to store the file uploaded (could be pdf, doc, xls, etc) into the database. Now of course this file will be loaded later by reviewers. Any idea what is the best way to do this? Serialization? and if yes, any where specific I should look?

I've seen that most serialization deal with user created objects (ones I am familiar with) like employee objects, buildings, etc. But I don't know how to represent the file document as object. as In what to pass to inputReader to get the binary string...

Thanks,

1
  • how about storing the metadata about the file in the database ... but actual content not in the database but letting it live on file system ... I usually find that aproach much cleaner Commented Mar 17, 2012 at 11:09

2 Answers 2

1

There's no need for serialization at all here - a file is (simplistically) just a name and the data within it. So you just need a name field of type VARCHAR (or whatever) and a data fields of type IMAGE or whatever type your database uses for blobs.

You can use PreparedStatement.setBlob (or potentially PreparedStatement.setBinaryStream; I'll readily admit I'm not confident in the differences) to upload the data.

Sign up to request clarification or add additional context in comments.

1 Comment

wow i hope that works...I will try to make that work although it seems simple enough :) thanks mate for the snappy reply! :D
1

To insert a binary document into a database you need a table with a column of type BLOB (binary large object).

A skeleton to write:

 File f = new File( "your.doc")
 FileInputStream   fis   = new FileInputStream( );
 PreparedStatement pstmt = dbCon.prepareStatement("insert into docs(doc) values(?)" );
 pstmt.setBinaryStream( 1, fis, (int) l.length() );
 pstmt.executeUpdate();
 pstmt.close();

to read from the DB:

  Statement  st    = dbCon.createStatement();
  ResultSet  rs    = st.executeQuery("select doc from docs where ..." );
  if( rs.next() )
  {
    InputStream      is  = rs.getBinaryStream( "doc" );
    FileOutputStream fos = new FileOutputStream( "filename" );
    // copy is to fos

  }
  rs.close();
  st.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.