struts多文件打包下载报错

Dong丶Xm 2014-10-20 10:21:26
<action name="getDownloadFile" method="getDownloadFile" class="com.njusc.wjsf.action.UnitAction">
<result name="success" type="stream">
<param name="contentType">${contentTypes}</param>
<param name="inputName">reportStreams</param>
<param name="contentDisposition">${fileNames}</param>
<param name="bufferSize">4096</param>
</result>
<result name="downAll" type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">application/octet-stream</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">inputStream</param>
<param name="contentDisposition">${fileNames}</param>
<!-- 下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [is] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:367)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:271)
com.njusc.system.interceptor.AuthorityInterceptor.intercept(AuthorityInterceptor.java:17)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:544)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)


问题:1、多文件下载时,文件已经下载,但还是报错
2、多文件下载时,没有提示打开或者保存
...全文
131 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dong丶Xm 2014-10-20
  • 打赏
  • 举报
回复
<action name="getDownloadFile" method="getDownloadFile" class="com.njusc.wjsf.action.UnitAction"> <result name="success" type="stream"> <param name="contentType">${contentTypes}</param> <param name="inputName">reportStreams</param> <param name="contentDisposition">${fileNames}</param> <param name="bufferSize">4096</param> </result> <result name="downAll" type="stream"> <!-- 下载文件类型定义 --> <param name="contentType">application/octet-stream</param> <!-- 下载文件输出流定义 --> <param name="inputName">inputStream</param> <param name="contentDisposition">${fileNames}</param> <!-- 下载文件的缓冲大小 --> <param name="bufferSize">4096</param> </result> </action>
Dong丶Xm 2014-10-20
  • 打赏
  • 举报
回复
public static void compressFiles2Zip(File[] files,String zipFilePath,InputStream is) { if(files != null && files.length >0) { if(isEndsWithZip(zipFilePath)) { ZipArchiveOutputStream zaos = null; try { File zipFile = new File(zipFilePath); zaos = new ZipArchiveOutputStream(zipFile); //Use Zip64 extensions for all entries where they are required zaos.setUseZip64(Zip64Mode.AsNeeded); //将每个文件用ZipArchiveEntry封装 //再用ZipArchiveOutputStream写到压缩文件中 for(File file : files) { if(file != null) { ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,file.getName()); zaos.putArchiveEntry(zipArchiveEntry); try { is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024 * 5]; int len = -1; while((len = is.read(buffer)) != -1) { //把缓冲区的字节写入到ZipArchiveEntry zaos.write(buffer, 0, len); } //Writes all necessary data for this entry. zaos.closeArchiveEntry(); }catch(Exception e) { throw new RuntimeException(e); }finally { if(is != null) { is.close(); } } } } zaos.finish(); }catch(Exception e){ throw new RuntimeException(e); }finally { try { if(zaos != null) { zaos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } }
Dong丶Xm 2014-10-20
  • 打赏
  • 举报
回复
public String getFwDownloadFile() throws UnsupportedEncodingException{ // ActionContext context = ActionContext.getContext(); // HttpServletRequest request = (HttpServletRequest)context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest"); FileModel fileModel = new FileModel(); List<Object[]> list = fileService.getFcById(fc.getId()); // HttpServletResponse response = ServletActionContext.getResponse(); String fileName; if(list.size()==0){ fileName = fileModel.getName(); // �ļ���Ĭ�ϱ����� ByteArrayInputStream inStream = new ByteArrayInputStream(fileModel.getFileByte()); // �������� // ��������ĸ�ʽ getResponse().reset(); getResponse().setCharacterEncoding("UTF-8"); getResponse().setContentType("application/octet-stream;"); // getResponse().addHeader("Content-Disposition", // "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8")); getResponse().addHeader( "Content-Disposition", new String(("attachment; name=" + fileName) .getBytes("UTF-8"), "UTF-8")); // getResponse().encodeURL(path); // ѭ��ȡ�����е���� byte[] b = new byte[100]; int len; try { OutputStream output=getResponse().getOutputStream(); while ((len = inStream.read(b)) > 0) output.write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } } //���ص��������� else if(list.size()==1){ // System.out.println("id:"+list.get(0)[0].toString()); // System.out.println("name:"+list.get(0)[1].toString()); // System.out.println("src:"+list.get(0)[2].toString()); fileModel.setId(list.get(0)[0].toString()); fileModel.setName(list.get(0)[1].toString()); fileModel.setSrc(list.get(0)[2].toString()); fileName = fileModel.getName(); // �ļ���Ĭ�ϱ����� System.out.println("fileName"+fileName); String fileBody =fileModel.getSrc(); System.out.println("fileBody"+fileBody); FileInputStream stream=null; try{ File file = new File(fileBody); stream=new FileInputStream(file); }catch (Exception e) { e.printStackTrace(); return "error"; } // �������� // ��������ĸ�ʽ getResponse().reset(); getResponse().setCharacterEncoding("UTF-8"); getResponse().setContentType("application/octet-stream;"); getResponse().setHeader("content-type","text/html;charset=UTF-8"); try { getResponse().setHeader("Content-Length", String.valueOf(stream.available())); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } getResponse().addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8")); // ѭ��ȡ�����е���� byte[] b = new byte[100]; int len=0; try { // PrintWriter output = getResponse().getWriter(); OutputStream output=getResponse().getOutputStream(); while ((len = stream.read(b))>0) output.write(b, 0, len); output.close(); stream.close(); } catch (IOException e) { e.printStackTrace(); } } //���ش��� else if(list.size()>1){ String[] fileurls = new String[list.size()]; for (int i = 0; i < list.size(); i++) { fileurls[i] = list.get(i)[2].toString(); } String formatDate =new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); fileName = "D:/test/" + formatDate + ".zip"; if(fileurls!=null){ if(fileurls.length>0){ File[] files = new File[fileurls.length]; for(int i=0;i<fileurls.length;i++) { if(fileurls[i] != null) { File file = new File(fileurls[i].trim()); if(file.exists()) { files[i] = file; } } } ZipFileUtil.compressFiles2Zip(files,fileName,is); } } return "downAll"; } return SUCCESS; }
EverWHL 2014-10-20
  • 打赏
  • 举报
回复
InputStream为null的缘故看StreamResult的源:

1.if (inputStream == null) {   
2.                // Find the inputstream from the invocation variable stack  
3.                inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));   
4.            }   
5.  
6.            if (inputStream == null) {   
7.                String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +   
8.                    "Check the <param name=\"inputName\"> tag specified for this action.");   
9.                LOG.error(msg);   
10.                throw new IllegalArgumentException(msg);   
11.            }  
路径错误,把下载路径打印出来看是否为空NULL

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧