问题描述
在编写文件下载的过程中,点击下载,然后出现这个错误:Cannotfindajava.io.InputStreamwiththename[renderResult]intheinvocationstack.Checkthe<paramname="inputName">tagspecifiedforthisaction.异常trace如下:java.lang.IllegalArgumentException:Cannotfindajava.io.InputStreamwiththename[renderResult]intheinvocationstack.Checkthe<paramname="inputName">tagspecifiedforthisaction.org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)源代码如下:packageorg.xjtu.framework.modules.normalUser.action;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.util.Map;importjava.util.zip.ZipEntry;importjava.util.zip.ZipOutputStream;importorg.apache.struts2.convention.annotation.Action;importorg.apache.struts2.convention.annotation.Namespace;importorg.apache.struts2.convention.annotation.ParentPackage;importorg.apache.struts2.convention.annotation.Result;importorg.xjtu.framework.core.base.model.User;@ParentPackage("struts-default")@Namespace("/web/user")publicclassDownloadActionextendsUserBaseAction{privatestaticfinalintBUFFEREDSIZE=1024;ByteArrayInputStreamrenderResult;StringdownloadFileName="";privateUseruser;@Action(value="downloadRenderResult",results={@Result(name=ERROR,type="httpheader",params={"status","204"}),@Result(name=SUCCESS,type="stream",params={"contentType","application/zip","inputName","renderResult","contentDisposition","attachment;filename=${downloadFileName}.zip","bufferSize","1024"})})publicStringdownloadRenderResult()throwsException{user=(User)session.get("user");System.out.println(user.getName());ByteArrayOutputStreamoutput=newByteArrayOutputStream();ZipOutputStreamzos=newZipOutputStream(output);Stringpath="/home/RenderFarm/apache-tomcat-6.0.37/logs/";log.info(path);//user.getHomeDir()+"building/Pictures";Filefile=newFile(path);this.downloadFileName="result";zip(file,zos,"result");//清空缓冲区数据,这一步务必先执行zos.flush();zos.close();byte[]ba=output.toByteArray();renderResult=newByteArrayInputStream(ba);System.out.println(renderResult);log.info(path);if(renderResult==null){log.info("renderResult是空的");}else{log.info("renderResult不是空的");}output.flush();output.close();returnSUCCESS;}privatesynchronizedvoidzip(FileinputFile,ZipOutputStreamout,Stringbase)throwsIOException{if(inputFile.isDirectory()){File[]inputFiles=inputFile.listFiles();out.putNextEntry(newZipEntry(base+"/"));base=base.length()==0?"":base+"/";for(inti=0;i<inputFiles.length;i++){zip(inputFiles[i],out,base+inputFiles[i].getName());}}else{if(base.length()>0){out.putNextEntry(newZipEntry(base));}else{out.putNextEntry(newZipEntry(inputFile.getName()));}FileInputStreamin=newFileInputStream(inputFile);try{intc;byte[]by=newbyte[BUFFEREDSIZE];while((c=in.read(by))!=-1){out.write(by,0,c);}}catch(IOExceptione){throwe;}finally{in.close();}}}}最后查看日志得到如下信息,显示路径不为空,且存在无误,并且renderResult不为空。11:29:42,362-org.xjtu.framework.modules.normalUser.action.DownloadAction-32100[http-8080-1]INFOorg.xjtu.framework.modules.normalUser.action.DownloadAction-/home/RenderFarm/apache-tomcat-6.0.37/logs/org.xjtu.framework.modules.normalUser.action.DownloadAction-32476[http-8080-1]INFOorg.xjtu.framework.modules.normalUser.action.DownloadAction-[color=#FF0000]renderResult不是空的[/color][framework]2014-09-3011:29:42,762-org.apache.struts2.dispatcher.StreamResult-32500[http-8080-1]ERRORorg.apache.struts2.dispatcher.StreamResult-Cannotfindajava.io.InputStreamwiththename[renderResult]intheinvocationstack.Checkthe<paramname="inputName">tagspecifiedforthisaction求各位大神解决啊。。。log.info(path);if(renderResult==null){log.info("renderResult是空的");}else{log.info("renderResult不是空的");}