本文整理汇总了Java中org.apache.http.entity.mime.MultipartEntityBuilder.build方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartEntityBuilder.build方法的具体用法?Java MultipartEntityBuilder.build怎么用?Java MultipartEntityBuilder.build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.mime.MultipartEntityBuilder
的用法示例。
在下文中一共展示了MultipartEntityBuilder.build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getHttpEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
@Override
public HttpEntity getHttpEntity() {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if (url != null)
builder.addTextBody(URL_FIELD, url);
if (certificate != null)
builder.addBinaryBody(CERTIFICATE_FIELD, new File(certificate));
if (maxConnections != null)
builder.addTextBody(MAX_CONNECTIONS_FIELD, maxConnections.toString());
if (allowedUpdates != null) {
if (allowedUpdates.length == 0) {
builder.addTextBody(ALLOWED_UPDATES_FIELD, "[]");
} else {
for (String allowedUpdate : allowedUpdates) {
builder.addTextBody(ALLOWED_UPDATES_FIELD + "[]", allowedUpdate);
}
}
}
return builder.build();
}
示例2: uploadWelcomeMessage
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public ApiResponse uploadWelcomeMessage(String filePath, int messageId, int selectedMessageId) {
File file = new File(filePath);
HttpPost uploadRequest = new HttpPost(WELCOME_MESSAGE_URI);
StringBody commandeBody = new StringBody("annonce_mess", ContentType.MULTIPART_FORM_DATA);
StringBody messageIdBody = new StringBody(String.valueOf(messageId), ContentType.MULTIPART_FORM_DATA);
StringBody maxFileSizeBody = new StringBody("5242880", ContentType.MULTIPART_FORM_DATA);
StringBody selectMessageBody = new StringBody(String.valueOf(selectedMessageId),
ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("FILE", file, ContentType.create("audio/mp3"), "message.mp3");
builder.addPart("commande", commandeBody);
builder.addPart("id_message", messageIdBody);
builder.addPart("id_message_select", selectMessageBody);
builder.addPart("MAX_FILE_SIZE", maxFileSizeBody);
HttpEntity entity = builder.build();
uploadRequest.setEntity(entity);
HttpResponse response = executeRequest(uploadRequest);
return new ApiResponse(HttpStatus.gethttpStatus(response.getStatusLine().getStatusCode()));
}
示例3: createMultipartFormEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public static HttpEntity createMultipartFormEntity(Map<String, String> parameters, InputStream is) {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
multipartEntityBuilder.addBinaryBody("file", is, ContentType.create("application/octet-stream"), "file");
for (Entry<String, String> entry : parameters.entrySet()) {
multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue());
}
return multipartEntityBuilder.build();
}
示例4: createMultipartEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* Create the required multipart entity
* @param uploadId Session ID
* @return Entity to submit to the upload
* @throws ClientProtocolException
* @throws IOException
*/
protected HttpEntity createMultipartEntity(String uploadId) throws ClientProtocolException, IOException {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("upload_id", uploadId);
builder.addTextBody("_uuid", api.getUuid());
builder.addTextBody("_csrftoken", api.getOrFetchCsrf());
builder.addTextBody("media_type", "2");
builder.setBoundary(api.getUuid());
HttpEntity entity = builder.build();
return entity;
}
示例5: createFileEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private HttpEntity createFileEntity(Object files) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (Entry<String, Object> entry : ((Map<String, Object>) files).entrySet()) {
if (new File(entry.getValue().toString()).exists()) {
builder.addPart(entry.getKey(),
new FileBody(new File(entry.getValue().toString()), ContentType.DEFAULT_BINARY));
} else {
builder.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), ContentType.DEFAULT_TEXT));
}
}
return builder.build();
}
示例6: sendFormToDLMS
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* * Send POST request to DLMS back end with the result file
* @param bluemixToken - the Bluemix token
* @param contents - the result file
* @param jobUrl - the build url of the build job in Jenkins
* @param timestamp
* @return - response/error message from DLMS
*/
public String sendFormToDLMS(String bluemixToken, FilePath contents, String lifecycleStage, String jobUrl, String timestamp) throws IOException {
// create http client and post method
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost postMethod = new HttpPost(this.dlmsUrl);
postMethod = addProxyInformation(postMethod);
// build up multi-part forms
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if (contents != null) {
File file = new File(root, contents.getName());
FileBody fileBody = new FileBody(file);
builder.addPart("contents", fileBody);
builder.addTextBody("test_artifact", file.getName());
if (this.isDeploy) {
builder.addTextBody("environment_name", environmentName);
}
//Todo check the value of lifecycleStage
builder.addTextBody("lifecycle_stage", lifecycleStage);
builder.addTextBody("url", jobUrl);
builder.addTextBody("timestamp", timestamp);
String fileExt = FilenameUtils.getExtension(contents.getName());
String contentType;
switch (fileExt) {
case "json":
contentType = CONTENT_TYPE_JSON;
break;
case "xml":
contentType = CONTENT_TYPE_XML;
break;
default:
return "Error: " + contents.getName() + " is an invalid result file type";
}
builder.addTextBody("contents_type", contentType);
HttpEntity entity = builder.build();
postMethod.setEntity(entity);
postMethod.setHeader("Authorization", bluemixToken);
} else {
return "Error: File is null";
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(postMethod);
// parse the response json body to display detailed info
String resStr = EntityUtils.toString(response.getEntity());
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(resStr);
if (!element.isJsonObject()) {
// 401 Forbidden
return "Error: Upload is Forbidden, please check your org name. Error message: " + element.toString();
} else {
JsonObject resJson = element.getAsJsonObject();
if (resJson != null && resJson.has("status")) {
return String.valueOf(response.getStatusLine()) + "\n" + resJson.get("status");
} else {
// other cases
return String.valueOf(response.getStatusLine());
}
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
示例7: buildMultiPartEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* @param buildMap
* @param partParam
*
* @return HttpEntity
*/
public static HttpEntity buildMultiPartEntity(Map<String, String> buildMap, Map<String, ContentBody> partParam) {
if (MapUtils.isEmpty(buildMap)) {
return null;
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
buildMap.forEach((k, v) -> builder.addTextBody(k, v));
if (MapUtils.isNotEmpty(partParam)) {
partParam.forEach((k, v) -> builder.addPart(k, v));
}
return builder.build();
}
示例8: constructRequestBody
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
private void constructRequestBody() {
// we have work to do here only when using multipart body
if (requestBodyParts.size() > 0) {
try {
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
for (HttpBodyPart part : requestBodyParts) {
entityBuilder.addPart(part.getName(), part.constructContentBody());
}
requestBody = entityBuilder.build();
} catch (Exception e) {
throw new HttpException("Exception trying to create a multipart message.", e);
}
}
}
示例9: uploadFileToWeedFS
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
@Override
public String uploadFileToWeedFS(String url, InputStream input) throws IllegalStateException {
HttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
// multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addBinaryBody("fileName", input);
HttpEntity entity = multipartEntity.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
String statusCode = String.valueOf(response.getStatusLine().getStatusCode());
if (statusCode.indexOf("20") == 0) {
entity = response.getEntity();
return StrUtil.readStream(entity.getContent(), responseContextEncode);
} else if (statusCode.indexOf("40") == 0) {
LOG.error("Page: " + url + " no find");
return "Page no find";
} else {
LOG.error("返回状态码:[" + statusCode + "]");
return "返回状态码:[" + statusCode + "]";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return null;
}
示例10: uploadFile
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public WebResponse uploadFile(String path, String fname, InputStream in,
String stoken) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost(path);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody fn = new StringBody(fname, ContentType.MULTIPART_FORM_DATA);
builder.addPart("fname", fn);
builder.addBinaryBody("file", in, ContentType.APPLICATION_XML, fname);
BasicCookieStore cookieStore = new BasicCookieStore();
if (stoken != null) {
BasicClientCookie cookie = new BasicClientCookie(
Constants.SECURE_TOKEN_NAME, stoken);
cookie.setDomain(TestConstants.JETTY_HOST);
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
TestConstants.LOG.debug("stoken=" + stoken);
HttpClient client = HttpClientBuilder.create().
setDefaultCookieStore(cookieStore).build();
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
String body;
ResponseHandler<String> handler = new BasicResponseHandler();
try {
body = handler.handleResponse(response);
} catch (HttpResponseException e) {
return new WebResponse(e.getStatusCode(), e.getMessage());
}
return new WebResponse(response.getStatusLine().getStatusCode(), body);
}
示例11: send
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
@Override
protected CloseableHttpResponse send(CloseableHttpClient httpClient, String baseUrl) throws Exception {
//1)构建实体
MultipartEntityBuilder entBuilder = MultipartEntityBuilder.create();
for (String key : params.keySet()) {
Object item = params.get(key);
if(item instanceof File){
File file = (File)item;
if((!file.exists()) || (file.isDirectory())){
throw new Exception("file error");
}
entBuilder.addPart(key, new FileBody(file));
}else if(item instanceof String){
String value = (String)item;
entBuilder.addPart(key, new StringBody(value, ContentType.TEXT_PLAIN));
}else{
throw new Exception(item.getClass().toString()+" not support");
}
}
HttpEntity reqEntity = entBuilder.build();
//2)发送并等待回复
HttpPost request = new HttpPost(baseUrl);
request.setEntity(reqEntity);
return httpClient.execute(request);
}
示例12: getResponseJsonObject
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
private JSONObject getResponseJsonObject(String httpMethod, String url, Object params, String imageName, byte[] fileData) throws IOException, MtWmErrorException {
String fullUrl = getBaseApiUrl() + url;
List<NameValuePair> sysNameValuePairs = getSysNameValuePairs(fullUrl, params);
List<NameValuePair> nameValuePairs = getNameValuePairs(params);
HttpPost httpPost = new HttpPost(fullUrl + "?" + URLEncodedUtils.format(sysNameValuePairs, UTF_8));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (NameValuePair p : nameValuePairs) {
builder.addTextBody(p.getName(), p.getValue(), ContentType.TEXT_PLAIN.withCharset(UTF_8));
}
builder.addPart("img_data", new ByteArrayBody(fileData, imageName));
final HttpEntity entity = builder.build();
httpPost.addHeader(entity.getContentType());
httpPost.setEntity(entity);
CloseableHttpResponse response = this.httpClient.execute(httpPost);
String resultContent = new BasicResponseHandler().handleResponse(response);
JSONObject jsonObject = JSON.parseObject(resultContent);
MtWmError error = MtWmError.fromJson(jsonObject);
if (error != null) {
logging(url, httpMethod, false, httpPost.getURI() + "\nBody:" + JSON.toJSONString(params), resultContent);
throw new MtWmErrorException(error.getErrorCode(), error.getErrorMsg());
}
logging(url, httpMethod, true, httpPost.getURI() + "\nBody:" + JSON.toJSONString(params), resultContent);
return jsonObject;
}
示例13: getMultiPartEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public static HttpEntity getMultiPartEntity(String fileName, String contentType, InputStream fileStream, Map<String, String> additionalFormFields) throws IOException {
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
if (additionalFormFields != null && !additionalFormFields.isEmpty()) {
for (Entry<String, String> field : additionalFormFields.entrySet()) {
entityBuilder.addTextBody(field.getKey(), field.getValue());
}
}
entityBuilder.addBinaryBody(fileName, IOUtils.toByteArray(fileStream), ContentType.create(contentType), fileName);
return entityBuilder.build();
}
示例14: startSuiteExecution
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
private SuiteExecutionResult startSuiteExecution(File testSuite) {
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create()
.addBinaryBody("suite", testSuite, ContentType.APPLICATION_XML, testSuite.getName());
if (domain != null) {
entityBuilder.addTextBody("domain", domain);
}
if (patternCorrelationId != null) {
entityBuilder.addTextBody("pattern", patternCorrelationId);
}
HttpEntity entity = entityBuilder.build();
return retrieveSuiteExecutionResult(entity, timeout);
}
示例15: makeMultipartEntity
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public static HttpEntity makeMultipartEntity(List<NameValuePair> params, final Map<String, File> files) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //如果有SocketTimeoutException等情况,可修改这个枚举
//builder.setCharset(Charset.forName("UTF-8")); //不要用这个,会导致服务端接收不到参数
if (params != null && params.size() > 0) {
for (NameValuePair p : params) {
builder.addTextBody(p.getName(), p.getValue(), ContentType.TEXT_PLAIN.withCharset("UTF-8"));
}
}
if (files != null && files.size() > 0) {
Set<Entry<String, File>> entries = files.entrySet();
for (Entry<String, File> entry : entries) {
builder.addPart(entry.getKey(), new FileBody(entry.getValue()));
}
}
return builder.build();
}