从struts2中的服务器下载PDF

时间:2013-07-18 16:31:03

标签: java struts2 liferay liferay-6

我需要在点击链接时将pdf文件从服务器下载到客户端计算机。我正在使用liferay 6.1,tomacat,struts2。我在这里给出了完整的流程:

Jsp Page:

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

struts.xml中:

<action name="download" class="com.stp.portal.view.DownloadAction">
            <result name="success" type="stream">
                <param name="contentType">application/pdf</param>
                <param name="inputName">fileInputStream</param>
                <param name="contentDisposition">attachment;filename="abc.pdf"</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>

DownlaodAction.java:

public class DownloadAction extends ActionSupport{

    private InputStream fileInputStream;

    public InputStream getFileInputStream() {
        return fileInputStream;
    }

    public String execute() throws Exception {

        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File("D:\\abc.pdf");
                Desktop.getDesktop().open(myFile);
            } catch (IOException ex) {
                // no application registered for PDFs
            }
        }
        fileInputStream = new FileInputStream(new File("D:\\abc.pdf"));
        return SUCCESS;
    }
}

当我点击jsp中的downloadPDF时,会出现以下错误消息:

java.lang.IllegalArgumentException: application/pdf is not a supported mime type
    at com.liferay.portlet.MimeResponseImpl.setContentType(MimeResponseImpl.java:159)

我尽力解决这个问题,但是没有为我工作,我尝试过octet-stream,但仍然给出错误。非常感谢别人的帮助。如果还有其他选择,请给我完整的代码。基本上我想将文件abc.java从服务器下载到客户机。

由于

0 个答案:

没有答案