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


Java MultipartEntity.addPart方法代碼示例

本文整理匯總了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);
}
 
開發者ID:williamgrosset,項目名稱:OSCAR-ConCert,代碼行數:21,代碼來源:SendingUtils.java

示例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;
}
 
開發者ID:Coffeeboys,項目名稱:RenewPass,代碼行數:26,代碼來源:RequestBuilder.java

示例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);
    }
 
開發者ID:Neembuu-Uploader,項目名稱:neembuu-uploader,代碼行數:27,代碼來源:UGotFileUploaderPlugin.java

示例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);
}
 
開發者ID:Neembuu-Uploader,項目名稱:neembuu-uploader,代碼行數:26,代碼來源:ShareSend.java

示例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);
    }
}
 
開發者ID:Neembuu-Uploader,項目名稱:neembuu-uploader,代碼行數:24,代碼來源:MediaFireUploadPlugin.java

示例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;
}
 
開發者ID:Blazemeter,項目名稱:jmeter-bzm-plugins,代碼行數:13,代碼來源:HttpUtils.java

示例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();
		}
	}
}
 
開發者ID:roscisz,項目名稱:KernelHive,代碼行數:37,代碼來源:HttpFileUploadClient.java

示例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;
}
 
開發者ID:Huawei,項目名稱:eSDK_FC_SDK_Java,代碼行數:20,代碼來源:LogFileUploaderTask.java

示例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));
}
 
開發者ID:youtongluan,項目名稱:sumk,代碼行數:25,代碼來源:HttpTest.java

示例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;
}
 
開發者ID:appez,項目名稱:appez-android,代碼行數:47,代碼來源:HttpUtility.java

示例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();
    }
}
 
開發者ID:mleoking,項目名稱:PhET,代碼行數:34,代碼來源:ClientMultipartFormPost.java

示例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;
}
 
開發者ID:Huawei,項目名稱:eSDK_OpenApi_Windows_Java,代碼行數:19,代碼來源:LogFileUploadTask.java

示例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;
}
 
開發者ID:leleliu008,項目名稱:Newton_for_Android_AS,代碼行數:9,代碼來源:HTTPUtil.java

示例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;
}
 
開發者ID:cowthan,項目名稱:JavaAyo,代碼行數:45,代碼來源:YNoteHttpUtils.java

示例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);
}
 
開發者ID:duckling-falcon,項目名稱:dchatsdk,代碼行數:36,代碼來源:ViRestClient.java


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