public class FileDownloadAction implements Action {
private String inputPath;
public void setInputPath(String value) {
inputPath = value;
}
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String execute() throws Exception {
return SUCCESS;
}
}
<action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
<param name="inputPath">/images/struts.gif</param> //这个是和action中的属性对应
<result name="success" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">inputStream</param> //和getInputStream()对应
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
</action>
参数
说明
contentType
内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片
inputName
下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法
contentDisposition
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:
attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"
bufferSize
下载缓冲区的大小
(1)中文文件名会出现乱码??
downFileName=new String(fileName.getBytes(), "ISO8859-1");
-------------文件下载,绝对路径,相对路径都行-------我修改的代码如下:(可通用)
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action {
private String inputPath;
private String fileName;
public void setInputPath(String value) {
inputPath = value;
}
public void setFileName(String fileName) {
this.fileName = fileName;
System.out.println(fileName);
}
public InputStream getInputStream() throws Exception {
char c=inputPath.charAt(1);
System.out.println((c==':'));
if(!exists(c)){
return null;
}
if(c==':'){
return new java.io.FileInputStream(inputPath);//从系统磁盘文件读取数据
}
else{
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);//从项目webRoot下的目录下
}
}
public String getDownloadFileName(){
String downFileName = null;
if(fileName==null){ //fileName可以不设初始值,默认原始文件名,分隔符为/
int start=inputPath.lastIndexOf("/");
fileName=inputPath.substring(start+1);
}
try {
downFileName=new String(fileName.getBytes(), "ISO8859-1"); //对中文的处理
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
private boolean exists(char c){
File file=null;
if(c==':'){
file=new File(inputPath);
}
else{
String realPath=ServletActionContext.getServletContext().getRealPath("/");
file=new File(realPath+inputPath);
}
//文件不存在
return file.exists();
}
public String execute() throws Exception {
return SUCCESS;
}
}
<action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
<!--
<param name="inputPath">images/struts.gif</param>
<result name="success" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
-->
<param name="inputPath">c:/test二.doc</param>
<param name="fileName"></param>
<result type="stream">
<param name="contentType">application/msword;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() -->
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
<action name="download2" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
<param name="inputPath">/images/struts-gif.zip</param>
<!-- 相对webRoot目录下 -->
<result name="success" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts-gif.zip"</param>
<param name="bufferSize">4096</param>
</result>
</action>
--------------------------------------