0

I am trying to make a little bit complex form for uploading files, Since it is different file size, I separate number of type, the form as follows : multiple and single file upload

I have succeeded to upload multi file with different name,

<form name="uploadForm" method="post" enctype="multipart/form-data" action="file.spring?action=upload">
<input type="file" name="upFile[0]">
<input type="file" name="upFile[1]">
<input type="file" name="upFile[2]">
</form>

but for the following form

<form name="uploadForm" method="post" enctype="multipart/form-data" action="file.spring?action=upload">
<input type="file" name="upFile">
<input type="file" name="upFile">
<input type="file" name="upFile">
</form>

the only file uploaded was the first one,

before inserting, I prepare Util class as follows to get the fileList,

public static List<Map<String, MultipartFile>> getFilesFromRequest(HttpServletRequest request){
        List<Map<String, MultipartFile>> fileList = new ArrayList<Map<String,MultipartFile>>();

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Iterator<String> it = multipartRequest.getFileNames();
        while(it.hasNext()){
            String key = it.next();
            if(!multipartRequest.getFile(key).isEmpty()){
                Map<String, MultipartFile> file = new HashMap<String, MultipartFile>();
                file.put(key, multipartRequest.getFile(key));           
                fileList.add(file);
            }
        }

        return fileList;
    }

is there something that I miss ?

Update :

I found the solution by changing MultipartHttpServletRequest to DefaultMultipartHttpServletRequest

Thanks to @Bart , the answer can be found in this post

1 Answer 1

0

You are using the same name(upfile) for all the files

<form name="uploadForm" method="post" enctype="multipart/form-data" action="file.spring?action=upload">
<input type="file" name="upFile">
<input type="file" name="upFile">
<input type="file" name="upFile">
</form>

and in your Util you are trying to iterate the files by names

Iterator<String> it = multipartRequest.getFileNames();

So you will get only one

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer @juned-ahsan , do you have any idea how to iterate the list of all value from parameters "upFile" ?
@tituchio An alternate way to iterate can be to iterate all the parts of request using MultipartHttpServletRequest.getParts() method . You may check the content type of part to decided whether its a file or a form text param.
Thanks @Juned, finally I found the solution by changing the request as DefaultMultipartHttpServletRequest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.