我已经做到了这种方式,它的工作原理:
public static void main(String[] args) throws Exception {
File f = new File(args[ 0 ]);
String fileName = f.getName();
String url = "https://[JIRA-SERVER]/rest/api/2/issue/[JIRA-KEY]/attachments";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.setHeader("Authorization", basicAuthHeader("username", "password"));
post.setHeader("X-Atlassian-Token", "nocheck");
HttpEntity reqEntity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName())
.build();
post.setEntity(reqEntity);
post.setHeader(reqEntity.getContentType());
CloseableHttpResponse response = httpClient.execute(post);
}
public static String basicAuthHeader(String user, String pass) {
if (user == null || pass == null) return null;
try {
byte[] bytes = (user + ":" + pass).getBytes("UTF-8");
String base64 = DatatypeConverter.printBase64Binary(bytes);
return "Basic " + base64;
}
catch (IOException ioe) {
throw new RuntimeException("Stop the world, Java broken: " + ioe, ioe);
}
}
的
可能重复[如何发送Java中的HTTP标头(http://stackoverflow.com/questions/819474/how- to-send-an-http-header-in-java) –
我会从https://studio.atlassian.com/svn/JRJC/trunk/atlassian-jira-rest-java-client/src/test/开始java/it/JerseyIssueRestClientTest.java其中有一个testAddAttachment方法 – mdoar
谢谢Mdoar。但是如何发布MultipartEntity还是没有任何提示。 –