1 下载链接
<s:a href="download.action?fileName=pdf.txt">txt</s:a>
<s:a href="download.action?fileName=pdf.png">png</s:a>
<s:a href="download.action?fileName=pdf.pdf">pdf</s:a>
<s:a href="download.action?fileName=X9Demo.zip">zip</s:a>
<s:a href="download.action?fileName=axis2.war">war</s:a>
<s:a href="download.action?fileName=axiom-api-1.2.7.jar">axiom-api-1.2.7.jar</s:a>
<s:a href="download.action?fileName=Wildlife.wmv">Wildlife.wmv</s:a>
2 下载action
package com.cyb.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;
/**
* 所下载文件相关的的几个属性:文件格式 contentType,
* 获取文件名的方法inputName,
* 文件内容(显示的)属性contentDisposition,
* 限定下载文件 缓冲区的值bufferSize
* */
@Namespace("")
@Results({ @Result(name = "success",
type = "stream",
params = { "contentType",
"application/octet-stream;charset=utf-8", "inputName",
"inputStream", "contentDisposition",
"attachment;filename=\"${fileName}\"", "bufferSize", "4096" }) })
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 8784555891643520648L;
private String STORAGEPATH = "\\upload\\pdf.txt";
private String fileName ;// 初始的通过param指定的文件名属性
private String storageId;
private String inputPath;// 指定要被下载的文件路径
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.STORAGEPATH = "\\upload\\"+fileName;
this.fileName = fileName;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public String getStorageId() {
return storageId;
}
public void setStorageId(String storageId) {
this.storageId = storageId;
}
// 如果下载文件名为中文,进行字符编码转换
public String getDownloadFileName() {
String downloadFileName = fileName;
try {
downloadFileName = new String(downloadFileName.getBytes(),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public InputStream getInputStream() throws Exception {
/**
* 下载用的Action应该返回一个InputStream实例,
* 该方法对应在result里的inputName属性值为targetFile
**/
String realPath=ServletActionContext.getServletContext().getRealPath("\\upload")+ "\\"+fileName;
File file = new File(realPath);
if(!file.exists()){
System.out.println("文件不存在!");
}else{
System.out.println("文件存在!");
}
System.out.println(realPath);
InputStream is = ServletActionContext.getServletContext().getResourceAsStream(
STORAGEPATH);
return is;
}
public String download() {
System.out.println("sdfsdfsdfsdf");
//this.getInputStream();
return SUCCESS;
}
}
3 web.xml的配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.cyb.action</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>