Sending additional data using multipart

I use apache-commons-fileupload to get the file from client to server . (using JSP and Servlet ).

Jsp / html

 <form method="POST" action="GetFile" enctype="multipart/form-data"> <input type="file" name="datafile"> <input type="text" name="text1"> <input type="submit" value="Next"> </form> 

Servl: GetFile

 System.out.println(request.getParameter("text1")); 

I can upload the file to the server, but I can’t get the text1 value in Servlet (I get null the text1 value in Servlet ), I need this textfield in the form to send some additional information when loading it to the server .

  • enctype="multipart/form-data" form option enctype="multipart/form-data" others from generating the data to be submitted? if he does not allow this, then what are the other parameters that I should send to this additional textfield in server .
  • Or is there another problem in my code?
+7
source share
5 answers

enctype="multipart/form-data" form option enctype="multipart/form-data" others from generating the data to be submitted? if this does not allow, then what are the other parameters I need to send this additional text field to the server.

No problem using enctype="multipart/form-data" . You can get other fields, then a file in this form.

Or is there another problem in my code?

Yes, as of now. When using enctype="multipart/form-data" you cannot directly get parameters using request.getParameter(name); . When using it, form fields are not available as a request parameter, they are included in the stream, so you cannot get it in the usual way. You can find a way to do this in the documents using commons-fileupload in the Handling Loaded Items section.

+9
source

Well, the parameters are not lost, namely that they are part of the Stream request.

You should get all the elements from the query and iterate and process it accordingly based on their element type

 List items = upload.parseRequest(request); 

Here is how you can get it

 // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName();//text1 String value = item.getString(); } else { processUploadedFile(item); } } 
+9
source
 MultipartRequest req = new MultipartRequest(request, UPLOAD_PATH, 1024 * 1024 * 1024); out.print(req.getParameter("contractNo")); out.println("<BR>"); Enumeration files = req.getFileNames(); while (files.hasMoreElements()) { String name = (String) files.nextElement(); String filename = req.getFilesystemName(name); String type = req.getContentType(name); File uploadedFile = req.getFile("xlFile"); FileInputStream fis = new FileInputStream(uploadedFile); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); FileWriter fstream = new FileWriter(UPLOAD_PATH + name, true); BufferedWriter out11 = new BufferedWriter(fstream); String aLine = null; while ((aLine = in.readLine()) != null) { //Process each line and add output to Dest.txt file out11.write(aLine); out11.newLine(); } // do not forget to close the buffer reader in.close(); // close buffer writer out11.close(); } 

The above code will read the file along with other form data, just take a look at req.getParameter(); MultipartRequest req object method

+1
source
  • download jar file com.oreilly.servlet.MultipartRequest
  • import com.oreilly.servlet.MultipartRequest in the servlet /.java file contained in Web-Inf/classes
  • in your servlet doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { add method

    MultipartRequest m = new MultipartRequest (request, "C: \ SavingDirectory");

then call the variables from the form as shown below:

 String pdate = m.getParameter("plandate"); 

and print them from a servlet, for example out.println(pdate);

0
source

The best practice for getting HTML form fields in Servlet is to use apache commons-fileupload 1.3 jar.

Using the iteration iterator through multipart HTTPServletRequest and use the for loop to check if it is FormField (), then

  String item1=null,item2=null,item3=null; if(item.isFormField()) { if(item.getFieldName().equals("field1")) { item1=item.getString(); } if(item.getFieldName().equals("field2")) { item2=item.getString(); } if(item.getFieldName().equals("field3")) { item3=item.getString(); } } 

and your HTML file should look like this

 <html> <body> <form action="servletname" method="post" enctype="multipart/form-data"> <input type="text" name="field1"> <input type="text" name="field2"> <input type="text" name="field3"> <input type="file" name="filetoupload"> <input type="submit" value="Upload"> </form> </body> </html> 
0
source

All Articles