[英]Request MultiPart/Form-Data with ByteArray
i want to send a request includes bytearray as form-data.我想发送一个请求,包括 bytearray 作为表单数据。 Everyone using "File" but i have just the "bytearray" and i don't want to use any path.
每个人都使用“文件”,但我只有“字节数组”,我不想使用任何路径。 My request in curl:
我在 curl 中的要求:
curl -F file=@file server
In java what i tried:在 java 我尝试了什么:
byte[] fileByte = Base64.decodeBase64(parameter);
ByteArrayInputStream myFile = new ByteArrayInputStream(fileByte);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("server");
multipartEntityBuilder.addBinaryBody("file", myFile, ContentType.APPLICATION_OCTET_STREAM, "filename");
HttpEntity multipart = multipartEntityBuilder.build();
httpPost.setEntity(multipart);
and i tried我试过了
multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");
//OR FileBody, ByteArrayEntity, InputStream or like thats
So, that methods not worked for me.所以,这些方法对我不起作用。 How i can send the request successfully?
我怎样才能成功发送请求?
EDIT: i used the ptsv2 with postman and result ->编辑:我将ptsv2与postman和结果一起使用->
when i send the same request and file with java the result ->当我使用 java 发送相同的请求和文件时,结果->
i think issue is certainly related to the ByteArray or InputStream.我认为问题肯定与 ByteArray 或 InputStream 有关。 I must find the another Type for the my byte[] or right method for post in java with using the File type but without path.
我必须在 java 中找到我的字节 [] 的另一种类型或正确的方法,使用文件类型但没有路径。
I don't know what "server" is, but maybe the problem is with the server you are sending to.我不知道“服务器”是什么,但问题可能出在您发送到的服务器上。 I ran your code, writing to a public HTTP POST test server available on the internet, and it works fine for me.
我运行了您的代码,将其写入 Internet 上可用的公共 HTTP POST 测试服务器,它对我来说很好用。 Here's the code I ran:
这是我运行的代码:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
class ListsTest {
public static void main(String[] args) {
byte[] fileByte = "Sample string data".getBytes(StandardCharsets.UTF_8);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://ptsv2.com/t/r7ypk-1613291354/post");
multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");
HttpEntity multipart = multipartEntityBuilder.build();
httpPost.setEntity(multipart);
try {
client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
}
}
If you go to the target test server bucket:如果你 go 到目标测试服务器存储桶:
https://ptsv2.com/t/r7ypk-1613291354
you can see what the full request looks like as received by the server, and see that the payload makes it over just fine as a file with the appropriate filename and contents.您可以看到服务器接收到的完整请求的样子,并看到有效负载将其作为具有适当文件名和内容的文件很好地覆盖。 The file content portion of the request display looks like this:
请求显示的文件内容部分如下所示:
Your alternate method worked equally well for me.您的替代方法对我同样有效。 I chose to post this version as it is simpler, not requiring you to wrap your byte array in an InputStream object.
我选择发布此版本是因为它更简单,不需要您将字节数组包装在 InputStream object 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.