0

我想问是否有人可以帮助我举个例子来使用 Struts 2 注释下载文件

我在我的动作课上试过这个

public class MyClass {
private InputStream       fileInputStream;

    private String      fileName;

    @Override
    @Action(AbstractBasisAction.VIEW)
    public String view() {
    System.out.println("HoursCycleDownloadFrameAction: view");
    super.view();
    return SUCCESS;
    }

    public InputStream getFileInputStream() {
    return fileInputStream;
    }

    @Action(value = "downloadFile", results = { @Result(name = "success", type = "stream", params = { "contentType", "application/octet-stream", "inputName", "fileInputStream", "contentDisposition", "filename=\"${fileName}\"", "bufferSize", "1024" }) })
    public String downloadFile() throws Exception {
    fileName = "license.txt";
    fileInputStream = new FileInputStream(new File("C:\\", fileName));
    return SUCCESS;
    }
}

这就是我的页面包含的内容

<s:url id="fileInputStream" namespace="/myClass" action="downloadFile" ></s:url>

Download file - <s:a href="%{fileInputStream}">lisence.txt</s:a>

但现在的问题是它下载了带有操作方法名称的文件。表示文件名为downloadFile.action。任何人都可以帮助我吗?

4

1 回答 1

0

1) 如果是 .txt 文件,"contentType", "application/octet-stream" 则应改为"contentType", "plain/text";

2)你的private String fileName;变量需要一个吸气剂;

3)"contentDisposition", "filename=\"${fileName}\""应该包含扩展,最终inline(默认,在浏览器中打开)或attachment(询问是否下载或使用客户端应用程序打开),例如

"contentDisposition", "attachment; filename=\"${fileName}.txt\""

于 2012-12-20T14:27:26.177 回答