且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过REST API在JIRA中上传文件

更新时间:2022-02-11 21:30:05

这就是我依赖okhttp和okio的方式

This is how i did it dependencies okhttp and okio

private static void upload(File file) throws Exception{
    final String address = "https://domain/rest/api/2/issue/issueId/attachments";
    final OkHttpClient okHttpClient = new OkHttpClient();
    final RequestBody formBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("text/plain"), file))
            .build();
    final Request request = new Request.Builder().url(address).post(formBody)
            .addHeader("X-Atlassian-Token", "no-check")
            .addHeader("Authorization", "Basic api_token_from_your_account")
            .build();
    final Response response = okHttpClient.newCall(request).execute();
    System.out.println(response.code() + " => " + response.body().string());
}