V
V
Viktoria Smirnova2019-05-27 10:56:12
Java
Viktoria Smirnova, 2019-05-27 10:56:12

multipart/form-data java fields not coming?

Help solve the problem with getting data from Multipart/form-data of the form:
Form:

<form method="post" action="UploadServlet" enctype="multipart/form-data">
            Select file to upload: <input type="file" name="uploadFile" >
            First name:<br>
                <input type="text" name="firstname" value="Mickey">
            <br>
            Last name:<br>
            <input type="text" name="lastname" value="Mouse">
            <br/>
            <input type="submit" value="Upload" />
        </form>


POST Servlet:
try {
            // parses the request's content to extract file data
            List<FileItem> formItems = upload.parseRequest(request);
            Iterator<FileItem> iter = formItems.iterator();
            // iterates over form's fields
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                // processes only fields that are not form fields
                if (!item.isFormField()) {
                   String fileName = new File(item.getName()).getName();
                    String filePath = uploadPath + File.separator + dateFormat.format(date) + fileName;
                    File storeFile = new File(filePath);
                    
                    // saves the file on disk
                    item.write(storeFile);
                    String value = item.getString("firstname");
                    String value1 = item.getString("lastname");   
                }
            }


The file comes and is written but the text fields are null.
I tried this , but it doesn't help. What's my mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2019-05-28
@Vika7

If you add the @MultipartConfig annotation to the class and take parameters from the form like request.getParameter("firstname") at the beginning of the method, then everything seems to work. item.getString() - getString() takes as an argument an encoding, not a parameter name from the form.
Here in English they explain what's where

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question