本文整理汇总了Java中org.apache.http.entity.mime.MultipartEntity.addPart方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartEntity.addPart方法的具体用法?Java MultipartEntity.addPart怎么用?Java MultipartEntity.addPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.mime.MultipartEntity
的用法示例。
在下文中一共展示了MultipartEntity.addPart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: postData
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private static int postData(String url, byte[] encryptedBytes, byte[] encryptedSecretKey, byte[] signature, String serviceName) throws IOException {
MultipartEntity multipartEntity = new MultipartEntity();
String filename=serviceName+'_'+System.currentTimeMillis()+".hl7";
multipartEntity.addPart("importFile", new ByteArrayBody(encryptedBytes, filename));
multipartEntity.addPart("key", new StringBody(new String(Base64.encodeBase64(encryptedSecretKey), MiscUtils.DEFAULT_UTF8_ENCODING)));
multipartEntity.addPart("signature", new StringBody(new String(Base64.encodeBase64(signature), MiscUtils.DEFAULT_UTF8_ENCODING)));
multipartEntity.addPart("service", new StringBody(serviceName));
multipartEntity.addPart("use_http_response_code", new StringBody("true"));
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(multipartEntity);
HttpClient httpClient = getTrustAllHttpClient();
httpClient.getParams().setParameter("http.connection.timeout", CONNECTION_TIME_OUT);
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode=httpResponse.getStatusLine().getStatusCode();
logger.debug("StatusCode:" + statusCode);
return (statusCode);
}
示例2: composeMultiPartFormRequest
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private HttpPost composeMultiPartFormRequest(final String uri, final Parameters parameters, final Map<String, ContentBody> files) {
HttpPost request = new HttpPost(uri);
MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
Charset utf8 = Charset.forName("UTF-8");
for(Parameter param : parameters)
if(param.isSingleValue())
multiPartEntity.addPart(param.getName(), new StringBody(param.getValue(), utf8));
else
for(String value : param.getValues())
multiPartEntity.addPart(param.getName(), new StringBody(value, utf8));
} catch (UnsupportedEncodingException e) {
throw MechanizeExceptionFactory.newException(e);
}
List<String> fileNames = new ArrayList<String>(files.keySet());
Collections.sort(fileNames);
for(String name : fileNames) {
ContentBody contentBody = files.get(name);
multiPartEntity.addPart(name, contentBody);
}
request.setEntity(multiPartEntity);
return request;
}
示例3: fileUpload
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private static void fileUpload() throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(postURL);
file = new File("h:/Sakura haruno.jpg");
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("Filename", new StringBody(file.getName()));
mpEntity.addPart("Filedata", cbFile);
// mpEntity.addPart("server", new StringBody(server));
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
System.out.println("Now uploading your file into ugotfile.com");
HttpResponse response = httpclient.execute(httppost);
System.out.println(response.getStatusLine());
if (response != null) {
uploadresponse = EntityUtils.toString(response.getEntity());
}
System.out.println("Upload Response : " + uploadresponse);
downloadlink = parseResponse(uploadresponse, "[\"", "\"");
downloadlink = downloadlink.replaceAll("\\\\/", "/");
deletelink = parseResponse(uploadresponse, "\",\"", "\"");
deletelink = deletelink.replaceAll("\\\\/", "/");
System.out.println("Download Link : " + downloadlink);
System.out.println("Delete Link : " + deletelink);
}
示例4: upload
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private void upload() throws IOException, Exception {
httpPost = new NUHttpPost(uploadUrl);
MultipartEntity mpEntity = new MultipartEntity();
mpEntity.addPart("file", createMonitoredFileBody());
httpPost.setEntity(mpEntity);
uploading();
httpResponse = httpclient.execute(httpPost, httpContext);
gettingLink();
stringResponse = EntityUtils.toString(httpResponse.getEntity());
jsonObject = new JSONObject(stringResponse);
if(jsonObject.has("error")){
//Handle errors
status = UploadStatus.GETTINGERRORS;
throw new Exception(jsonObject.getString("error"));
}
else{
downURL = "http://sharesend.com/" + jsonObject.getString("token");
}
//FileUtils.saveInFile("ShareSend.html", stringResponse);
}
示例5: fileUpload
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
public static void fileUpload() throws IOException {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(postURL);
File file = new File("d:/hai.html");
System.out.println(ukeycookie);
httppost.setHeader("Cookie", ukeycookie + ";" + skeycookie + ";" + usercookie);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("", cbFile);
httppost.setEntity(mpEntity);
System.out.println("Now uploading your file into mediafire...........................");
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Getting upload response key value..........");
uploadresponsekey = EntityUtils.toString(resEntity);
getUploadResponseKey();
System.out.println("upload resoponse key " + uploadresponsekey);
}
}
示例6: createPost
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
/**
* Create Post Request with FormBodyPart body
*/
public HttpPost createPost(String uri, LinkedList<FormBodyPart> partsList) {
HttpPost postRequest = new HttpPost(uri);
MultipartEntity multipartRequest = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (FormBodyPart part : partsList) {
multipartRequest.addPart(part);
}
postRequest.setEntity(multipartRequest);
return postRequest;
}
示例7: postFileUpload
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
@Override
public void postFileUpload(final String uploadServletUrl,
final String fileName, final byte[] bytes) throws IOException {
File file = null;
try {
client = new DefaultHttpClient();
client.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// if uploadServletURL is malformed - exception is thrown
new URL(uploadServletUrl);
file = File.createTempFile(fileName, "");
final BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bos.write(bytes);
bos.flush();
bos.close();
final HttpPost post = new HttpPost(uploadServletUrl);
final MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(file,
"application/octet-stream"));
post.setEntity(entity);
client.execute(post);
} catch (final IOException e) {
throw e;
} finally {
client.getConnectionManager().shutdown();
if (file != null && file.exists()) {
file.delete();
}
}
}
示例8: buildHttpPost
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private HttpPost buildHttpPost(String fileNameWithPath, String product)
{
HttpPost httpPost = new HttpPost(LogUtil.getInstance().getLogUrl());
MultipartEntity mutiEntity = new MultipartEntity(HttpMultipartMode.STRICT);
httpPost.setEntity(mutiEntity);
File file = new File(fileNameWithPath);
try
{
mutiEntity.addPart("LogFileInfo",
new StringBody("{\"product\":\"" + product + "\"}", Charset.forName("UTF-8")));
}
catch (UnsupportedEncodingException e)
{
LogUtil.runLog(LogUtil.errorLogType, "LogFileUploaderTask", "UTF-8 is not supported encode");
//LOGGER.error("UTF-8 is not supported encode");
}
mutiEntity.addPart("LogFile", new FileBody(file));
return httpPost;
}
示例9: upload
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
@Test
public void upload() throws IOException {
String charset = "UTF-8";
HttpClient client = HttpClientBuilder.create().build();
String act = "upload";
HttpPost post = new HttpPost(getUploadUrl(act));
Map<String, Object> json = new HashMap<>();
json.put("name", "张三");
json.put("age", 23);
String req = Base64.encodeBase64String(GsonUtil.toJson(json).getBytes(charset));
System.out.println("req:" + req);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("Api", StringBody.create("common", "text/plain", Charset.forName(charset)));
reqEntity.addPart("data", StringBody.create(req, "text/plain", Charset.forName(charset)));
reqEntity.addPart("img", new FileBody(new File("E:\\doc\\logo.jpg")));
post.setEntity(reqEntity);
HttpResponse resp = client.execute(post);
String line = resp.getStatusLine().toString();
Assert.assertEquals("HTTP/1.1 200 OK", line);
HttpEntity resEntity = resp.getEntity();
Log.get("upload").info(EntityUtils.toString(resEntity, charset));
}
示例10: processFileUploadInfo
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
/**
* Processes file upload information to extract the information regarding
* the files from the device storage that needs to be uploaded to the remote
* server
*
* @param postRequest
* : {@link HttpPost} request object
*
* */
private MultipartEntity processFileUploadInfo(HttpPost postRequest) {
String fileUploadInfo = this.requestFileUploadInfo;
MultipartEntity fileUploadEntity = new MultipartEntity();
try {
// Here we are receiving the JSONArray
JSONArray fileInformation = new JSONArray(fileUploadInfo);
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->file information:" + fileUploadInfo);
int totalFilesToUpload = fileInformation.length();
for (int currentIndex = 0; currentIndex < totalFilesToUpload; currentIndex++) {
// add the file entry to the 'MultipartEntity'
String fileToUploadType = fileInformation.getJSONObject(currentIndex).getString("imageType");
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->fileToUploadType:" + fileToUploadType);
if (fileToUploadType.equalsIgnoreCase(SmartConstants.FILE_UPLOAD_TYPE_URL)) {
String filenameToUpload = fileInformation.getJSONObject(currentIndex).getString("imageData");
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->fileToUpload:" + filenameToUpload);
if (filenameToUpload.contains("file://")) {
filenameToUpload = filenameToUpload.replace("file://", "");
}
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->NEW fileToUpload:" + filenameToUpload);
File fileToUpload = new File(filenameToUpload);
FileBody fileBody = new FileBody(fileToUpload);
fileUploadEntity.addPart(fileInformation.getJSONObject(currentIndex).getString("name"), fileBody);
} else if (fileToUploadType.equalsIgnoreCase(SmartConstants.FILE_UPLOAD_TYPE_DATA)) {
// TODO need to add handling for Base64 content uploading
byte[] fileContent = Base64.decode(fileInformation.getJSONObject(currentIndex).getString("imageData"), Base64.DEFAULT);
ByteArrayBody fileByteArrayBody = new ByteArrayBody(fileContent, null);
fileUploadEntity.addPart(fileInformation.getJSONObject(currentIndex).getString("name"), fileByteArrayBody);
}
}
} catch (JSONException je) {
// TODO need to handle this exception
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->JSONException:" + je.getMessage());
} catch (Exception e) {
Log.d(SmartConstants.APP_NAME, "HttpUtility->processFileUploadInfo->Exception:" + e.getMessage());
}
return fileUploadEntity;
}
示例11: main
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("File path not given");
System.exit(1);
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");
FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println("Chunked?: " + resEntity.isChunked());
}
if (resEntity != null) {
resEntity.consumeContent();
}
}
示例12: buildHttpPost
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
private HttpPost buildHttpPost(String fileNameWithPath, String product)
{
HttpPost httpPost = new HttpPost(serverUrl);
MultipartEntity mutiEntity = new MultipartEntity(HttpMultipartMode.STRICT);
httpPost.setEntity(mutiEntity);
File file = new File(fileNameWithPath);
try
{
mutiEntity.addPart("LogFileInfo",
new StringBody("{\"product\":\"" + product + "\"}", Charset.forName("UTF-8")));
}
catch (UnsupportedEncodingException e)
{
logUtil.showRunningtLog(LOG_TYPE_E.LOG_ERROR, "LogFileUploadTask.buildHttpPost()", "UTF-8 is not supported encode");
}
mutiEntity.addPart("LogFile", new FileBody(file));
return httpPost;
}
示例13: getMultipartEntity
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
public static MultipartEntity getMultipartEntity(List<FormBodyPart> formBodyParts) {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
for (FormBodyPart formBodyPart : formBodyParts) {
DebugLog.d(TAG, formBodyPart.getHeader().toString());
multipartEntity.addPart(formBodyPart);
}
return multipartEntity;
}
示例14: doPostByMultipart
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
/**
* Do a http post with the multipart content type. This method is usually
* used to upload the large size content, such as uploading a file.
*
* @param url
* @param formParams
* @param accessor
* @return
* @throws IOException
* @throws YNoteException
*/
public static HttpResponse doPostByMultipart(String url,
Map<String, Object> formParams, OAuthAccessor accessor)
throws IOException, YNoteException {
HttpPost post = new HttpPost(url);
// for multipart encoded post, only sign with the oauth parameters
// do not sign the our form parameters
Header oauthHeader = getAuthorizationHeader(url, OAuthMessage.POST,
null, accessor);
if (formParams != null) {
// encode our ynote parameters
MultipartEntity entity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (Entry<String, Object> parameter : formParams.entrySet()) {
if (parameter.getValue() instanceof File) {
// deal with file particular
entity.addPart(parameter.getKey(),
new FileBody((File)parameter.getValue()));
} else if (parameter.getValue() != null){
entity.addPart(parameter.getKey(), new StringBody(
parameter.getValue().toString(),
Charset.forName(YNoteConstants.ENCODING)));
}
}
post.setEntity(entity);
}
post.addHeader(oauthHeader);
HttpResponse response = client.execute(post);
if ((response.getStatusLine().getStatusCode() / 100) != 2) {
YNoteException e = wrapYNoteException(response);
throw e;
}
return response;
}
示例15: setVi
import org.apache.http.entity.mime.MultipartEntity; //导入方法依赖的package包/类
/**
* 设置用户所在组织的形象设置
*
* @param title
* 软件标题
* @param windowsLogoFile
* windows客户端显示的Logo,为null时代表清除Logo。
* @param androidLogoFile
* android客户端显示的Logo,为null时代表清除Logo。
* @throws Exception
*/
public void setVi(String title, File windowsLogoFile, File androidLogoFile, String group)
throws Exception {
LOG.info("Update vi group of "+ group +" using title:"+title
+",windowLogoPath:" + windowsLogoFile +",androidLogoPath:" + androidLogoFile);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE, null,
Charset.forName("UTF-8"));
if (title == null) {
title = "";
}
entity.addPart("clientTitle",new StringBody(title, Charset.forName("UTF-8")));
if (windowsLogoFile != null) {
entity.addPart(LOGO_FOR_WINDOWS, new FileBody(windowsLogoFile));
} else {
entity.addPart(LOGO_FOR_WINDOWS, new StringBody(""));
}
if (androidLogoFile != null) {
entity.addPart(LOGO_FOR_ANDROID, new FileBody(androidLogoFile));
} else {
entity.addPart(LOGO_FOR_ANDROID, new StringBody(""));
}
String url = getSetViUrl(group);
postMultipartEntity(entity,url);
}