使用面向java的问题从salesforce的RESTAPI检索知识附件

m1m5dgzv  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(185)

我已经使用java从salesforce获取了附件(图像),我得到了http附件响应,但是当我在本地保存响应时,附件文件没有打开。我的代码如下:-

HttpResponse response = null;
   HttpClient httpClient = HttpClientBuilder.create().build();
   HttpGet httpGet = new HttpGet(baseUri + "/sobjects/Knowledge__kav/ka02w000000RzYOAA0/Attachment__Body__s");
   System.out.println("httpGet****"+httpGet);
    System.out.println("oauthHeader2: " + oauthHeader);
    httpGet.addHeader(oauthHeader);
    httpGet.addHeader(prettyPrintHeader);

    String getResult = null;
    try {
        response = httpClient.execute(httpGet);
        System.out.println("response****"+response);
        getResult = EntityUtils.toString(response.getEntity());
        EntityUtils.consume(response.getEntity());
    } catch (ParseException | IOException e) {
        System.out.println("Error****"+e);
    }

    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        return null;
    }
    System.out.println("getResult****"+getResult);
    Date date = new Date();
    String attachName = "pdf_" + date.getTime() + ".png";
    //File outputFile = new File("c:/test/tmp/"+attachName);
    File outputFile = new File("c:/Users/xyz/Desktop/Knowledge/Data/"+attachName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile));
        bos.write(getResult.getBytes());
        bos.flush();
        bos.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题