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

发布于 2024-11-27 04:22:32 字数 1526 浏览 1 评论 0原文

我正在服务器端生成 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文