2016-09-27 64 views
0

我想將圖像發佈到作爲附件的內容的合流頁面。用Java和Apache發佈附件到confluence時得到了404 HttpClient

這是我的Java應用程序的功能:

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 

然後

HttpResponseProxy{HTTP/1.1 404 Not Found [Server: Apache-Coyote/1.1, X-ASEN: SEN-1343236, Set-Cookie: JSESSIONID=9DF46011711C2828977E17A945D023E1; Path=/; Secure; HttpOnly, X-Seraph-LoginReason: OK, X-AUSERNAME: xxxx, X-Content-Type-Options: nosniff, Content-Type: text/plain, Transfer-Encoding: chunked, Date: Tue, 27 Sep 2016 11:20:35 GMT] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}} 

(I改變了域名和用戶名只是這個職位..)

所以一切似乎都沒問題。如果我複製生成的POST URL並在瀏覽器中執行GET操作,我會得到一個帶附件列表的json片段,之前手動上傳。所以POST網址應該沒問題。

我在網上搜索,但我找不到我的代碼錯在哪裏..任何建議?

+0

從文檔: 狀態404:返回,如果請求的內容不發現該用戶沒有查看權限,或者附件超過了最大配置的附件大小。 https://docs.atlassian.com/confluence/REST/latest/#content/{id}/child/attachment-createAttachments – mtheriault

+0

非常感謝您的評論。使用同一個用戶,我可以在Web界面中手動創建相同的附件。我的約150 kb大..所以不幸我不認爲這是問題。我還使用另一個(第三方)應用程序生成相同的內容,所以我不認爲它的權限或附件大小問題。 – DaHopi

+0

好奇心,你能夠使用GET獲取頁面 - https:// xxx.xxxx.de:443/rest/api/content/38262140? – mtheriault

回答

0

我也在努力添加附件;現在到一個403禁止...

我工作通過我的404發現它是在我的(壞)網址。但是,您的網址確實顯示正確。頁面標識已關閉或您的已認證用戶缺乏權限(全局,空間或頁面級別)的任何機會?

編輯:哦!而且,正如mtheriault的帖子所示,請檢查附件的大小,以查看您的Confluence實例的「attachmentMaxSize」。

+0

感謝您的評論,但與同一個用戶,我可以創建與專有的第三方應用程序的內容。所以我不認爲這是任何權限或附件大小問題。 – DaHopi

1

Confluence對「X-Atlassian-Token」標頭值是嚴格的。標題名稱中有額外的

變化

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

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

這將創建正確的標題,404錯誤就會消失

相關問題