使用流读取文件信息

读文件的action配置

<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>

在跳转后得页面添加

<iframe src="<s:url value='/wxdate/fileLoad.action?resInfo.resId=%{resInfo.resId}'/>" style="width: 100%; height: 700px;border: 0"></iframe>

action代码

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;
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值