0 Replies Latest reply: Mar 3, 2008 7:57 AM by 843844 RSS

    Displaying Files Inline

    843844
      hi all,

      i'm trying to get the browser (IE7) to display MS word file saved in MySQL database, but wether i display the file or save it, it appears in a strange encoding.

      here's the code i used to save the file to the databse:

      //myFile is an UploadedFile instance
      String fileName = myFile.getName();
      String contentType = myFile.getContentType();
      Long fileSize = myFile.getSize();
      int fileSizeInt = fileSize.intValue();
      byte[] byteFile = new byte[fileSizeInt];
      byteFile = myFile.getBytes();
      String stringFile = new String(byteFile,"UTF-8");
      //the stringFile is saved in the database in a MeduimBlob field

      and this is the code i use for displaying the file inline:

      HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();

      byte[] byteFile = myFile.getBytes();
      String fileName = myFile.getName();
      String contentType = myFile.getContentType();

      InputStream input = new ByteArrayInputStream(byteFile)
      int contentLength = input.available();

      response.reset();
      response.setContentLength(contentLength);
      response.setContentType(contentType);
      response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");
      response.setHeader("Pragma", "public");
      response.setHeader("Cache-control", "must-revalidate");

      BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
      while (contentLength-- > 0) {
      output.write(input.read());
      }
      output.flush();

      context.responseComplete();

      i dont know if it's an encoding or a conversion problem ..

      i'll appreciate any suggestion!!