我正在尝试使用apache http client v4.x。
对于特定情况,我必须进行多部分表单请求...
多部分表单请求在java中使用以下代码生成 -
HttpPost httppost = new HttpPost("http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample");
FileBody bin = new FileBody(new File(args[0]));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
就我而言,我有一个文件正文的字节流,
byte[] bytes = value.toBinary(charset);
现在我想将这个文件(字节流)添加到multipart实体中,这样我就可以发出一个多部分的帖子请求......我该怎么做?
答案 0 :(得分:4)
byte[] bytes = value.toBinary(charset);
ContentBody bin = new ByteArrayBody(bytes, "myfile.dat");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);