在tomcat服务器上设置要下载的文件.

Dar*_*ava 6 java tomcat download docx

我正在生成一个.docx文件服务器端.我将它保存到tmp目录,如下所示:

docx.createDocx(System.getProperty("java.io.tmpdir") + "/example_title");
Run Code Online (Sandbox Code Playgroud)

(我可以确认这确实有效,文件存储在/ tmp/tomcat6-tmp /

我希望用户能够下载创建的文件.我尝试过以下方法:

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

但这不起作用.它将我引导至http:// localhost:8080/tmp/tomcat6-tmp/example_title.docx.这显然是错误的方法,但是如何在服务器上创建文件以供用户使用Tomcat下载?

谢谢Dara

编辑:知道了,对于任何有兴趣的人:

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());
}
Run Code Online (Sandbox Code Playgroud)