action类
ioc
private String contentType;
private String contentDisposition;
private String inputPath;
public void setInputPath(String value) {
inputPath = value;
}
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String getContentDisposition() {
return contentDisposition;
}
public String getContentType() {
return contentType;
}
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* 下载
* @return
* @throws BusinessException
*/
public String downloadFile() throws BusinessException
{
Accessories a=this.getLoginAndSystemManager().getAccessoriesByAccessoriesId(fileId);
inputPath ="/"+a.getPath();
contentType=a.getFile_type();
try {
contentDisposition= new String(a.getFile_name().getBytes(), "UTF-8");
contentDisposition=java.net.URLEncoder.encode(contentDisposition, "UTF-8"); //这句很重要,不然文件名为乱码或为action的名称
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return this.SUCCESS;
}
xml配置:
<action name="downloadFile" class="ewing.business.basic.action.LoginAndSystemAction" method="downloadFile">
<!--<result name="success" type="chain">downloadFile1</result>-->
<param name="inputPath">{inputPath}</param>
<result name="success" type="stream">
<param name="contentType">{contentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${contentDisposition}"</param>
<param name="bufferSize">4096</param>
</result>
</action>