4 Replies Latest reply on Aug 4, 2009 2:20 PM by Dolphin Here

    open a file from a server location

    Dolphin Here Newbie

      Hi all,

      im totally struck.

      kindly guide me with options to proceed.

      the requirement is that i need to open a file(pdf) from a server location on a button action(download).

      the file name is dynamically determined. some thing similar to downloading a bill.

      is this possible?

      kindly guide me.

        • 1. Re: open a file from a server location
          Shervin Asgari Master

          The best practise is to have the files in the database.
          If you do, you can call a method from your view:

          public void download(Long fileId) throws IOException, EntityNotFoundException {
                    File f = entityManager.find(File.class, fileId);
                    if (f == null) {
                         throw new EntityNotFoundException();

          Then you can serve the file back to the client

          public void download(File file) throws IOException {
                    byte[] data = file.getData();
                    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                    //Take mime type from filename, I don't know if the null check is necessary, the API doesn't say. 
                    String mime = fileNameMap.getContentTypeFor(file.getName());
                    if (mime == null || "".equals(mime.trim())) {
                         mime = "application/octet-stream";
                    response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
                    OutputStream writer = response.getOutputStream();

          Hope this helps

          • 2. Re: open a file from a server location
            Dolphin Here Newbie
            thanks that you had helped me to proceed with a way.

            i started this way...

            in db:
            create directory DIR1 as 'd:\data'
            create table filetable(id number(6),filename bfile)
            insert into filetable values (1,bfilename('DIR1','filename'));

            so having this set up.
            i created the entity for filetable.

            now wen i tried the way that you had suggested, i am struck with the start up.

            1. byte[] data = file.getData();
                i dont fine getData() function with file.

            2. String mime = fileNameMap.getContentTypeFor(file.getName());
                fileNameMap... canu tel me what is this.

            am with very basic knowledge about this. so pardon me if i had missed any setup that i had to do.

            do kindly guide me.

            • 3. Re: open a file from a server location
              Shervin Asgari Master

              First you need to make sure you table is byte.

              Here is how my File table looks like:

              import java.io.ByteArrayInputStream;
              import java.io.InputStream;
              import java.util.Date;
              import javax.persistence.Basic;
              import javax.persistence.Column;
              import javax.persistence.Entity;
              import javax.persistence.FetchType;
              import javax.persistence.GeneratedValue;
              import javax.persistence.Id;
              import javax.persistence.Lob;
              import javax.persistence.PrePersist;
              import javax.persistence.Temporal;
              import javax.persistence.TemporalType;
              import javax.persistence.Transient;
              import org.hibernate.validator.NotNull;
              public class File {
                   @Id @GeneratedValue
                   private Long id;
                   private String name;
                   //BLOB = L + 2 bytes (max size is 2^16 - 1 or 65,535 bytes, 65KB)
                   //MEDIUMBLOB = L + 3 bytes (max size is 2^24 - 1 or 16,777,215 bytes, 16MB)
                   //LONGBLOB = L + 4 bytes (max size is 2^32 - 1 or 4,294,967,295 bytes, 4GB)
                   @Basic(fetch = FetchType.LAZY) //@Basic is used in conjunction with @Lob
                   //Set to MAX 100MB MEDIUMBLOB in MySQL
                   @Column(length=1024721500, nullable=false)
                   private byte[] data;
                   private Date date;
                   @PrePersist //@PreUpdate - otherwise the create date will always be the same as the emailed date
                   public void setDate() {
                        date = new Date();
                   public InputStream getInputStream() {
                       return new ByteArrayInputStream(data);
                   //getter & setter

              I have a postconstruct that initiliaze the fileNameMap like this:

              public void contruct() {
                   fileNameMap = URLConnection.getFileNameMap();

              So file.getData() will retrieve the file.
              You need this so that the browser can show the file. You can read more about mimetypes by googling.

              • 4. Re: open a file from a server location
                Dolphin Here Newbie


                i didn't store the file in db as blob, instead i had created that as bfile

                should i step for change?

                or is there a way that i can continue with existing set up?