我已经这样做了,它的工作原理:
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 );
}
}