0

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

Jsp页面:

<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>

下载Action.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)

我已尽力解决此问题,但对我不起作用,我尝试了八位字节流,但仍然给出错误。真的很感激有人的帮助。如果还有其他选择,请给我完整的代码。基本上我想将文件 abc.java 从服务器下载到客户端机器。

谢谢

4

0 回答 0