AabinGunz
AabinGunz

Reputation: 12347

how to get values from a multipart request in java

Here is the form submission part

var form=document.forms["mainForm"];
form.setAttribute("action",url_action);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");
form.submit();

Now how can I get all the parameters or form input type names and corresponding values to a map in servlet?

Map example:

name=Abhishek
age=25
filename=abc.txt

Upvotes: 1

Views: 3766

Answers (1)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299208

Use Commons / FileUpload:

The simplest case The simplest usage scenario is the following: Uploaded items should be retained in memory as long as they are reasonably small. Larger items should be written to a temporary file on disk. Very large upload requests should not be permitted. The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable. Handling a request in this scenario couldn't be much simpler:

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

Source: Using FileUpload

Upvotes: 3

Related Questions