Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring : Multipart form data request : Read dynamic parameter from request

I am using Spring framework and able to upload the file on server successfully.

<form action="upload.do" method="post" enctype="multipart/form-data">
    <input type="text" name="description" />
    <input type="file" name="locationMapFile" />
    ........
    ........
    <input type="submit" />
</form>

// controller layer

@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
   ,@RequestParam("description")String desc
   ,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData)
{

}

till now everything is fine. Now i am adding some dynamic hidden parameter on form using the javascript.

Note : as per setting i am defining the dynamic parameter name and its value like

<input type="hidden" name="setting_14" value="abcd"> 
<input type="hidden" name="setting_5" value="xyz"> 

How can i fetch these dynamic parameter into Spring controller.

I have tried

(1) I can not use the @RequestParam since do not want to hard code the parameter name

(2) request.getParameter() : not working and returning null since this is multipart/form-data request

(3) I have used this link How to upload files to server using JSP/Servlet? and tried

List<FileItem> items = 
       new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

but in spring controller items is null. so not able to iterate it and fetch the FileItem from it.

Please help me to find out the way to get the dynamic parameter's value into the spring framework.

like image 468
Rakesh Soni Avatar asked Nov 07 '13 08:11

Rakesh Soni


1 Answers

You can use MultipartHttpServletRequest to get the request parameters:

@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
   ,@RequestParam("description")String desc
   ,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData, MultipartHttpServletRequest mrequest)
{
String value = mrequest.getParameter("setting_14");
}
like image 169
Debojit Saikia Avatar answered Oct 25 '22 18:10

Debojit Saikia