如何使用jirarestapi下载jira附件



我正在尝试从Jira下载附件。我使用/rest/api/2/attach/{id}来获取附件的json响应。它具有字段";内容";那就是附件url。我使用这个url,构造HttpGet并执行以获得响应,该响应总是向我提供请求登录的html内容。我正在httpGet标头中发送基本授权。显然,这适用于下载.png文件,但不适用于任何其他文件类型。我正在使用java spring-rest连接Jira horizon。

Closeable httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(jira attachmenturl);
httpGet.setHeader("Authorization", "Basic <base64Credentials>);
CloseableHttpResponse response = httpclient.execute(httpGet)

对.txt、.jpeg、Microsoft文档的响应与我对.png文件的响应不同。

这是有效的:

@GetMapping(value = "/getAttachment")
public String getAttachment(@RequestParam("id") String attachmentID) throws Exception {
Client client = Client.create();

WebResource webResource = client.resource(jiraBaseURLRest + "attachment/" +
attachmentID);

ClientResponse response = webResource.header("Authorization", "Basic " +
base64Creds).type("application/json")
.accept("application/json").get(ClientResponse.class);

String result = response.getEntity(String.class); 
JSONObject jsonObj = new JSONObject(result);
System.out.println("JSON Object = "+jsonObj);
URL url = new URL(jsonObj.getString("content"));

Closeable httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.toURI());
httpGet.setHeader("Authorization", "Basic " + base64Creds);
CloseableHttpResponse response1 = ((CloseableHttpClient) httpclient).execute(httpGet);

if(response1.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response1.getEntity();
if (entity.isStreaming())
{ 
System.out.println("Streaming..."); 
byte data[] = EntityUtils.toByteArray(entity); 
FileOutputStream fout = new FileOutputStream(new File("D://pdf1.pdf"));
fout.write(data);
fout.close();
System.out.println("Done!!"); 
}
}

return "Success";
}

相关内容

  • 没有找到相关文章

最新更新