使用JIRA Java API发布现有问题的附件

时间:2015-08-05 20:56:10

标签: api jira attachment

我正在尝试使用API​​在JIRA中发布现有问题的附件,但它给了我奇怪的错误:

公共课JiraRest {

public static void main(String[] args) throws ClientProtocolException, IOException 
{
String pathname= "C:/Users/skalbur/Videos/eclipse-jee-mars-R-win32-x86_64/Mars workspace/Desert.jpg"; 
File fileUpload = new File(pathname);

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("https://rallytojira.atlassian.net/rest/api/2/issue/FP-1389/attachments");
postRequest.setHeader("X-Atlassian-Token","nocheck");

MultipartEntityBuilder entity = MultipartEntityBuilder.create();    
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity((HttpEntity) entity);
HttpResponse response = httpClient.execute(postRequest);
}
}

我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
at JiraRest.main(JiraRest.java:33)
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.Lookup
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

我为我的程序使用了以下JAR:

apache-httpcomponents-httpcore.jar,json-simple-1.1.1.jar,httpmime-4.3.jar,jcommander.jar,httpclient-4.3-beta1.jar

1 个答案:

答案 0 :(得分:0)

此代码的工作原理:

public class JiraRest {
    public static void main(String[] args) throws ClientProtocolException, IOException
    {
        String pathname= "<Full path name of the attachment file>";
        File fileUpload = new File(pathname);
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost postRequest = new HttpPost("URL+Post REST API");
        BASE64Encoder base=new BASE64Encoder();
        String encoding = base.encode ("username:password".getBytes());
        postRequest.setHeader("Authorization", "Basic " + encoding);
        postRequest.setHeader("X-Atlassian-Token","nocheck");
        MultipartEntityBuilder entity=MultipartEntityBuilder.create();
        entity.addPart("file", new FileBody(fileUpload));
        postRequest.setEntity( entity.build());
        HttpResponse response = httpClient.execute(postRequest);
    }
}

必需的JAR: httpcomponents-client-4.5-bin和sun.misc.BASE64Decoder.jar的lib文件夹中的所有JAR