我正在尝试使用最新的 REST API 发布一个 JIRA 附件。 这是我的代码:
public boolean addAttachmentToIssue(String issueKey, String path){
String auth = new
String(org.apache.commons.codec.binary.Base64.encodeBase64((user+":"+pass).getBytes()));
Client client = Client.create();
WebResource webResource = client.resource(baseURL+"issue/"+issueKey+"/attachments");
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
File f = new File(path);
if(f.exists() && f.isFile()){
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
return false;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
fis.close();
bos.close();
} catch (IOException ex) {
try {
fis.close();
bos.close();
} catch (IOException e) {
return false;
}
return false;
}
byte[] bytes = bos.toByteArray();
FormDataBodyPart bodyPart = new FormDataBodyPart("file", new ByteArrayInputStream(bytes), MediaType.APPLICATION_OCTET_STREAM_TYPE);
formDataMultiPart.bodyPart(bodyPart);
}else{
return false;
}
ClientResponse response = null;
response = webResource.header("Authorization", "Basic " + auth).header("X-Atlassian-Token", "nocheck").type(MediaType.MULTIPART_FORM_DATA).accept("application/json").post(ClientResponse.class, formDataMultiPart);
System.out.println(response);
int statusCode = response.getStatus();
System.out.println(statusCode);
String resp = response.getEntity(String.class);
System.out.println(resp);
return true;
}
但是,我得到以下响应:
POST http://localhost:8082/rest/api/2/issue/TEST-2/attachments returned a response status of 404 Not Found
404
XSRF check failed
我的本地 JIRA 实例中确实存在 key TEST-2 的问题,我可以在 Jira 应用程序本身中“手动”添加附件。 我知道我必须添加一个“X-Atlassian-Token:nocheck”类型的 header 来防止 XSRF,但是,从输出来看,我一定是做错了什么.. 更让我困惑的是,在 XSRF 检查失败后抛出 404。
我已经在谷歌上搜索了没有成功的答案 谁能猜猜我做错了什么?
最佳答案
我已经设法通过使用 apache http 客户端解决了这个问题 对于谁可能有同样的问题,这里是代码:
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;
}
关于java - 通过 REST API 将附件添加到 Jira,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18631361/