0

Seam application using tomcat and postgresql 9.0.3
App allows users to download word documents that are saved in the DB as BLOB and defined in the bean as a byte array.

These word documents have been uploaded by users in xml format.

When I use this code with Tomcat(5) in Windows, it works just fine. When used with Tomcat (v. 6.0.24) on a Linux server, I get the behavior that the contents are displayed as HEX code in the Downloaded file???

At first I thought that the server was missing some fonts, files with Fonts that are definitely on the server show the same behavior. Even TXT files do the same thing.

The code for doing the download is as follows:

byte[] wordDoc = createApptLetter();
        HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setContentType("application/msword");
        fileName  = letter.getFileName();

        response.addHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");

        response.setContentLength(wordDoc.length);
        OutputStream os = response.getOutputStream();
        //log.error(response.getContentType());
        os.write(wordDoc);
        os.flush();
        os.close();
        facesContext.renderResponse();
        facesContext.responseComplete();

edit: here is createApptLetter method:

private byte[] createApptLetter() {
    LetterTemplate template = appointmentHome.getInstance().getLetterTemplate();
    Client client = appointmentHome.getInstance().getClientRegistration().getEligibilityCycle().getClient();
    String sageClinic = appointmentHome.getInstance().getFacility().getName();
    String apptDate = appointmentHome.getInstance().getAppointmentDateString();
    String apptTime = appointmentHome.getInstance().getAppointmentTimeString();
    String notes = appointmentHome.getInstance().getAppointmentNotes();
    String longDateFormat = SageConstantsEnum.DateFormats.LONGDATEFORMAT.getDescription();

    SimpleDateFormat df = new SimpleDateFormat(longDateFormat);
    String today = df.format(new Date());
    //StringBuilder sbTemplate = new StringBuilder(new String(template.getLetterTemplateText()));
    String xmlTemplate = new String (template.getLetterTemplateText());

    xmlTemplate = xmlTemplate.replace("letterDate", today);
    xmlTemplate = xmlTemplate.replace("apptDate", apptDate);
    xmlTemplate = xmlTemplate.replace("apptTime", apptTime);
    xmlTemplate = xmlTemplate.replace("apptNotes", notes);
    xmlTemplate = xmlTemplate.replace("sageClinic", sageClinic);

    xmlTemplate = xmlTemplate.replace("clientName", client.getFullname());
    xmlTemplate = xmlTemplate.replace("clientCity",client.getMailingAddress().getAddress().getCity());
    xmlTemplate = xmlTemplate.replace("clientAddress", client.getMailingAddress().getAddress().getStreetaddress());
    xmlTemplate = xmlTemplate.replace("clientState",client.getMailingAddress().getAddress().getState());
    xmlTemplate = xmlTemplate.replace("clientZip", client.getMailingAddress().getAddress().getZipcode());
    return xmlTemplate.getBytes();
}
mcgyver5
  • 1,153
  • 2
  • 15
  • 29

1 Answers1

1

Change the content type of your response to "application/octet-stream". This works for any kind of file. There is an example with JSF. In my example, I'm using a a4j:htmlCommandLink from RichFaces, since you're using Seam i guess you won't have any problem.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks for your reply. changed it to application/octet-stream and it is still coming in hex – mcgyver5 Feb 17 '12 at 20:20
  • @mcgyver5 what does your `createApptLetter` function do? Can you show the full code of your download function? – Luiggi Mendoza Feb 17 '12 at 20:43
  • ok. edited question to include createApptLetter method. as you can see, it is a simple string replace. And, like I said, this all works fine with tomcat running on windows. I'm going with a theory that it is a jdbc driver issue. – mcgyver5 Feb 17 '12 at 22:02
  • @mcgyver5 I don't get why are you converting a String to a byte array. Are you sure you are getting the data from DB as a BLOB? – Luiggi Mendoza Feb 18 '12 at 00:07
  • Look at the API for java.io.OutputStream. The write method requires a byte array. – mcgyver5 Feb 18 '12 at 15:33
  • @mcgyver5 I know that requires a byte array, but you are creating a byte array from a String, you must get the byte array from the file in your database you want to download. – Luiggi Mendoza Feb 18 '12 at 17:09
  • and what if I want to manipulate the contents of the file before the download? – mcgyver5 Feb 18 '12 at 17:36
  • @mcgyver5 you can do it if you know what you're changing in it, but you must save a temporal file with your changes and then download it, what you're doing is to parse a String to a byte array. – Luiggi Mendoza Feb 18 '12 at 17:46
  • Now that I am getting the byte array directly from my file in the database, it still comes down as five pages of hex garbage when I download it from Linux. Why does my code work on Tomcat in Windows and not in Linux and why does it still not work on linux when I, as you say, "get the byte array from the file in my database" – mcgyver5 Feb 27 '12 at 20:57
  • @mcgyver5 have you checked that the files have the same data when you upload them on Windows and Linux server? I have tested the same code in Windows (development environment) and Linux (testing/production environment) and it works fine in both. – Luiggi Mendoza Feb 27 '12 at 22:11
  • same data. it is pointing to the same db. You're saying you tested the code that I posted and it worked? That is interesting. – mcgyver5 Feb 27 '12 at 22:18
  • @mcgyver5 I tested my code to download files. If you can post or send me your code I could give a better solution. – Luiggi Mendoza Feb 27 '12 at 22:53