我试图在JIRA使用API发布一个现有问题的附件,但它给了我奇怪的错误:
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.3 .jar、jcommander.jar、httpclient-4.3-beta1.jar
此代码有效:
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