通过Struts2文件下载时Can not find a java.io.InputStream with the name 异常

2023-10-28

异常堆栈信息:

引用:
严重: Can not find a java.io.InputStream with the name [photoStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
2008-4-17 17:04:44 org. apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [photoStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
        at org.apache. struts2" οnclick="tagshow(event)" class="t_tag"> struts2.dispatcher.StreamResult.doExecute(StreamResult.java:189)
        at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
        at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
        at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
        at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
        at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)

可以初步判断为流不存在造成的。

检查Action的相关代码:download.java

复制内容到剪贴板
代码:
        public InputStream getPhotoStream() throws Exception {

                ServletContext sc = ServletActionContext.getServletContext();
                String realpath = sc.getRealPath("/WEB-INF/upload");
                String path = realpath + "//" + fileName;
                System.out.println("<<<" + this.fileName);
                System.out.println(">>>" + path);
                return ServletActionContext.getServletContext().getResourceAsStream(path);
        }

首先,sc变量是个没充分使用的变量,最后一行又重新去了一次:“ServletActionContext.getServletContext()”。

第二,对ServletContext的getResourceAsStream方法理解有误,该方法不是从本地路径取文件,而是从web application的路径取文件流。

而且在return行前调用ServletActionContext.getServletContext().getResourceAsStream(path)应该可以发现是空指针,比如这样:

复制内容到剪贴板
代码:
                System.out.println(">>>"
                                + ServletActionContext.getServletContext().getResourceAsStream(
                                                path));
                return ServletActionContext.getServletContext().getResourceAsStream(
                                path);

打印值应该为null,这样就可以定位错误到行了。

我对这部分代码的修改:

复制内容到剪贴板
代码:
                return ServletActionContext.getServletContext().getResourceAsStream(
                                "/WEB-INF/upload/" + this.fileName);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过Struts2文件下载时Can not find a java.io.InputStream with the name 异常 的相关文章

  • System.Runtime.InteropServices.COMException(0x80040154):[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我在 C 项目中遇到异常 System Runtime InteropServices COMException 0x80040154 检
  • Android 和 Java 中绘制椭圆的区别

    在Java中由于某种原因Ellipse2D Double使用参数 height width x y 当我创建一个RectF在Android中参数是 left top right bottom 所以我对适应差异有点困惑 如果在 Java 中创
  • Java 环境变量设置正确,但 java 找不到 jar

    Summary Java 环境变量设置正确 从 echoing 可以看出 但是 仅当明确包含在 javac 命令中时才能看到 jar Details 这与图形和OpenGl没有具体关系 肯定问题与类路径有关 即第三方的导入jar files
  • Java异常处理——捕获超类异常

    我有一个关于处理网络应用程序异常的问题 我经常听说捕获超类 Exception 是一个坏主意 我经常编写代码来捕获 struts action java servlet 类中的所有异常 try call business facade bu
  • 如何让 Python 找到 ffprobe?

    I have ffmpeg and ffprobe安装在我的 mac macOS Sierra 上 并且我已将它们的路径添加到 PATH 中 我可以从终端运行它们 我正在尝试使用ffprobe使用以下代码获取视频文件的宽度和高度 impor
  • 将 LocalDate 转换为 DD/MM/YYYY LocalDate [重复]

    这个问题在这里已经有答案了 我有一个字符串DD MM YYYY日期格式 我想检查新日期是否比今天老 I use LocalDate now 但是 当我运行此代码时出现异常 LocalDate today LocalDate now Date
  • XSD 验证错误:在 web.xml 中找不到 TagLib 标记

    我详细显示错误如下 cvc complex type 2 4 a 发现以元素开头的无效内容 taglib One of http java sun com xml ns javaee 描述 http java sun com xml ns
  • 索引越界异常

    你好 这段代码将返回indexoutofboundsException我真的不知道为什么 我想从中删除这些对象pointlist它们与中的对象相同list public void listOfExternalPoints List
  • Excel的解析路径

    其实我想问以下问题 对于位于 目录中定义的 PATH 怎么能 我找出这些目录中的哪个 找到了 因为我需要使用 Process Run 从 C 运行 Excel 并且只需指示 Excel 即可正常工作 Windows 似乎知道在哪里可以找到它
  • Process.Start() 可以考虑系统路径吗?

    我已经对此进行了一段时间的搜索和实验 但我没有运气 我正在尝试制作一个控制台程序来自动执行一些我无法使用 BAT 文件完成的任务 我想从 Windows SDK 调用 signcode exe 该 bin 文件夹包含我的系统路径中的所有工具
  • 如何处理PDO异常[重复]

    这个问题在这里已经有答案了 我正在尝试与PDOphp 上的类 但我在找到处理错误的正确方法时遇到了一些麻烦 我编写了以下代码
  • 如果输入无效,是否可以抛出异常?

    我有一个简单的 ANLTR 语法和随附的访客 一切都很好 除非输入无效 如果输入无效 错误就会被吞噬 并且我的计算器会输出错误的输出 我尝试过实现一个错误侦听器 超越Recover词法分析器的方法 还有 好吧 今天还有六件事 有人可以告诉我
  • Android:将声音保存为铃声/SQLiteConstraintException

    我正在尝试使用 Android 将声音保存为铃声this http www stealthcopter com blog 2010 01 android saving a sound file to sd from resource and
  • 是否可以手动检查 LocateRegistry 是否存在?

    I 已经发现 https stackoverflow com a 8338852 897090一种安全的方式获得LocateRegistry 即使注册表尚不存在 Registry registry null try registry Loc
  • boost.log std::exception 格式化程序无法在自己的命名空间中找到运算符 << 重载

    我为 boost log 创建了一个简单的格式化程序 如下所示this http www boost org doc libs 1 59 0 libs log doc html log extension settings html例如st
  • Cmake 错误:无效的转义序列 \U

    使用 CMake 在 VC 10 中运行 OpenCL 代码时 出现以下错误 CMake Error at CMakeLists txt 6 set Syntax error in cmake code at C Users Shreedh
  • 有没有办法处理Java堆空间异常[重复]

    这个问题在这里已经有答案了 我正在寻找将文件输入流转换为大文件 文件大小为 100MB 并且抛出 java lang OutOfMemoryError Java Heap space import java io FileInputStre
  • “config”脚本存在于系统或 Homebrew 目录之外

    运行 brew doctor 并出现一些错误 我按照此链接中的建议设法解决了路径问题 如何修改 Homebrew 的 PATH https stackoverflow com questions 10343834 homebrew want
  • FOSRestBundle:显示我的自定义异常消息

    我试图在 FOSRestBundle 中添加异常的自定义控制 但它似乎忽略了我的自定义消息 响应的状态代码正常 I have throw new HttpException 404 User id not found 但得到这个 json
  • 断开连接期间处理 pyserial 中的异常

    我有从串口读取数据的代码 try dataIn self port read 100 except serial SerialException do some work return None 如果我断开设备连接 我会捕获我无法处理的异常

随机推荐