Receiving Multipart Response on client side (ClosableHttpResponse)

三世轮回 提交于 2019-11-27 14:32:35

I have finally got a workaround for it.

I will be using javax mail MimeMultipart.

Below is a code snipped for the solution:-

    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());
        }
    }

Please let me know if anybody else have any standard solution.

Mime4j from Apache is one way to parse the responses from client-side. Its a common practice to use a tool like this.

You can refer this link - http://www.programcreek.com/java-api-examples/index.php?api=org.apache.james.mime4j.MimeException

You can download the jar from this link - http://james.apache.org/download.cgi#Apache_Mime4J

Hope this helps. Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!