文件未从服务器下载 [英] File is not downloading from server

查看:48
本文介绍了文件未从服务器下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to download a pdf file from server. I am using tomcat. and developing the application in struts2.

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

In my jsp code the link for download is as below:

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

我的struts.xml是:

My struts.xml is:

<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内.不知道出了什么问题.如果有人可以帮助我,我将不胜感激.

My problem is when i click on the download link the file is not downloaded. abc.pdf file is inside the local disk D. Dont know what is wrong. Would really appreciate if someone could help me on this.

谢谢.

推荐答案

有一个 sample-struts portlet ,其中包含下载的自定义类型,该类型未嵌入完整的门户页面中-例如内容类型可以与普通门户网站的HTML不同.在

There's a sample-struts portlet for Liferay which contains a downloaded custom type that is not embedded in the full portal page - e.g. the content type can be something different than the usual portal's HTML. In this example it's image/jpeg. Apply this according to your problem.

这篇关于文件未从服务器下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆