我怎么能看到一个MultipartForm请求的内容? [英] How can I See the content of a MultipartForm request?

查看:183
本文介绍了我怎么能看到一个MultipartForm请求的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache了HTTPClient 4.我做的非常正常的多部分的东西是这样的:

I am using Apache HTTPClient 4. I am doing very normal multipart stuff like this:

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);

我想看到的实体内容(或交的,等等)之前,我把它。然而,该特定方法未执行

I want to see the contents of the entity (or post, whatever) before I send it. However, that specific method is not implemented:

entity.getContent() // not defined for MultipartEntity

怎么可以看到我张贴?

How can I see what I am posting?

推荐答案

使用 org.apache.http.entity.mime.MultipartEntity 的writeTo(java.io.OutputStream中)方法来写的内容到 java.io.OutputStream中,然后将该数据流转换为一个字符串字节[]

Use the org.apache.http.entity.mime.MultipartEntity writeTo(java.io.OutputStream) method to write the content to an java.io.OutputStream, and then convert that stream to a String or 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();

请注意:这仅适用于多部分实体都超过2GB,在Java字节数组的最大尺寸,足够小,小到读入内存

Note: this only works for multipart entities both smaller than 2Gb, the maximum size of a byte array in Java, and small enough to be read into memory.

这篇关于我怎么能看到一个MultipartForm请求的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆