简体   繁体   中英

Java Fileupload getParts() return Files and a null value

I'm trying to create a fileupload. My file input:

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

And this for-loop in the servlet iterate over the Parts to get their total size. It iterate over the selected files and a additional element with size 1 and content type null . This cause an errror.

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;
            }
        }

在这里,我遍历列表并打印出大小和类型

First it worked, but know there's this null element. What should I do? Where does it comes from? I don't change anything. I'm using the code from oracle EE7 doc and a famous online article. I don't change anything before this for-loop.

Thank you in advance.

The getParts() returns all named Inputs too. So it returns me the choosen subject as a Part. Now i'm filtering for inputs named "file".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM