且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 REST API 向 Jira 添加附件

更新时间:2022-02-11 21:29:53

我已经通过使用 apache http 客户端解决了这个问题对于谁可能有同样的问题,这里是代码:

I've managed to resolve the issue by using the apache http client For whom may have the same issue, here's the code:

public boolean addAttachmentToIssue(String issueKey, String path){


        String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));


    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(baseURL+"issue/"+issueKey+"/attachments");
    httppost.setHeader("X-Atlassian-Token", "nocheck");
    httppost.setHeader("Authorization", "Basic "+auth);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    File fileToUpload = new File(path);
    FileBody fileBody = new FileBody(fileToUpload, "application/octet-stream");
    entity.addPart("file", fileBody);

    httppost.setEntity(entity);
    HttpResponse response = null;
    try {
        response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
    HttpEntity result = response.getEntity();

    if(response.getStatusLine().getStatusCode() == 200)
        return true;
    else
        return false;

}