设置要在 tomcat 服务器上下载的文件。
我正在服务器端生成 .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>");
但这不起作用。 rel="noreferrer">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());
}
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论