使用Java和Apache HttpClient将附件发布到confluence时得到404

时间:2016-09-27 11:38:59

标签: java api post attachment confluence

我正在尝试将图片发布到汇合页面作为内容的附件。

这是我的java应用程序的功能:

wordToScan

终端输出为:

public void postAttachment(File f, String comment) throws IOException {

    if (!f.exists() || !f.canRead()) {
        throw new IOException("Could not access file " + f.getAbsolutePath());
    }

    final FileBody bin = new FileBody(f);
    final StringBody cmt = new StringBody(comment, ContentType.TEXT_PLAIN);
    final HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", cmt).build();

    final HttpPost post = new HttpPost(baseUrl + "/rest/api/content/" + contentid + "/child/attachment");
    post.setEntity(reqEntity);

    post.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(props.getString("confluence_user"),props.getString("confluence_password")), "UTF-8", false));
    post.addHeader("X-Atlassian-Token:","no-check");

    System.out.println("executing request " + post.getRequestLine());
    final CloseableHttpClient httpclient = HttpClients.createDefault();


    final CloseableHttpResponse response = httpclient.execute(post);

    System.out.println(post.getRequestLine());
    System.out.println(response.toString());

    if (response.getStatusLine().getStatusCode() == 404) {
        throw new IOException("Status 404 thrown!");
    }

}

然后

POST https://xxx.xxxx.de:443/rest/api/content/38262140/child/attachment 

(我为此帖更改了域名和用户名..)

所以一切似乎都好。如果我复制生成的POST URL并在浏览器中执行GET,我会得到一个带有附件列表的json片段,之前手动上传。所以POST网址应该没问题。

我在网上搜索但我无法找到我的代码错误...有什么建议吗?

2 个答案:

答案 0 :(得分:2)

Confluence在“X-Atlassian-Token”标头值中是严格的。标题名称中有额外的

更改

 post.addHeader("X-Atlassian-Token:","no-check");

 post.addHeader("X-Atlassian-Token","no-check");

这将创建正确的标题,404错误将消失

答案 1 :(得分:0)

我现在也在努力添加附件;现在进入403 Forbidden ...

我通过我的404发现它在我的(坏)URL中。但是,您的网址确实显示正确。页面ID关闭或经过身份验证的用户是否缺少权限(全局,空间或页面级别)?

编辑:哦!并且,正如mtheriault的帖子建议的那样,检查附件大小与Confluence实例的“attachmentMaxSize”相比。