Results 1 to 3 of 3

Thread: Spring 3: Making the web-application accept null parameter for multipartfile argument

  1. #1

    Default Spring 3: Making the web-application accept null parameter for multipartfile argument

    Currently, when the file parameter is null or empty in the form, the web application throws an error. I have even checked for the case when file is empty but it doesn’t seem to work. Does anyone know how to check for the case when the filename field in the form is empty and not throw an error in the web app?

    I am using the following code for controller. As you can see I accept a Multi-part file parameter, which is the file uploaded in the form.

    @RequestMapping(value="/attachment", method=RequestMethod.POST)
    public String sendAttachment(@RequestHeader("Authorization") String authHeader, @RequestParam("to") String to,@RequestParam("cc") String cc,@RequestParam("bcc") String bcc,@RequestParam("subject") String subject,@RequestParam("body") String body, @RequestParam("file") MultipartFile file,@RequestParam("filename") String filename) {

    . . .

    //here i check if the file is empty...if not empty, then only attach it in the message.

    if(!file.isEmpty())
    {
    msg.getAttachments().addFileAttachment(filename,fi le.getBytes());
    }

    . . .
    The php file I am using to call this web service is as follows:

    Following is the form post parameters sent to the web service with empty file param

    $params = array(
    'to' => null,
    'cc' => null,

    'bcc' => "sanjaygir@gmail.com",
    'subject' => "hola sanjayg",
    'body' => "just testing message service",

    'filename'=> "",
    'file'=>""
    );
    I have also tried using null:

    $params = array(
    'to' => null,
    'cc' => null,

    'bcc' => "sanjaygir@gmail.com",
    'subject' => "hola sanjayg",
    'body' => "just testing message service",

    'filename'=> null,
    'file'=>null
    );
    Still it throws 500 error in the server.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Please use [ code][/code ] tags when posting code, that way it remains readable. @RequestParam has an attribute required, set it to false to allow for null elements. You still need to check for this in your controller because you can now have nullpointerexceptions because you directly check the size.

    I would suggest using a form object instead of all the request parameters makes it easier imho (and makes the parameters optional by default).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks a lot! That solves the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •