struts2文件下载示例
?
package com.topdt.policesms.action;
?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
?
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
?
import com.topdt.frame.base.Struts2Action;
import com.topdt.frame.common.AppContext;
import com.topdt.policesms.model.DataLog;
import com.topdt.policesms.service.DataLogService;
?
/**
?* 数据日志
?* @author Administrator
?*
?*/
@Controller
@Scope("prototype")
@ParentPackage(value = "gloab-package")
@Namespace(value = "/message")
@Action(value = "dlog", results = {
@Result(name = "list", location = "/message/dlog/dlog_list.jsp"),
@Result(name = "download", type = "stream(类型为流)", params = { "contentType",
"application(应用程序)/octet-stream;charset=ISO8859-1", "inputName(输入文件名称)",
"fstream", "contentDisposition", "attachment;filename=${downloadFileName}",
"bufferSize", "4096" }),
@Result(name = "invalid.token",type="redirect",location = "task!list.action",params = {"message", "请不要重复提交" })?
},?
interceptorRefs = {
@InterceptorRef(value = "pageInterceptor", params = { "includeMethods", "list" }),
@InterceptorRef(value = "token", params = { "includeMethods", "save" }),
@InterceptorRef("topDtStack")?
})
public class DataLogAction extends Struts2Action<DataLog>{
?
private static final long serialVersionUID = 1792436660779570837L;
?
@Autowired
private DataLogService dataLogService;
private String statDate;
private String endDate;
private InputStream fstream;
private String inputPath;
private String fileName;
? ? public void setInputPath(String value) {
? ? ? ? inputPath = value;
? ? }
?
? ? public void setFileName(String fileName) {
this.fileName = fileName;
}
?
@Override
protected void doList() throws Exception {
list = dataLogService.queryDataLog(statDate, endDate, getModel().getDataType());
}
?
//取得文件名称
?
public String getDownloadFileName(){
? ? String downFileName=fileName;
? ? try {
downFileName=new String(downFileName.getBytes(),"ISO8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return downFileName;
? ? }
//下载文件
public String downLog () {
inputPath = AppContext.getProperties("breakDataDir")+fileName;
try {
fstream = new FileInputStream(new File(inputPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "download";
}
public DataLogService getDataLogService() {
return dataLogService;
}
?
public void setDataLogService(DataLogService dataLogService) {
this.dataLogService = dataLogService;
}
?
public String getStatDate() {
return statDate;
}
?
public void setStatDate(String statDate) {
this.statDate = statDate;
}
?
public String getEndDate() {
return endDate;
}
?
public void setEndDate(String endDate) {
this.endDate = endDate;
}
?
public InputStream getFstream() {
return fstream;
}
?
public void setFstream(InputStream fstream) {
this.fstream = fstream;
}
?
}
?
?
HTML :代码:<a href='dlog!downLog.action?fileName=<s:property value="#dlog.errorDetail"/>'>错误详细</a>