structs2 json java jsp 下载单个文件

来源:互联网 发布:金融网络销售做不得 编辑:程序博客网 时间:2024/06/05 08:07

1、代码

private OutputStream res;
public OutputStream getRes() {
return res;
}


public void setRes(OutputStream res) {
this.res = res;
}


public String downLoadPostCheckResult() throws IOException 
{
HttpServletResponse response = ServletActionContext.getResponse(); 
res = response.getOutputStream(); 
response.reset();  
response.setHeader("Content-Disposition",
"attachment;filename=PostCheckResult.xls");
response.setContentType("application/x-xls");
//zos = new ZipOutputStream(res);
 
String activeChangePath = common.PropertiesOper.get("changeFile")+"PostCheckResult.xls"; 
File file = new File(activeChangePath);

InputStream is = new FileInputStream(file);
byte[] buf = new byte[8192];
int len;
//ZipEntry ze = new ZipEntry(file.getName());   
//zos.putNextEntry(ze);
BufferedInputStream bis = null;
if (is != null) 
{
bis = new BufferedInputStream(is);
while ((len = bis.read(buf)) > 0) 

res.write(buf, 0, len);
}
}
is.close(); 
bis.close(); 
res.close();  
msg = new BaseActionMsg("1","下载完成");  
return "success";
}


2、配置

<struts> 
 
<package name="ICBC" namespace="/ICBC" extends="struts-default,json-default"> 


<action name="downLoadPostCheckResult"  method="downLoadPostCheckResult"  class="IcbcAction">
     <result type="json" name="success">
      <param name="root">msg</param>  
     </result> 
     <result type="json" name="error">
      <param name="root">msg</param> 
     </result>
</action>

</package>
</struts>


0 0
原创粉丝点击