[1] curl -v -X POST "someip/emailtemplate?mimeType=image/png" -H 'content-type: application/octet-stream' -k --data-binary '@image.jpg'
[2] curl -v -X POST "someip/emailtemplate?mimeType=image/png" -H 'content-type: application/octet-stream' -k --data-binary 'D:/myfolder/image.jpg'
兩個命令發送png文件運行成功,但第二捲曲反應表明這是無效的,第一個捲曲的位置顯示圖像的導航到相同的有效位置,它需要的圖像時。如何使用的HttpClient在Java
然而,隨着第二,當導航到位置時,它顯示
"The image 「https://location」 cannot be displayed because it contains errors."
提供的絕對路徑,而不是@ image.jpg的時候出了什麼問題:第二個
我還需要在Java自動化以上,用下面的代碼,它獲得通過,但發送圖像使用它的時候打破瀏覽到所收到的位置,並顯示上述錯誤「內有錯誤,」
public void sendPostForTemplate() throws Exception {
file = new File(directoryPath + "/path/Test.png");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
post = new HttpPost(URL);
client = HttpClientBuilder.create().build();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("my_file", fileBody);
builder.addBinaryBody("upstream", file, ContentType.DEFAULT_BINARY, "Test.png");
post.setHeader("User-Agent", USER_AGENT);
post.addHeader("Content-Type", "application/octet-stream");
entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
}
至少有兩個獨立的問題在這裏,也不是沒有一些細節交代關於服務器的功能。 – EJP
我提供了代碼以及curl命令,請讓我知道您還需要什麼 –
您正在問一個關於curl的問題(當您指定文件時需要'@''',否則您只需包含數據,不想做。[ref](https://curl.haxx.se/docs/manpage.html))你的另一個問題是關於用java發出一個http post請求。他們是兩個完全不同的問題。 – matt