您的位置:首页 > 编程语言 > Java开发

工作记录:文章详情页的附件下载,基于struts2的文件下载

2014-10-07 00:00 531 查看
摘要: 文章详情页的附件下载,基于struts2的文件下载

系统架构是 struts2 ibatis spring,使用jquery easy ui 进行数据的展示等。

本例是 此系统下的文件下载;

1. 首先jsp页面上遍历出要下载的文件:(存储在request域中的List<Map>)
<c:forEach var="dList" items="${atttachmentLsit}" varStatus="sta" >
?<a href="###" onclick="toDownLoad('${dList.VCHR_ID}');">  ${fn:substring(dList.VCHR_ORIGINNAME, 0, 20)}<c:if test="${fn:length(dList.VCHR_ORIGINNAME)>20}">...</c:if></a> <br>
</c:forEach>
2. 点击文件,触发toDownLoad(...)方法:此js方法具体为;
function toDownLoad(vchr_id){//附件的主键id
url="questionQueryAction!toDownLoad.dhtml?vchr_id="+vchr_id;
window.location.href=url;
}
3. action中的toDownLoad()以及其他需要的内容如下:
private String inputPath = "";// 下载文件路径+名称
private String filename = "";// 所要下载文件的名称
/**
* 和配置文件对应,必须加
* @return
* @throws Exception
*/
public InputStream getTargetFile() throws Exception {
return new java.io.FileInputStream(inputPath);
}
public String getInputPath() {
return inputPath;
}

public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}

/**
* 转码,必须加
* @return
*/
public String getFilename() {
try {
filename = new String(filename.getBytes("GBK"), "ISO8859-1");
//filename = new String(filename.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}
/**
* 执行文件下载.9/29 mamy
*/
public String toDownLoad(){
String vchr_id = super.getRequestParameter("vchr_id");
Map map = (Map)dealSimpleService.getObject("questionQt.queryOneAttachment", vchr_id);
String fileName = map.get("VCHR_ORIGINNAME").toString();
String fileNameString = map.get("VCHR_STORAGENAME").toString();
String filePath = ResourceMessage.newInstance().getValue("uploadfile.zmhd");
this.setFilename(fileName);
this.setInputPath(filePath + File.separator+"zmhd" + File.separator + fileNameString);
return "download";
}
4. download在struts。xml中的配置为:
<result name="download" type="stream" >
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">4096</param>
</result>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息