【问题标题】:How to make jira attachments download work using jira rest api如何使用 jira rest api 使 jira 附件下载工作
【发布时间】:2020-03-30 17:12:08
【问题描述】:

我正在尝试从 Jira 下载附件。我使用 /rest/api/2/attachment/{id} 来获取附件的 json 响应。它有字段“内容”,即附件 url。我使用这个 url 并构造 HttpGet 并执行以获得响应,该响应总是给我要求登录的 html 内容。我在 httpGet 标头中发送基本授权。显然这适用于下载 .png 文件,但不适用于任何其他文件类型。我正在使用 java spring rest 连接到 Jira Horizo​​n。

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

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

【问题讨论】:

  • 你能分享完整的代码或者你是如何下载文件的吗?

标签: download jira attachment jira-rest-api


【解决方案1】:

这行得通:

@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";
}

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多