【问题标题】:Java Fileupload getParts() return Files and a null valueJava Fileupload getParts() 返回文件和空值
【发布时间】:2017-02-07 13:14:02
【问题描述】:

我正在尝试创建文件上传。我的文件输入:

<input class="form-control" name="file" type="file" size="20" accept="application/pdf, image/jpeg, image/png, text/plain" multiple >

servlet 中的这个 for 循环遍历 Parts 以获得它们的总大小。它遍历选定的文件和一个大小为 1 且内​​容类型为 null 的附加元素。这会导致错误。

for (Part part : request.getParts()) {
            System.out.println("Dateigrösse: " + part.getSize());
            uploadSize = uploadSize + part.getSize();
            System.out.println("Dateityp: " + part.getContentType());
            if(part.getContentType().equals("application/pdf") || part.getContentType().equals("text/plain") || part.getContentType().equals("image/jpeg") || part.getContentType().equals("image/png")) {
            } else {
                types = false;
            }
        }

首先它起作用了,但知道有这个空元素。我该怎么办?它来自哪里?我什么都不改变。我正在使用来自 oracle EE7 doc 和著名在线文章的代码。在这个 for 循环之前我没有改变任何东西。

提前谢谢你。

【问题讨论】:

    标签: java html web file-upload


    【解决方案1】:

    getParts() 也返回所有命名的输入。所以它把我选择的主题作为一个部分返回给我。现在我正在过滤名为“文件”的输入。

    【讨论】:

      猜你喜欢
      • 2011-11-18
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多