在跳转后得页面添加<action name="fileLoad" class="com.ccxe.wxdate.action.FileUpAndDownLoad">
<result name="success" type="stream">
<!-- 指定下载文件的内容类型,text/plain是默认类型 -->
<param name="contentType">${contentType}</param>
<!--
inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数
-->
<param name="inputName">inputStream</param>
<!--动态获取文件名,从Action中的取得filename-->
<param name="contentDisposition">${loadFileName}</param>
<param name="bufferSize">2048</param>
</result>
<result name="input">/error.jsp</result>
</action>
action代码<iframe src="<s:url value='/wxdate/fileLoad.action?resInfo.resId=%{resInfo.resId}'/>" style="width: 100%; height: 700px;border: 0"></iframe>
public class FileUpAndDownLoad extends BaseAction {
Logger log = Logger.getLogger(FileUpAndDownLoad.class);
/**
*
*/
private static final long serialVersionUID = 2451501278444274004L;
private String downLoadFilePath;
private IResInfoService resInfoService;
private IAnnMainService annMainService;
private WxConfig wxConfig;
private ResInfo resInfo;
private AnnMain annMain;
private String attSeqNum;
private String loadFileName;
private String contentType;
private InputStream inputStream;
public String getDownLoadFilePath() {
return downLoadFilePath;
}
public void setDownLoadFilePath(String downLoadFilePath) {
this.downLoadFilePath = downLoadFilePath;
}
public IResInfoService getResInfoService() {
return resInfoService;
}
public void setResInfoService(IResInfoService resInfoService) {
this.resInfoService = resInfoService;
}
public IAnnMainService getAnnMainService() {
return annMainService;
}
public void setAnnMainService(IAnnMainService annMainService) {
this.annMainService = annMainService;
}
public WxConfig getWxConfig() {
return wxConfig;
}
public void setWxConfig(WxConfig wxConfig) {
this.wxConfig = wxConfig;
}
@Override
public String execute() throws Exception {
String path = null;
if (resInfo != null && resInfo.getResId() != null) {
resInfo = resInfoService.fetchResInfo(resInfo);
if (resInfo != null) {
path = resInfo.getResFilePath();
}
} else if (annMain != null && annMain.getAnnId() != null) {
annMain = annMainService.fetchAnnMain(annMain);
if (annMain != null) {
path = annMain.getFilePath();
}
}
if (path != null) {
path = path.replace("affix", "2011");
path = path.replace("newsDownload", "news");
path = wxConfig.getDisk() + path;
String attPath = fileAbsoluteToRelativeDir(path); //替换附件路径
setDownLoadFilePath(attPath);
int index = attPath.lastIndexOf("/", attPath.length()) + 1;
attPath = attPath.substring(index, attPath.length());
checkFormat(attPath);
inputStream = new FileInputStream(getDownLoadFilePath());
return SUCCESS;
} else {
return ERROR;
}
}
public InputStream getInputStream() throws Exception {
inputStream = new FileInputStream(getDownLoadFilePath());
return inputStream;
}
public String fileAbsoluteToRelativeDir(String path) {
return path;
}
/**
* 检查格式,文件后缀
* @param fileName
*/
private void checkFormat(String fileName) {
String[] splitString = fileName.split("\\.");
int length = splitString.length;
String format = "";
if (length > 1) {
format = splitString[length - 1];
}
if (format.equalsIgnoreCase("xls")) {
// contentType设定
contentType = "application/vnd.ms-excel;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else if (format.equalsIgnoreCase("xlsx")) {
// contentType设定
contentType = "application/vnd.ms-excel;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else if (format.equalsIgnoreCase("pdf")) {
// contentType设定
contentType = "application/pdf;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else if (format.equalsIgnoreCase("doc")) {
// contentType设定
contentType = "application/msword;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else if (format.equalsIgnoreCase("docx")) {
// contentType设定
contentType = "application/msword;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else if (format.equalsIgnoreCase("txt")) {
// contentType设定
contentType = "text/plain;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
} else {
// contentType设定
contentType = "text/plain;charset=utf-8";
// attachment表示网页会出现保存、打开对话框
loadFileName = "inline; filename=" + fileName;
}
}
public ResInfo getResInfo() {
return resInfo;
}
public void setResInfo(ResInfo resInfo) {
this.resInfo = resInfo;
}
public AnnMain getAnnMain() {
return annMain;
}
public void setAnnMain(AnnMain annMain) {
this.annMain = annMain;
}
public String getAttSeqNum() {
return attSeqNum;
}
public void setAttSeqNum(String attSeqNum) {
this.attSeqNum = attSeqNum;
}
public String getLoadFileName() {
return loadFileName;
}
public void setLoadFileName(String loadFileName) {
this.loadFileName = loadFileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}