title: How to save doc, pdf, and image files to mysql database using java?
body: <p>I am trying to save <code>.doc</code>, <code>.pdf</code>, <code>.txt</code>, and image files into my database using hibernate, jsf, and mysql.</p>
  
  <p>I have created a column to save the file of type BLOB. If I am saving <code>.txt</code> type then files are saved correctly.</p>
  
  <p>If i am trying to save file of any other format then i am getting an exception.
  In my bean I have created a field name: <code>byte[] file;</code></p>
  
  <p>How i can save it correctly without any exceptions? Do I need to change datatype for mysql column or use a different field for java class?</p>
  
  <hr>
  
  <p>(in response to BalusC)</p>
  
  <p>This is the code which I am using for file writing. I am using <code>fileInputStream</code> and then saving the file using hibernate framework.</p>
  
  <pre><code>Iterator iter = items.iterator();
  while (iter.hasNext()) {
  FileItem item = (FileItem) iter.next();
  
  if (item.isFormField()) {
     String name = item.getFieldName();
     String value = item.getString();
  } else {
     String fieldName = item.getFieldName();
     String fileName = item.getName();
     String contentType = item.getContentType();
     boolean isInMemory = item.isInMemory();
     long sizeInBytes = item.getSize();
     byte[] fileInBytes=item.get();
  
  
     try {
        File uploadedFile = new File("/home/db/webApp", fileName);
        uploadedFile.createNewFile();
        FileInputStream fileInputStream = new FileInputStream(uploadedFile);
        //convert file into array of bytes
        fileInputStream.read(fileInBytes);
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  
     UploadedFile object= new UploadedFile(); 
     object.setFile(fileInBytes);
     uploadedObject.setFileName(fileName);
     session.save(object);
  </code></pre>
  
  <p><code>UploadedFile</code> is jsf managed bean:</p>
  
  <pre><code>public class UploadedFile{
     private String fileName;
     private byte[] file;
     /**
      * @return the fileName
      */
     public String getFileName() {
        return fileName;
     }
     /**
      * @param fileName the fileName to set
      */
     public void setFileName(String fileName) {   
        this.fileName = fileName;
     }
     /**
      * @return the file
      */
    public byte[] getFile() {
       return file;
    }
    /**
     * @param file the file to set
     */
    public void setFile(byte[] file) {
        this.file = file;
     }
  }
  </code></pre>
  
  <p>and my database table has following structure:</p>
  
  <pre><code>Create UploadFile(FILE_NAME` VARCHAR(1000) NOT NULL,
   `FILE` BLOB NOT NULL);
  </code></pre>