Java – receive a closeablehttpresponse on the client
•
Java
I have a Java controller that must send me some text data and different byte arrays So I'm building n multipart requests and writing them to the stream from httpservletresponse
Now my question is how to parse the response and extract multiple parts on the client
Server code chain: –
multipartentityBuilder builder = multipartentityBuilder.create(); // Prepare payload builder.addBinaryBody("document1",file); builder.addBinaryBody("document2",file2); builder.addPart("stringData",new StringBody(jsonData,ContentType.TEXT_PLAIN)); // Set to request body httpentity entity = builder.build(); postRequest.setEntity(entity);
Customer code snippet: –
HttpPost httpPost = new HttpPost(finalUrl); StringEntity entity = new StringEntity(json); httpPost.setEntity(entity); httpPost.setHeader("Content-type",APPLICATION_JSON_TYPE); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CloseableHttpResponse response = httpClient.execute(httpPost); InputStream in = new BufferedInputStream(response.getEntity().getContent());
I checked closeablehttpresponse and httpentity, but neither of them provided a way to parse multipart requests
Edit 1: This is the sample reply I received in the client stream: –
--bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe Content-Disposition: form-data; name="numeric" Content-Type: text/plain; charset=ISO-8859-1 Content-@R_555_301@: 8bit 01010110 --bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe Content-Disposition: form-data; name="stringmessage" Content-Type: text/plain; charset=ISO-8859-1 Content-@R_555_301@:8bit testmessage --bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe Content-Disposition: form-data; name="binarydata"; filename="file1" Content-Type: application/octet-stream Content-@R_555_301@: binary HI,THIS IS MY BINARY DATA --bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe Content-Disposition: form-data; name="ending" Content-Type: text/plain; charset=ISO-8859-1 Content-@R_555_301@: 8bit ending --bvRi5oZum37DUldtLgQGSbc5RRVZxKpjZMO4SYDe--
Solution
I finally got a solution
I will use javax mail mimemultipart
Here is the solution code: –
ByteArrayDataSource datasource = new ByteArrayDataSource(in,"multipart/form-data"); MimeMultipart multipart = new MimeMultipart(datasource); int count = multipart.getCount(); log.debug("count " + count); for (int i = 0; i < count; i++) { BodyPart bodyPart = multipart.getBodyPart(i); if (bodyPart.isMimeType("text/plain")) { log.info("text/plain " + bodyPart.getContentType()); processTextData(bodyPart.getContent()); } else if (bodyPart.isMimeType("application/octet-stream")) { log.info("application/octet-stream " + bodyPart.getContentType()); processBinaryData(bodyPart.getInputStream())); } else { log.warn("default " + bodyPart.getContentType()); } }
If others have any standard solutions, please let me know
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码