2

我正在尝试访问公开 pdf 文件的 Rest 服务,但在调用该过程时收到此响应:

{
   "errors": [
      "Failed to parse the payload from backend (procedure: HttpRequest)"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "responseHeaders": {
      "Content-Type": "application\/octet-stream; type=\"application\/xop+xml\"; boundary=\"uuid:****************\"; start=\"<pdf>\"; start-info=\"application\/pdf\"",
      "Date": "Thu, 07 Nov 2013 14:44:54 GMT",
      "Server": "Apache-Coyote\/1.1",
      "Transfer-Encoding": "chunked",
      "X-Powered-By": "Servlet 2.5; **********",
      "content-disposition": "attachment; filename = ********.PDF"
   },
   "responseTime": 5329,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 9923,
   "warnings": [
   ]
}

我可以使用工作灯适配器获取 pdf 文件吗?有没有其他方法?

4

2 回答 2

3

我可以通过从适配器调用 Java 例程来调用将 PDF 作为byte[]. 一旦byte[]检索到,我base64在 Java 例程中对其进行编码,在适配器中对其进行 URI 编码,然后在客户端 js 代码中执行相反的操作。Java 代码使用 Apache HttpClient. 我尝试了普通的返回类型以及其他没有运气的类型。

Java示例在这里

适配器代码:

var service = new ServiceClient(); 
var pdf = service.getUnsecureContentBase64(url);
result.pdf = encodeURIComponent(pdf);

return result;

客户端代码:

onSuccess : function (response){

        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        //use pdf byte[] to pass to Mozilla pdf.js
        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        PDFJS.disableWorker = false;
        PDFJS.getDocument(pdf).then(function (pdfDoc) {
            //use pdfDoc to render
        });
    }

Java代码:

public class ServiceClient {

public String getUnsecureContentBase64(String url)
        throws ClientProtocolException, IOException {

    byte[] result = getUnsecureContent(url);

    return Base64.encodeBase64String(result);
}

public byte[] getUnsecureContent(String url)
        throws ClientProtocolException, IOException {

    byte[] result = null;
    CloseableHttpClient httpclient = null;

    try {
        httpclient = HttpClientBuilder.create()
                .setRedirectStrategy(new LaxRedirectStrategy()).build();

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
            public byte[] handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toByteArray(entity);
                } else {
                    return null;
                }
            }
        };

        result = httpclient.execute(httpget, handler);

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }

    return result;
}
} 
于 2013-11-12T04:17:41.917 回答
1

您需要在适配器实现中更改“returnedContentType”参数。我的猜测是现在您将其设置为“xml”。由于从后端检索的 pdf 不是 XML,因此您会收到该错误消息。

例子:

function getPDF() {

    var input = {
        method : 'get',
        returnedContentType : 'plain',
        path : "/test.pdf"
    };

    return WL.Server.invokeHttp(input);
}
于 2013-11-07T15:38:20.213 回答