如何在java中使用httpclient发送png文件
两个命令都成功运行,但第二个curl响应显示了无效的位置,第一个curl显示了图像的有效位置,并且当导航到图像所需的相同位置时 但是,当导航到显示的位置时,使用第二个如何在java中使用httpclient发送png文件,java,Java,两个命令都成功运行,但第二个curl响应显示了无效的位置,第一个curl显示了图像的有效位置,并且当导航到图像所需的相同位置时 但是,当导航到显示的位置时,使用第二个 [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/e
[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'
当提供绝对路径而不是@image.jpg时,第二个有什么问题
我还需要在java中使用下面的代码自动执行上面的操作,它会被传递,但是当导航到响应中接收到的位置并显示上面的错误“它包含错误”时,使用它发送的图像会被破坏
"The image “https://location” cannot be displayed because it contains errors."
可以使用.bat文件执行此操作。我以前也遇到过同样的问题,并使用.bat文件解决了它 您还可以使用bat脚本传递参数。下面是示例。只需使用.bat保存此文件并使用下面的java代码即可。它应该会起作用。但是,您可以相应地更改.bat脚本 “test.bat”: “执行上述.bat脚本的Java代码”:
这里至少有两个独立的问题,如果没有关于服务器功能的详细信息,这两个问题都无法回答。我提供了代码以及curl命令,请告诉我您还需要什么您正在询问关于curl的问题(指定文件时需要
@
。否则,只需包含数据,而不需要这样做。)你的另一个问题是关于用java发出http post请求。这是两个完全不同的问题。我已经提到,我需要用java自动执行上面的curl。我只是提供了全部信息,以便可以清除所有内容。仍在等待答案。你的curl问题是否已排序?对于你的java问题,什么给了你答案错误?当您尝试查看图像之后?您在<代码>响应< /代码>对象中得到了什么?<代码> SETMODE调用< /代码>?您是否考虑过尝试,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);
}
@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%"
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();