Add Attachment to Jira via REST API

前端 未结 2 2072
小鲜肉
小鲜肉 2020-12-17 05:48

I\'m trying to post an attachment o JIRA using the latest REST API. Here\'s my code:

public boolean addAttachmentToIssue(String issueKey, String path){

             


        
相关标签:
2条回答
  • 2020-12-17 06:15

    @Nuno Neto, I'm surprised your method is working, as it's missing some key elements in the FileBody. Possible update to the Confluence API? Most importantly the file comment, and the encoding. As it were, your example will throw a 500, but for new people coming to this via Google the code below will in fact work.

    The major difference here would be:

    FileBody fileBody = new FileBody(fileToUpload, fileComment, "application/octet-stream", "UTF-8");
    

    I also have added a small bit of logic for empty file comments.

    /**************************************************************************************************
     /**
     * Confluence integration. This allows the user to attach captured images to confluence pages.
     *
    /**************************************************************************************************/
    /**
     *
     * @param pageID {int} Page ID of the Confluence page to add to. Navigate to Confluence page, hit 'e', copy the ID from the URI.
     * @param {String} path 
     * @param {String} user Your Confluence username.
     * @param {String} pass Your Confluence password.
     * @param {String} baseURL Your Confluence url.
     * @return {boolean}
     */
    
    public boolean addAttachmentToPage(int pageID, String path, String user, String pass, String baseURL, String fileComment){
        String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));
    
        if ( fileComment.equals("") | fileComment.equals(" ") | fileComment.equals(null)){
            fileComment = user + "-" + path;
        };
    
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost( baseURL + "/rest/api/content/" + pageID + "/child/attachment" );
        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, fileComment, "application/octet-stream", "UTF-8");
        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();
    
        // Success!
        if(response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Confluence -> Exported to the page with ID: " + confPageID);
            return true;
        }
        else {
            System.out.println("Confluence -> Error : " + response.getStatusLine().getStatusCode());
            System.out.println(response + "\n" + "\n" + response.getAllHeaders() + "\n" + result + "\n" + path + "\n" + "Attempted against: " + baseURL + "/rest/api/content/" + pageID + "/child/attachment" + "\n");
            return false;
        }
    };
    
    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
提交回复
热议问题