首页 > 解决方案 > 带有 REST API 的文件附件

问题描述

下面的代码可以很好地在 JIRA 中附加文件,只有一个问题是我不能使用 MultipartEntityBuilder,因为它需要在 pom 中添加新的依赖项,这是不允许的,任何人都可以建议我可以在那里使用哪个基本 API ? 提前致谢

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

HttpPost postRequest = new HttpPost("https://xxxx.zzzz.net/rest/api/2/issue/" + issueID +"/attachments");
postRequest.setHeader("Authorization", "Basic <AUTHSTRING>");
postRequest.setHeader("X-Atlassian-Token", "nocheck");
File file = new File("C:\\Users\\MKumar\\Desktop\\Oauth_JIRA.rtf");
URL url = new URL("C:\\Users\\MKumar\\Desktop\\Oauth_JIRA.rtf");

MultipartEntityBuilder builder = MultipartBodyBuilder.create();

// This attaches the file to the POST:

builder.addBinaryBody(
    "file",
    new FileInputStream(file),
    ContentType.MULTIPART_FORM_DATA,
    file.getName()
);

HttpEntity multipart = builder.build();

postRequest.setEntity(multipart);

HttpResponse response = httpClient.execute(postRequest);

标签: javarest

解决方案


org.apache.http.entity.mime.MultipartEntity已被弃用,因此您必须使用MultipartEntityBuilder

有关更多相关帖子,请参阅此线程


推荐阅读