最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Handle octed-stream response pdf in Java - Stack Overflow

programmeradmin1浏览0评论

I am calling the soap Api. I am calling using HttpsURLConnection Code for making api call:

    HttpsURLConnection connection = (HttpsURLConnection) new URL(soapEndpointUrl).openConnection();
    connection.setRequestMethod("POST");
    // Encode SOAPAction value
    String encodedSoapAction = URLEncoder.encode(soapAction, StandardCharsets.UTF_8);

    connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    connection.setRequestProperty("SOAPAction", "\"" + encodedSoapAction + "\"");

    disableSSLVerification(connection, logger);

    connection.setDoOutput(true);

    // Write SOAP message to the output stream
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        soapMessage.writeTo(byteArrayOutputStream);
        byte[] requestBytes = byteArrayOutputStream.toByteArray();

        connection.getOutputStream().write(requestBytes);
        // connection.getOutputStream().flush();
    }
    // Get the response code and log it
    int responseCode = connection.getResponseCode();
    logger.info("Response Code: " + responseCode);

    // Get response from the server

    // Read the response
    StringBuilder response = new StringBuilder();
    String base64EncodedAttachment = null;

    try (BufferedReader br = new BufferedReader(new InputStreamReader(
            responseCode >= 200 && responseCode < 300 ? connection.getInputStream() : connection.getErrorStream(),
            StandardCharsets.UTF_8))) {
        String line;
        while ((line = br.readLine()) != null) {
            response.append(line.trim());
        }
    }

    if (responseCode == 200) {
        // Process the MIME response (attachment) here
        // logger.info(response.toString());
        String pdfMimeString = getpdfData(response.toString(), logger);
        if (pdfMimeString == null) {
            logger.info("Null value for pdfMimeString");
        }
        base64EncodedAttachment = extractPdfBase64(pdfMimeString, logger);
        logger.info("Base64 Encoded Attachment: ");
    }

    // Log the response code and return the JSON
    logger.info("Response Code: " + responseCode);
    // logger.info("Response" + response);

Response is as below:

       --uuid:9a919725-1013-42c2-9df9-4e145657ae1e+id=3Content-ID: : 8bitContent-Type: application/xop+xml;charset=utf-8;type="text/xml"
<s:Envelope xmlns:s=/>
<s:Body xmlns:xsi= xmlns:xsd=;
<retrieveDocumentResponse xmlns=/>
<retrieveDocumentResult>
<docData>
    <xop:Include href=cid: xmlns:xop=/></docData><createDt>2022-02-24T16:56:45</createDt><createdBy>BATCH</createdBy><docType/><dmnType>B</dmnType><formId>****</formId><clmntSsn>****</clmntSsn><clmntNm>***</clmntNm><claimType>UI</claimType><claimBybDt xsi:nil="true"/>
</retrieveDocumentResult>
</retrieveDocumentResponse>
</s:Body>
</s:Envelope>--uuid:9a919725-1013-42c2-9df9-4e145657ae1e+id=3Content-ID: : binaryContent-Type: application/octet-stream
%PDF-1.4%    4 0 obj<< Creator (thunderhead) Producer (thunderhead) CreationDate (D:20220224165645-05'00')>>endobj5 0 obj<< N 3 Length 11 0 R Filter FlateDecode>>streamx wTS Ͻ7 P {d+ }….EOF

I am taking pdf content as starting from %PDF-1.4% to EOF and encoding it to base 64 but base 64 value is not generating the correct pdf file after encoding.

Can any one suggest the best way to handle in Java.

    private static String getpdfDataEncoded(String s, LoggerUtil logger) {
    // Find the starting index of "%PDF"
    int startIndex = s.indexOf("%PDF");

    // Check if "%PDF" is found in the string
    String result = null;
    if (startIndex != -1) {
        // Extract the substring starting from "%PDF"
        result = s.substring(startIndex);
        logger.info("Extracted substring is created.");
    } else {
        logger.info("Substring not found.");
    }
    String encodedString = Base64.getEncoder().encodeToString(result.getBytes());


    return encodedString;
}
发布评论

评论列表(0)

  1. 暂无评论