當前位置: 首頁>>代碼示例>>Java>>正文


Java MultipartEntityBuilder.setContentType方法代碼示例

本文整理匯總了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();
    }
}
 
開發者ID:http-builder-ng,項目名稱:http-builder-ng,代碼行數:47,代碼來源:ApacheEncoders.java

示例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);
}
 
開發者ID:BotMill,項目名稱:fb-botmill,代碼行數:63,代碼來源:FbBotMillNetworkController.java


注:本文中的org.apache.http.entity.mime.MultipartEntityBuilder.setContentType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。