2 Replies Latest reply on Dec 21, 2009 10:13 AM by David Bolsover

    Off Topic - Database object retrieval

    David Bolsover Newbie

      I'm afraid this is a bit off topic - but I'm posting here in the hope that someone may have tackled a similar problem...

       

      The major requirement of the project I'm currently working on is that of storing arbitrary file based data in a database. The data stored can potentially be any valid file: .pdf, .htm, .xls, .docx, .mp3, .zip, .exe etc.

       

      Storage of any file type is no problem.using <rich:fileUpload/> and an appropriate listener.

       

      Retrieval of some of these objects is however problematic in some cases; the following code works just fine for .pdf, .htm, .doc, .xls where the appropriate client side application (IE, word, excel etc.) is launched and opens the document. This method does not however work for all file types (mp3, .exe) since IE (and probably other browsers) does not know how to handle the 'pushed' data stream despite being told the mime type.

       

      public String actionOpenDocument() {
              HttpServletRequest request = AppUtils.getCurrentRequest();

      // this if required to prevent unwanted execution of the method

              if ((request.getParameter("AJAXREQUEST") == null) || request.getParameter("AJAXREQUEST").isEmpty()) {
                  String                docid    = request.getParameter("docid"); // get docid
                  DocumentDao           dao      = new DocumentDao(); // database access methods
                  Document              doc      = dao.retrieveDocumentById(docid); // get the document
                  FacesContext          fc       = FacesContext.getCurrentInstance();
                  HttpServletResponse   response = AppUtils.getCurrentResponse();
                  ByteArrayOutputStream baos     = new ByteArrayOutputStream();

                  try {
                      SerializableBlob sblob = (SerializableBlob) doc.getFdata(); // serialise doc data
                      Blob             blob  = sblob.getWrappedBlob();

                      // get stream from Blob
                      InputStream inStream = blob.getBinaryStream();
                      int         length   = -1;
                      byte[]      buffer   = new byte[4096];

                      // write data to output stream
                      while ((length = inStream.read(buffer)) != -1) {
                          baos.write(buffer, 0, length);
                      }

                      baos.flush();

                      MimetypeDao mDao = new MimetypeDao();
                      Mimetype    mt   = mDao.retrieveMimetypeById(doc.getMimetype_id()); // gets mime type on basis of file extention

                      response.setContentType(mt.getMimetype());
                      response.setContentLength(baos.size());

                      ServletOutputStream outStream = response.getOutputStream();

                      baos.writeTo(outStream);
                      baos.flush();
                      outStream.flush();
                      outStream.close();
                      inStream.close();
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }

                  fc.responseComplete();
              }
              return null;
          }

       

      <h:commandLink id="openlink" style="color: #ffffff;" target="#{document.fname}" immediate="false" action="#{ProductBean.actionOpenDocument}" >
      <h:graphicImage  alt="#{document.fname}" url="#{document.mimetype.icon}" />
      <f:param name="docid" value="#{document.id}" ></f:param>
      </h:commandLink>

       

      What I'm seeking, is suggestions for how to handle the 'problem' file types...

       

      Any help / suggestions greatly appreciated.