asd*_*asd 12 java apache scala multipartform-data httpclient
我正在使用Apache HTTPClient 4.我正在做非常正常的多部分这样的事情:
val entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("filename", new FileBody(new File(fileName), "application/zip").asInstanceOf[ContentBody])
entity.addPart("shared", new StringBody(sharedValue, "text/plain", Charset.forName("UTF-8")));
val post = new HttpPost(uploadUrl);
post.setEntity(entity);
Run Code Online (Sandbox Code Playgroud)
我希望在发送之前看到实体的内容(或发布,等等).但是,没有实现该特定方法:
entity.getContent() // not defined for MultipartEntity
Run Code Online (Sandbox Code Playgroud)
我如何看到我发布的内容?
Lac*_*ing 16
使用org.apache.http.entity.mime.MultipartEntity writeTo(java.io.OutputStream)方法将内容写入java.io.OutputStream,然后将该流转换为String或byte[]:
// import java.io.ByteArrayOutputStream;
// import org.apache.http.entity.mime.MultipartEntity;
// ...
// MultipartEntity entity = ...;
// ...
ByteArrayOutputStream out = new ByteArrayOutputStream(entity.getContentLength());
// write content to stream
entity.writeTo(out);
// either convert stream to string
String string = out.toString();
// or convert stream to bytes
byte[] bytes = out.toByteArray();
Run Code Online (Sandbox Code Playgroud)
注意:这仅适用于小于2Gb的多部分实体,Java中字节数组的最大大小,以及足以读入内存的小部分实体.
你不知道内容吗?虽然,您正在StringBody通过提供来构建sharedValue. 那么,它怎么可能不同于sharedValue。