title: pgql/Hibernate File up/download - download size twice file size?
body: <p>i'm having trouble realizing file up/download with Java HttpServlet, Hibernate(3.5) &amp; Postgresql Server(9.1.9)
  in general the problem is when i'm uploading a file via: </p>
  
  <pre><code>FileItemFactory factory = new DiskFileItemFactory();
  
      ServletContext servletContext = this.getServletConfig().getServletContext();
      File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
      ((DiskFileItemFactory) factory).setRepository(repository);
  
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
          List&lt;FileItem&gt; items = upload.parseRequest(request);
              byte[] dataBytes = items.get(0).get();
          Session session = (Session) entityManager.getDelegate();
          SQLQuery query = session
                  .createSQLQuery("SELECT nextval('test_vorgabe.seq_did')");
          Long key = ((BigInteger) query.uniqueResult()).longValue();
          session.flush();
          Datei datei = new Datei();
          datei.setDid(key);
          datei.setRevision(new Date(System.currentTimeMillis()));
          datei.setFilename(items.get(0).getName()
                  );
          datei.setFile(dataBytes);
          session.save(datei);
          session.flush();
  
      } catch (FileUploadException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>which seems to be working like it should (the file size is also the same as it is on my computer)
  But the problem is, when i'm downloading it - the file size is twice as it was and the file doesn't work anymore when i'm sending it back to the client.</p>
  
  <pre><code>        String dateiname = request.getReader().readLine();
      System.out.println("Dateiname: " + dateiname);
      dateiname = dateiname.replace("isc_TextItem_", "");
      dateiname = dateiname.substring(2);
  
      while (dateiname.startsWith("=")) {
          dateiname = dateiname.substring(1);
      }
      System.out.println(dateiname);
      Session session = (Session) entityManager.getDelegate();
      Criteria crit = session.createCriteria(Datei.class);
  
      crit.add(Restrictions.eq("filename", dateiname));
      Datei datei = (Datei) crit.list().get(0);
      response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      response.setHeader("Content-disposition", "attachment;filename=\""
              + datei.getFilename() + "\"");
      ServletOutputStream stream = response.getOutputStream();
      stream.write(datei.getFile());
      stream.flush();
      stream.close();
  </code></pre>
  
  <p>i'll also include the source of the entity bean: </p>
  
  <pre><code>@Entity
  @Table(name = "datei", schema = "test_vorgabe")
  public class Datei implements java.io.Serializable {
  
  private long did;
  private Vorgabe vorgabe;
  private String filename;
  private byte[] file;
  private Date revision;
  
  public Datei() {
  }
  
  public Datei(long did, String filename, byte[] file, Date revision) {
      this.did = did;
      this.filename = filename;
      this.file = file;
      this.revision = revision;
  }
  
  public Datei(long did, Vorgabe vorgabe, String filename, byte[] file,
          Date revision) {
      this.did = did;
      this.vorgabe = vorgabe;
      this.filename = filename;
      this.file = file;
      this.revision = revision;
  }
  
  @Id
  @Column(name = "did", unique = true, nullable = false)
  public long getDid() {
      return this.did;
  }
  
  public void setDid(long did) {
      this.did = did;
  }
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "vid")
  public Vorgabe getVorgabe() {
      return this.vorgabe;
  }
  
  public void setVorgabe(Vorgabe vorgabe) {
      this.vorgabe = vorgabe;
  }
  
  @Column(name = "filename", nullable = false)
  public String getFilename() {
      return this.filename;
  }
  
  public void setFilename(String filename) {
      this.filename = filename;
  }
  
  @Column(name = "file", nullable = false)
  public byte[] getFile() {
      return this.file;
  }
  
  public void setFile(byte[] file) {
      this.file = file;
  }
  
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "revision", nullable = false, length = 29)
  public Date getRevision() {
      return this.revision;
  }
  
  public void setRevision(Date revision) {
      this.revision = revision;
  }
  
  }
  </code></pre>
  
  <p>it is saved as bytea in postgresql </p>
  
  <p>can anybody help me solve this problem?</p>
  
  <p>thnx in advance</p>