6

I'm generating a .docx file server-side. I have it saved to a tmp directory, as follows:

docx.createDocx(System.getProperty("java.io.tmpdir") + "/example_title");

(I can confirm that this indeed works, the file is stored in /tmp/tomcat6-tmp/

And I want the user to be able to download the created file. I have tried the following:

out.println("<a href = '"+System.getProperty("java.io.tmpdir") + "/example_title.docx"+"'>Here ya go!</a>");

But that does not work. It directs me to http://localhost:8080/tmp/tomcat6-tmp/example_title.docx . This is obviously the wrong way to do this, but how does one create files on the server for the user to download using Tomcat?

Thank you, Dara

EDIT: Got it, for anyone that's interested:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/msword");
    response.setHeader("Content-Disposition", "attachment; filename=\"consolidatedReport.docx\"");

    // Load the template.
    // Java 5 users will have to use RhinoFileTemplate instead
    CreateDocx docx = new CreateDocx("docx");

    String text = "Lorem ipsum dolor sit amet.";

    HashMap paramsTitle = new HashMap();
    paramsTitle.put("val", "1");
    paramsTitle.put("u", "single");
    paramsTitle.put("sz", "22");
    paramsTitle.put("font", "Blackadder ITC");

    docx.addTitle(text, paramsTitle);
    docx.createDocx(System.getProperty("java.io.tmpdir") + "/example_title");

    FileInputStream a = new FileInputStream(System.getProperty("java.io.tmpdir") + "/example_title.docx");
    while(a.available() > 0)
      response.getWriter().append((char)a.read());
}
Dara Java
  • 2,410
  • 3
  • 27
  • 52
  • 3
    Duplicate: http://stackoverflow.com/questions/4543936/load-the-image-from-outside-of-webcontext-in-jsf True, the question is different, but the answer is the same for you. By the way, instead of saving the file in the temp dir, you could also just write it **immediately** to the response of the request. – BalusC Jul 31 '11 at 02:32
  • Yeah, I took a look at that. I'd love to be able to just write it immediately to the response, but seeing as I only have the createDocx(String) method, is it even possible? – Dara Java Jul 31 '11 at 02:49
  • I see. Well, you could get an `InputStream` of the temp file within the same request and stream it to the `OutputStream` of the response instead of providing a download link? (and then if necessary delete the temp file) – BalusC Jul 31 '11 at 02:53
  • Cool, thanks @BalusC - I edited my question, am I close? – Dara Java Jul 31 '11 at 03:02

0 Answers0