2017-04-24 81 views
-1
[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); 
} 
+0

至少有兩個獨立的問題在這裏,也不是沒有一些細節交代關於服務器的功能。 – EJP

+0

我提供了代碼以及curl命令,請讓我知道您還需要什麼 –

+0

您正在問一個關於curl的問題(當您指定文件時需要'@''',否則您只需包含數據,不想做。[ref](https://curl.haxx.se/docs/manpage.html))你的另一個問題是關於用java發出一個http post請求。他們是兩個完全不同的問題。 – matt

回答

1

您可以使用.bat文件來執行此操作。我以前遇到同樣的問題,並使用.bat文件解決它。

您也可以使用bat腳本傳遞參數。這裏是示例示例。只需使用.bat保存並使用下面的java代碼即可。它應該工作。但是,您可以相應地更改.bat腳本。

「下的test.bat」:

@echo off 

SET firstparameter=%1 
SET secondparameter=%2 
ECHO %firstparameter% 
ECHO %secondparameter% 

curl -v -X POST "url/v1/media/%secondparameter%/emailtemplate?mimeType=image/png" -H "Content-Type: application/octet-stream" -k --data-binary "@image.png" -H "%firstparameter%" 

「Java代碼來執行的.bat腳本上面」:

String filePath="\\test.bat"; 
File pathFile = new File(filePath); 
Process process=Runtime.getRuntime().exec(new String[]{String.valueOf(pathFile),"parameter1","parameter2"}); 
BufferedReader read = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
ost = new FileOutputStream(outputFile); 
String line; 
while ((line = read.readLine()) != null) { 
System.out.println("output is "+line); 
} 
read.close(); 
+0

嗨@vish,它工作。非常感謝。 +1 –