枚举 hasMoreElements()

发布于 2024-08-03 05:28:52 字数 747 浏览 4 评论 0原文

我有一个 JSP/HTML 表单,其中有 2 个元素。一个是选择下拉列表,另一个是文件上传框(输入类型=“文件”)。我使用 POST 方法和 enctype 作为 form-multipart。现在我能够使用 MultipartRequest 对象。没问题。

但是,当我不上传任何文件并且当我在接收表单中使用代码时,例如

MultipartRequest multipartRequest = new MultipartRequest(request,".",5*1024*1024);
String dummySelect= (String) multipartRequest.getParameter("dummy");
out.println("<BR>select is "+dummySelect);
Enumeration files = multipartRequest.getFileNames();

现在,理想情况下,如果我不上传任何文件,我应该得到一个空枚举器。含义

while(files.hasMoreElements())< /code> 应该评估为 false,但没有发生。任何人都可以告诉我为什么吗?这会导致 nullPointerException。

I have a JSP/HTML form where there are 2 elements.One is a select dropdown and another is a File upload box(input type="file").I use POST method and enctype as form-multipart.Now I am able to access both the dropdown list and file using MultipartRequest object.No problem in that.

But when I don't upload any file and when I use a code in the reciecing form like

MultipartRequest multipartRequest = new MultipartRequest(request,".",5*1024*1024);
String dummySelect= (String) multipartRequest.getParameter("dummy");
out.println("<BR>select is "+dummySelect);
Enumeration files = multipartRequest.getFileNames();

Now ideally if I don't upload any file I should get an empty enumerator.Meaning

while(files.hasMoreElements()) should evaluate to false which is not happening.Can anyone tell me why? This results in an nullPointerException.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-08-10 05:28:52

请确保您发送的内容类型(确切地)是两个

  • application/x-www-form-urlencoded
  • multipart/form-data

之一。根据您链接的 API,该类仅处理后一种类型。

或者,您可以尝试 Apache commons FileUpload 库。

Please make sure that the content type you send is (exactly) one of the two

  • application/x-www-form-urlencoded
  • multipart/form-data

According to the API you linked, the class handles only the latter type.

Alternatively, you can try the Apache commons FileUpload library.

老旧海报 2024-08-10 05:28:52

只需查看 javadoc 中的 getFileNames()方法 - 它表示,如果页面上有任何文件输入为空,则结果为 null,仅当页面上根本没有文件输入时才返回空枚举。不知道为什么它的实现如此奇怪,但文档是这样说的...

编辑:我想我都错了 - 即使对于留空的文件输入,枚举中也应该有一些东西(或者你应该在这种情况......不知道,医生对我来说不是很清楚)。

Just look on the javadoc for the getFileNames() method - it says, that in case of any file input left empty on the page, the result is null, empty enumeration is returned only when there are no file inputs at all on the page. Don't know why it's implemented this strange, but the documentation says so...

EDIT: I think I got it all wrong - there should be something in the enumeration even for file input that's left empty (or should you get empty enum in that case...don't know, the doc isn't very clear to me).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文