Jakarta ee 使用html锚标记在struts2中下载文件

Jakarta ee 使用html锚标记在struts2中下载文件,jakarta-ee,struts2,download,Jakarta Ee,Struts2,Download,在普通HTML标记中,可以使用锚标记生成下载链接,如: <a href="www.example.com/somefile.zip">DOWNLOAD</a> 在struts2中,我将文件名和文件URL存储在数据库中。 然后,在JSP文件中填充下载链接,如: <s:iterator value="fileList"> <a href='<s:property value="fileURL"/>'> <s:property v

在普通HTML标记中,可以使用锚标记生成下载链接,如:

<a href="www.example.com/somefile.zip">DOWNLOAD</a>

在struts2中,我将文件名和文件URL存储在数据库中。 然后,在JSP文件中填充下载链接,如:

<s:iterator value="fileList">
<a href='<s:property value="fileURL"/>'> <s:property value="fileName"/> </a>
</s:iterator>

通过这种方式,将填充文件名及其链接。当我将鼠标悬停在链接上时,我可以在浏览器的状态栏中看到正确的文件URL。但是,当我单击链接时,现在会显示“下载”对话框。我搜索了互联网,他们告诉我使用FileInputStream。
我的问题是,是否可以像上面的代码那样生成下载链接,而不是使用FileInputStream?

使用Struts2,您有
操作
s和
结果
s

因此,您需要一个映射到您的链接的
操作
,我们称之为
下载\u文件

创建链接列表,传入一个参数,告诉struts2要下载哪个文件(允许任意文件是危险的,所以可能需要一个文件名)

还有一个MIME类型的getter,比如

public String getContentType() {
    return "text/plain";
}
显然,将MIME设置为正确的类型

因此,您的基本
操作将如下所示

public class MyAction extends ActionSupport {

    private final File baseDownloadDir = new File("somewhere");
    private String fileName;
    private InputStream inputStream;
    private long fileSize;

    @Override
    public String execute() throws Exception {
        /*
         *This is a security hole begging to be exploited.
         *A user can submit "../../../../someImportantFile"
         *and potentially download arbitrary files from the server.
         *You really need to do some validation on the input!
         */ 
        final File fileToDownload = new File(baseDownloadDir, fileName);
        fileSize = fileToDownload.length();
        inputStream = new FileInputStream(fileToDownload);
        return "downloadFile";
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getFileSize() {
        return fileSize;
    }

    public InputStream getFileToDownload() {
        return inputStream;
    }

    public String getContentDisposition() {
        return "attachment;filename=\"" + fileName + "\"";
    }

    public String getContentType() {
        return "text/plain";
    }
}
然后返回一个结果名,让我们调用它
downloadFile

在动作映射中,您需要将该结果映射到
StreamResult
,下面是一个XML示例

<result name="downloadFile" type="stream">
    <param name="inputName">fileToDownload</param>
    <param name="contentType">${contentType}</param>
    <param name="contentLength">${fileSize}</param>
    <param name="contentDisposition">${contentDisposition}</param>
    <param name="contentCharSet">UTF-8</param>
    <param name="allowCaching">true</param>
</result>

文件下载
${contentType}
${fileSize}
${contentDisposition}
UTF-8
真的

您可能需要更改字符集。

使用Struts2您有
操作
s和
结果
s

因此,您需要一个映射到您的链接的
操作
,我们称之为
下载\u文件

创建链接列表,传入一个参数,告诉struts2要下载哪个文件(允许任意文件是危险的,所以可能需要一个文件名)

还有一个MIME类型的getter,比如

public String getContentType() {
    return "text/plain";
}
显然,将MIME设置为正确的类型

因此,您的基本
操作将如下所示

public class MyAction extends ActionSupport {

    private final File baseDownloadDir = new File("somewhere");
    private String fileName;
    private InputStream inputStream;
    private long fileSize;

    @Override
    public String execute() throws Exception {
        /*
         *This is a security hole begging to be exploited.
         *A user can submit "../../../../someImportantFile"
         *and potentially download arbitrary files from the server.
         *You really need to do some validation on the input!
         */ 
        final File fileToDownload = new File(baseDownloadDir, fileName);
        fileSize = fileToDownload.length();
        inputStream = new FileInputStream(fileToDownload);
        return "downloadFile";
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getFileSize() {
        return fileSize;
    }

    public InputStream getFileToDownload() {
        return inputStream;
    }

    public String getContentDisposition() {
        return "attachment;filename=\"" + fileName + "\"";
    }

    public String getContentType() {
        return "text/plain";
    }
}
然后返回一个结果名,让我们调用它
downloadFile

在动作映射中,您需要将该结果映射到
StreamResult
,下面是一个XML示例

<result name="downloadFile" type="stream">
    <param name="inputName">fileToDownload</param>
    <param name="contentType">${contentType}</param>
    <param name="contentLength">${fileSize}</param>
    <param name="contentDisposition">${contentDisposition}</param>
    <param name="contentCharSet">UTF-8</param>
    <param name="allowCaching">true</param>
</result>

文件下载
${contentType}
${fileSize}
${contentDisposition}
UTF-8
真的

您可能需要更改字符集。

非常感谢您抽出时间。请允许我再问一个问题。在struts.xml中,“inputName”和“contentDisposition”参数是否足以下载文件?是否需要其他参数?唯一需要的参数是
inputName
-请参阅。其他浏览器只是让浏览器按照预期运行。非常感谢您抽出时间。请允许我再问一个问题。在struts.xml中,“inputName”和“contentDisposition”参数是否足以下载文件?是否需要其他参数?唯一需要的参数是
inputName
-请参阅。其他浏览器只是让浏览器按照预期的方式运行。