标签:
jsp:
<body>
<a href="stream.action?fileName=psb.jpg">psb</a> <br>
</body>
action:
public class StreamAction {
private String fileName;
public String execute(){
return Action.SUCCESS;
}
public InputStream getInputStream() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getRealPath("/download");
return new FileInputStream(new File(path,fileName));
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
struts.xml
<action name="stream" class="com.sun.action.StreamAction">
<result type="stream">
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
Struts2下载流跟servlet下载流一样,唯一不同的地方是,struts2是通过一个输入流来写的,根据inputName获得输入流的名称 它会去找值栈里面的对应的action的一个方法,所以在action中提供一个输入流方法即可,它通过inputName去取,这个inputName的默认值是”inputStream”,那么就会去action中取这个inputStream的get方法,即getInputStream()
public class StreamResult extends StrutsResultSupport {
protected String inputName = "inputStream";//输入流默认的名称
if (inputStream == null) {
// Find the inputstream from the invocation variable stack
inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
}
因为action是在值栈中,所以会去值栈中取 get 方法
标签:
原文地址:http://www.cnblogs.com/austinspark-jessylu/p/5743140.html