后台代码:
public String getFileName() {
String fileName = this.getRequest().getParameter("fileName");
String downloadFileName = fileName;
try {
} catch (Exception e) {
e.printStackTrace();
}
return downloadFileName;
}
// 下载的流
public InputStream getInputStream() {
String rootPath = CustomizedPropertyPlaceholderConfigurer
.getContextProperty(SystemConstants.ROOT_PATH).toString();
String name = this.getFileName();
String realPath = rootPath + name;
InputStream in = null;
try {
in = new FileInputStream(new File(realPath));
return in;
} catch (Exception e) {
e.printStackTrace();
System.out
.println("Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name=\"inputName\"> tag specified for this action.检查action中文件下载路径是否正确.");
return null;
}
}
public String download() {
return "download";
}
struts.xml 配置文件设置
<result name="download" type="stream">
<!-- 该参数负责下载文件后的(另存为),默认保存的文件名-->
<param name="contentDisposition">filename="${fileName}"</param>
<!--下载文件流 -->
<param name="inputName">inputStream</param>
<!--缓存 -->
<param name="bufferSize">4096</param>
</result>
Struts2 上传下载