我正在生成.docx文件服务器端。我将它保存到tmp目录,如下所示:
docx.createDocx(System.getProperty("java.io.tmpdir") + "/example_title");
(我可以确认这确实有效,文件存储在/ tmp / tomcat6-tmp /
我希望用户能够下载创建的文件。我尝试过以下方法:
out.println("<a href = '"+System.getProperty("java.io.tmpdir") + "/example_title.docx"+"'>Here ya go!</a>");
但这不起作用。它引导我http://localhost:8080/tmp/tomcat6-tmp/example_title.docx。这显然是错误的方法,但是如何在服务器上创建文件以供用户使用Tomcat下载?
谢谢你, 达拉
编辑:知道了,感兴趣的人:
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());
}