Sending additional data with multipart [duplicate]
Asked Answered
A

5

11

I am using apache-commons-fileupload to get file from client to the 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>

Servlet: GetFile

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

I am able to upload the file to the server, but I am not able to get the value of text1 in the servlet (I am getting null value of text1 in the servlet), I need this textfield in the form to submit some additional information while uploading it to the server.

  • Is enctype="multipart/form-data" option of form doesn't allow other form data to be submited? if it doesn't allow it then what are the other options I have to send this additional textfield to the server.
  • Or is there any other problem in my code?
Amalia answered 27/2, 2013 at 6:7 Comment(4)
you can use javascript or jquery to set the data in textfieldJulissa
@Julissa that has nothing to do with the problem of the OP.Emulous
Could it be that you have more input fields with name="text1"? Cause request.getParameter(...) only gets the first value of a parameter, if you have more with the same name attribute the first one is might empty and therefore null.Emulous
@Julissa Why in the world do you think it will help to set the field using js/jquery?Paganism
P
14

Is enctype="multipart/form-data" option of form doesn't allow other form data to be submited? if it doesn't allow it then what are the other options I have to send this additional textfield to the server.

No there is no issue with using enctype="multipart/form-data". You can get other fields then file in such form.

Or is there any other problem in my code?

Yes, as for now. While using enctype="multipart/form-data" you can not directly get parameters by using request.getParameter(name);. While using it, form fields aren't available as parameter of the request, they are included in the stream, so you can not get it the normal way. You can find a way to do this in the docs of using commons-fileupload#Processing the uploaded items.

Paganism answered 27/2, 2013 at 6:17 Comment(0)
C
14

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

You have to get all the items from the Request and iterate and handle it accordingly based on their item type

List  items = upload.parseRequest(request);

Heres 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);
    }
}
Cullin answered 27/2, 2013 at 6:36 Comment(1)
I searched for an hour for my bug, here iis the solution. Thank youDecember
H
1
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();
    }

Above code will read file along with other form data just have a look at req.getParameter(); method ofMultipartRequest req object

Homopolar answered 20/7, 2015 at 9:57 Comment(0)
A
0
  1. download the jar file com.oreilly.servlet.MultipartRequest
  2. import com.oreilly.servlet.MultipartRequest in your servlet / .java file contained in Web-Inf/classes
  3. in your servlets doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { method add

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

then call your variables from the form as below;

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

and print them from the servlet like out.println(pdate);

Aphonic answered 27/5, 2013 at 14:4 Comment(0)
M
0

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

Using iterator iterate through multipart HTTPServletRequest and use a for loop to check if it isFormField(), 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 be 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>
Minette answered 5/2, 2014 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.