Announcement Announcement Module
Collapse
No announcement yet.
Spring 3: Making the web-application accept null parameter for multipartfile argument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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' => "[email protected]",
    'subject' => "hola sanjayg",
    'body' => "just testing message service",

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

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

    'bcc' => "[email protected]",
    'subject' => "hola sanjayg",
    'body' => "just testing message service",

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

  • #2
    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).

    Comment


    • #3
      Thanks a lot! That solves the problem.

      Comment

      Working...
      X