简体   繁体   中英

Java servlet can't receive the parts of an http post multipart request

First, I asked this question What did I miss to send a http part post request and it seems i didn't understand the logic of sending multipart from a client to a server. Using this post https://developer.constantcontact.com/docs/mylibrary-files-api/file-add-multipart-post.html I was able t see the required fields in each part.

I build my request and added all the required fields as you see here

HttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(
                "http://localhost:8080/ServletExample1/multipart1");
        httpPost.addHeader("Content-Type",
                "multipart/related; boundary=HereItGoes");
        httpPost.addHeader("Accept", MediaType.TEXT_PLAIN);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        FileBody bin = new FileBody(new File("./test.txt"));
        builder.addPart("source", new StringBody("MyComputer",
                ContentType.TEXT_PLAIN));
        builder.addPart("folder_id", new StringBody("-1",
                ContentType.TEXT_PLAIN));
        builder.addPart("file_type", new StringBody("txt",
                ContentType.TEXT_PLAIN));
        builder.addPart("file_name", new StringBody("test.txt",
                ContentType.TEXT_PLAIN));
        builder.addPart("description", new StringBody("The file to test",
                ContentType.TEXT_PLAIN));
        builder.addPart("data", bin);
        HttpEntity entity = builder.build();
        httpPost.setEntity(entity);
        HttpResponse response = client.execute(httpPost);
        String responseString = new BasicResponseHandler()
                .handleResponse(response);
        System.out.println(responseString);

my problem is in the server, i keep receive that the number of the parts is zero. for somewho the server is not receiving the parts (note i am not saying there is exceptions in the server)

I am saying this because on the server (my servlet) i do this

Iterator<Part> partsIterator = request.getParts().iterator();
            System.out.println("The number of parts is :"
                    + request.getParts().size());

and the result of the printing is always zero, always

what am i missing please?

Servlets' multipart configuration expects a Content-Type of multipart/form-data .

This is specified, among other places, in the javadoc of @MultipartConfig

Annotation that may be specified on a javax.servlet.Servlet class, indicating that instances of the Servlet expect requests that conform to the multipart/form-data MIME type.

You're specifying a different content type

httpPost.addHeader("Content-Type", "multipart/related; boundary=HereItGoes");

Get rid of this. The MultipartEntityBuilder already builds a request with the appropriate headers. Just add

builder.setBoundary("HereItGoes")

for the boundary.

Example Servlet

@WebServlet(loadOnStartup = 1, urlPatterns = "/multipart1")
@MultipartConfig()
public class MyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("The number of parts is :" + request.getParts().size());
        Iterator<Part> partsIterator = request.getParts().iterator();
        while (partsIterator.hasNext()) {
            System.out.println(partsIterator.next().getName());
        }
    }
}

It will receive all 6 of the parts you send with the code in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM