import org.apache.struts2.config.Result; import org.apache.struts2.config.Results; import org.apache.struts2.dispatcher.StreamResult; @Results({ @Result(name = FileDownloadAction.RESULT_EXCEL, value = "stream", type = StreamResult.class, params = { "contentType", "application/vnd.ms-excel", "inputName", "stream", "contentDisposition", "attachment;filename=\"file2.xls\"", "bufferSize", "1024" }) }) public class FileDownloadAction { public static final String RESULT_EXCEL = "excel"; private InputStream stream; public String execute() throws Exception { stream = new FileInputStream("c:\\temp\\file.xls"); return RESULT_EXCEL; } //... getter and setter }
这里最重要的是Result里面params的写法,params后面的大括号中,奇数个字符串是key值,偶数个字符串是value值。
其中,inputName的值-stream要和Class中的InputStream的属性名一致,而Result中的value的值也要与之一致。