Flama
Flama

Reputation: 868

How to put a maximum limit on file amount being uploaded using Spring MVC?

I'm allowing the upload of JPG files in my program. What I want to do is to set a limit of the amount of photos, let's say 15.

So I got the following in my JSP file:

<td  class="upload-pic"><input class="file-submit" type="file" name="fileUpload" size="50" multiple="multiple" accept=".jpg"/></td>

And here's a part of my controller. This works fine and I could obviously check here or make the program not to upload if there are more than 15 photos, BUT what I want to do is to send the user a message letting him know that he can't upload more than 15 photos.

@RequestMapping(value = "publish4" ,method = RequestMethod.POST)
public ModelAndView publish4(@Valid @ModelAttribute("fourthPublicationForm") final FourthPublicationForm form, final BindingResult errors,
                             @RequestParam("type") String type, @RequestParam("operation") String operation , @RequestParam CommonsMultipartFile[] fileUpload) {
    if (errors.hasErrors()) {
        //return publish4();
    }
    long userid = us.findByUsername(SecurityContextHolder.getContext().getAuthentication().getName()).getUserId();
    Publication aux = ps.create(form.getTitle(), form.getAddress(), operation, form.getPrice(), form.getDescription(), 
            type, form.getBedrooms(), form.getBathrooms(), form.getFloorSize(), form.getParking(),userid);


    if (fileUpload != null && fileUpload.length > 0) {
        for (CommonsMultipartFile aFile : fileUpload){

            System.out.println("Saving file: " + aFile.getOriginalFilename());


            UploadFile uploadFile = new UploadFile();

            uploadFile.setId((int) (Math.random()*1000000));

            uploadFile.setPublicationId(Long.toString(aux.getPublicationid())); 

            uploadFile.setData(aFile.getBytes());

            final long limit = 20 * 1024 * 1024; //10MB

            System.out.println("I'm about to save with id: " + uploadFile.getId());
            if(aFile.getSize() < limit && uploadFile.getData().length > 0) {
                if(aFile.getOriginalFilename().contains(".jpg")) {
                    fileUploadImpl.save(uploadFile); 
                }else {
                    System.out.println("Sth went wrong with file format");
                }

            }else {
                System.out.println("File is too big or it's empty");
            }


        }
    }
    return new ModelAndView("redirect:/meinHaus/home");
}

As you see I declared this in my controller @Valid @ModelAttribute("fourthPublicationForm") final FourthPublicationForm form

But I'm not sure how to get the amount of files from there and then show a message? Right now that class is empty.

Upvotes: 1

Views: 269

Answers (1)

grindlewald
grindlewald

Reputation: 338

I am not sure I understood the query correctly; if you want to check the number of files uploaded, then in your code, fileUpload.length would give you that. And then send a warning message to your jsp (view) from controller something like this

attributes.addFlashAttribute("errorMsg", "Not more than 15 files allowed!");

Check out this link https://howtodoinjava.com/spring-mvc/spring-mvc-multi-file-upload-example/ (might not open in Chrome) he's done a similar thing but using List of MultipartFile for images instead of an array. Also, with RequestParam, you can check the size with min and max limits, see this link for String https://www.javaquery.com/2018/02/passing-and-validating-requestparam-in.html. But, I don't know if it will work for an array of objects in your case.

Upvotes: 1

Related Questions