【问题标题】:File is not downloading from server文件未从服务器下载
【发布时间】:2013-07-18 06:49:16
【问题描述】:

我想从服务器下载一个 pdf 文件。我正在使用tomcat。并在 struts2 中开发应用程序。

在我的jsp代码中,下载链接如下:

<td>
    <a href='<s:url action='downloadPdf'> </s:url>'>
        Download PDF</a>
</td>

我的 struts.xml 是:

<action name="downloadPdf" class="com.stp.portal.view.SearchServicePortlet" method="downloadPdf">
</action>

动作类是:

    public void downloadPdf() throws Exception
    {
        HttpServletResponse response = null;
        try
        {
            response.setContentType ("application/pdf");
            File f = new File ("D:\\abc.pdf");
            response.setHeader ("Content-Disposition", "attachment;filename=abc.pdf");
            InputStream inputStream = new FileInputStream(f);
            ServletOutputStream servletOutputStream = response.getOutputStream();
            int bit = 256;
            int i = 0;
            try 
            {
                while ((bit) >= 0) 
                {
                    bit = inputStream.read();
                    servletOutputStream.write(bit);
                }
                }
                catch (Exception ioe) 
                {
                    ioe.printStackTrace(System.out);
                }
                servletOutputStream.flush();
                inputStream.close();    
        }
        catch(Exception e)
        {

        }
    }

    public String generateGraph() throws Exception
    {
        return "success";
    }
}

我的问题是当我单击下载链接时,文件未下载。 abc.pdf文件在本地D盘里面。不知道哪里出了问题。如果有人可以帮助我,我将不胜感激。

提前致谢。

【问题讨论】:

  • 你试过调试吗?您能否验证文件是否正在从磁盘读取并确实发送回响应?
  • 此外,为了提高性能,使用byte[] 而不是一次读取/写入一个字节可能会很有趣。
  • 你应该在 finally 块中关闭你的输入流。如果由于某种原因出现 I/O 错误并且未达到 close(),则您会泄漏文件描述符
  • 我试过调试,但什么都没有,没有错误
  • 您使用的是 Java 7 还是 Java 6?

标签: java struts2


【解决方案1】:

尝试将您的代码更改为以下内容:

response.setContentType("application/force-download");
File f = new File ("D:\\abc.pdf");
response.addHeader("Content-Disposition", "attachment; filename=\"abc.pdf\"");
InputStream inputStream = new FileInputStream(f);
BufferedOutputStream out =
                new BufferedOutputStream(response.getOutputStream());
byte by[] = new byte[32768];
int index;
if (inputStream != null) {
    index = inputStream.read(by, 0, 32768);
} else {
    index = -1;
}
while (index != -1) {
    out.write(by, 0, index);
    index = inputStream.read(by, 0, 32768);
}
out.flush();

【讨论】:

  • 感谢 MaVRoSCy 的回复,但它对我有用,response.setContentType("application/force-download");在控制台中出现错误。
  • 你是什么意思getting errored 有例外吗?那条线应该不会造成任何问题
  • Never use that hack :@...Content-Disposition: attachment 足以确保文件被要求下载,或以用户配置浏览器设置的方式受到威胁(始终打开,始终下载等)
  • @user2594235 你用response.setContentType ("application/pdf"); 代替response.setContentType("application/force-download"); 试试我的代码吗?
  • @AndreaLigios 感谢您的评论,我会记住这一点
【解决方案2】:

更改为此代码。注意输入流的处理:在发生 I/O 错误时正确关闭:

final byte[] buf = new byte[16384];
int count;

final InputStream in = new FileInputStream(...);
final OutputStream out = servlet.getOutputStream();

try {
    while ((count = in.read(buf)) != -1)
        out.write(buf, 0, count);
    out.flush();
} catch (...) {
} finally {
    in.close();
}

如果您负担得起,请使用 Guava 及其 Closer 来处理 I/O 资源。

【讨论】:

    【解决方案3】:

    Liferay 有一个 sample-struts portlet,其中包含一个下载的自定义类型,该类型未嵌入到完整的门户页面中 - 例如。内容类型可以不同于通常门户的 HTML。在this example 中是图像/jpeg。根据你的问题应用这个。

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 1970-01-01
      • 2013-08-30
      • 2017-07-06
      • 2019-03-26
      • 2015-04-04
      相关资源
      最近更新 更多