本文整理汇总了Java中org.apache.http.entity.mime.MultipartEntityBuilder.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartEntityBuilder.setContentType方法的具体用法?Java MultipartEntityBuilder.setContentType怎么用?Java MultipartEntityBuilder.setContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.mime.MultipartEntityBuilder
的用法示例。
在下文中一共展示了MultipartEntityBuilder.setContentType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: multipart
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* Encodes multipart/form-data where the body content must be an instance of the {@link MultipartContent} class. Individual parts will be
* encoded using the encoders available to the {@link ChainedHttpConfig} object.
*
* @param config the chained configuration object
* @param ts the server adapter
*/
public static void multipart(final ChainedHttpConfig config, final ToServer ts) {
try {
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
final Object body = request.actualBody();
if (!(body instanceof MultipartContent)) {
throw new IllegalArgumentException("Multipart body content must be MultipartContent.");
}
final String contentType = request.actualContentType();
if (!(contentType.equals(MULTIPART_FORMDATA.getAt(0)) || contentType.equals(MULTIPART_MIXED.getAt(0)))) {
throw new IllegalArgumentException("Multipart body content must be multipart/form-data.");
}
final String boundary = randomString(10);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().setBoundary(boundary);
final String boundaryContentType = "multipart/form-data; boundary=" + boundary;
entityBuilder.setContentType(ContentType.parse(boundaryContentType));
for (final MultipartContent.MultipartPart mpe : ((MultipartContent) body).parts()) {
if (mpe.getFileName() == null) {
entityBuilder.addTextBody(mpe.getFieldName(), (String) mpe.getContent());
} else {
final byte[] encodedBytes = EmbeddedEncoder.encode(config, mpe.getContentType(), mpe.getContent());
entityBuilder.addBinaryBody(mpe.getFieldName(), encodedBytes, parse(mpe.getContentType()), mpe.getFileName());
}
}
request.setContentType(boundaryContentType);
ts.toServer(entityBuilder.build().getContent());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
示例2: postFormDataMessage
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* POSTs a message as a JSON string to Facebook.
*
* @param recipient
* the recipient
* @param type
* the type
* @param file
* the file
*/
public static void postFormDataMessage(String recipient,
AttachmentType type, File file) {
String pageToken = FbBotMillContext.getInstance().getPageToken();
// If the page token is invalid, returns.
if (!validatePageToken(pageToken)) {
return;
}
// TODO: add checks for valid attachmentTypes (FILE, AUDIO or VIDEO)
HttpPost post = new HttpPost(
FbBotMillNetworkConstants.FACEBOOK_BASE_URL
+ FbBotMillNetworkConstants.FACEBOOK_MESSAGES_URL
+ pageToken);
FileBody filedata = new FileBody(file);
StringBody recipientPart = new StringBody("{\"id\":\"" + recipient
+ "\"}", ContentType.MULTIPART_FORM_DATA);
StringBody messagePart = new StringBody("{\"attachment\":{\"type\":\""
+ type.name().toLowerCase() + "\", \"payload\":{}}}",
ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.STRICT);
builder.addPart("recipient", recipientPart);
builder.addPart("message", messagePart);
// builder.addPart("filedata", filedata);
builder.addBinaryBody("filedata", file);
builder.setContentType(ContentType.MULTIPART_FORM_DATA);
// builder.setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
HttpEntity entity = builder.build();
post.setEntity(entity);
// Logs the raw JSON for debug purposes.
BufferedReader br;
// post.addHeader("Content-Type", "multipart/form-data");
try {
// br = new BufferedReader(new InputStreamReader(
// ())));
Header[] allHeaders = post.getAllHeaders();
for (Header h : allHeaders) {
logger.debug("Header {} -> {}", h.getName(), h.getValue());
}
// String output = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
// postInternal(post);
}