本文整理汇总了Java中org.apache.http.entity.mime.MultipartEntityBuilder.setCharset方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartEntityBuilder.setCharset方法的具体用法?Java MultipartEntityBuilder.setCharset怎么用?Java MultipartEntityBuilder.setCharset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.mime.MultipartEntityBuilder
的用法示例。
在下文中一共展示了MultipartEntityBuilder.setCharset方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initRequest
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public HttpPost initRequest(final FileUploadParameter parameter) {
final FileUploadAddress fileUploadAddress = getDependResult(FileUploadAddress.class);
final HttpPost request = new HttpPost("http://" + fileUploadAddress.getData().getUp() + CONST.URI_PATH);
final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setCharset(Consts.UTF_8);
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart(CONST.QID_NAME, new StringBody(getLoginInfo().getQid(), ContentType.DEFAULT_BINARY));
multipartEntity.addPart("ofmt", new StringBody("json", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("method", new StringBody("Upload.web", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("token", new StringBody(readCookieStoreValue("token"), ContentType.DEFAULT_BINARY));
multipartEntity.addPart("v", new StringBody("1.0.1", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("tk", new StringBody(fileUploadAddress.getData().getTk(), ContentType.DEFAULT_BINARY));
multipartEntity.addPart("Upload", new StringBody("Submit Query", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("devtype", new StringBody("web", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("pid", new StringBody("ajax", ContentType.DEFAULT_BINARY));
multipartEntity.addPart("Filename",
new StringBody(parameter.getUploadFile().getName(), ContentType.APPLICATION_JSON));
multipartEntity.addPart("path", new StringBody(parameter.getPath(), ContentType.APPLICATION_JSON));// 解决中文不识别问题
multipartEntity.addBinaryBody("file", parameter.getUploadFile());
request.setEntity(multipartEntity.build());
return request;
}
示例2: main
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
public static void main(String args[]){
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/sso-web/upload");
httpPost.addHeader("Range","bytes=10000-");
final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setCharset(Consts.UTF_8);
multipartEntity.setMode(HttpMultipartMode.STRICT);
multipartEntity.addBinaryBody("file", new File("/Users/huanghuanlai/Desktop/test.java"));
httpPost.setEntity(multipartEntity.build());
try {
HttpResponse response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
}
示例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: postBodyAsMultipart
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
/**
* 发送body为multipart form
* sample
*/
public static String postBodyAsMultipart(String url,Map<String,ContentBody> contentBodies,String charset) throws Exception{
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder mb = MultipartEntityBuilder.create();
mb.setCharset(Charset.forName(charset));
for(Map.Entry<String,ContentBody> entry : contentBodies.entrySet()) {
mb.addPart(entry.getKey(),entry.getValue());
}
httpPost.setEntity(mb.build());
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
}
EntityUtils.consume(entity);
return result;
} finally {
response.close();
}
}
示例5: postFile
import org.apache.http.entity.mime.MultipartEntityBuilder; //导入方法依赖的package包/类
private Result postFile(String url, List<String[]> datas, File file) {
if (!file.exists()) {
throw new JapiException(file.getAbsolutePath() + " file not exist.");
}
HttpPost httpPost = new HttpPost(url);
Integer tryCount = 0;
while (reties > tryCount) {
try {
final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setCharset(Charset.forName("utf-8"));
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (String[] nameValue : datas) {
multipartEntity.addPart(nameValue[0], new StringBody(nameValue[1], ContentType.APPLICATION_JSON));
}
multipartEntity.addBinaryBody("file", file);
httpPost.setEntity(multipartEntity.build());
httpPost.setHeader("token", token);
String result = EntityUtils.toString(HTTP_CLIENT.execute(httpPost).getEntity());
Result result1 = JSON.parseObject(result, ResultImpl.class);
if (result1.getCode() != 0) {
throw new JapiException(result1.getMsg());
}
return result1;
} catch (IOException e) {
if (e instanceof HttpHostConnectException) {
tryCount++;
LOGGER.warn("try connect server " + tryCount + " count.");
try {
TimeUnit.SECONDS.sleep(tryTime);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
if (tryCount <= reties) {
LOGGER.error("server connect failed.");
}
return null;
}