Receiving Multipart Response on client side (ClosableHttpResponse)

后端 未结 2 1082
慢半拍i
慢半拍i 2020-12-03 12:40

I have a java controller which have to send me some text data and different byte arrays. So I am building n multipart request and writing it to stream from HttpServletRespon

相关标签:
2条回答
  • 2020-12-03 13:31

    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

    0 讨论(0)
  • 2020-12-03 13:34

    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.

    0 讨论(0)
提交回复
热议问题