List of usage examples for org.apache.http.entity.mime MultipartEntity MultipartEntity
public MultipartEntity()
From source file:org.deviceconnect.android.manager.test.MultipartTest.java
/** * 0?????????.//from www.j a v a 2 s . c om * <pre> * Method: POST * Path: /file/send?deviceid=xxxx&filename=xxxx * </pre> * <pre> * ?? * result?0??????? * </pre> */ public void testSendZeroByteFile() { URIBuilder builder = TestURIBuilder.createURIBuilder(); builder.setProfile(FileProfileConstants.PROFILE_NAME); builder.setAttribute(FileProfileConstants.ATTRIBUTE_SEND); builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId()); builder.addParameter(DConnectMessage.EXTRA_ACCESS_TOKEN, getAccessToken()); builder.addParameter(FileProfileConstants.PARAM_PATH, "/test/zero.dat"); builder.addParameter(FileProfileConstants.PARAM_FILE_TYPE, String.valueOf(FileProfileConstants.FileType.FILE.getValue())); try { MultipartEntity entity = new MultipartEntity(); // ?0?? entity.addPart(FileProfileConstants.PARAM_DATA, new BinaryBody(new byte[] {}, "zero.dat")); HttpPost request = new HttpPost(builder.toString()); request.setEntity(entity); JSONObject root = sendRequest(request); assertResultOK(root); } catch (JSONException e) { fail("Exception in JSONObject." + e.getMessage()); } }
From source file:eu.liveGov.libraries.livegovtoolkit.Utils.RestClient.java
public void Execute(RequestMethod method, int soTime, int connTime) throws Exception { switch (method) { case GET: {//ww w. ja v a 2s . com // add parameters String combinedParams = ""; if (!params.isEmpty()) { combinedParams += "?"; for (NameValuePair p : params) { String paramString; paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(), "UTF-8"); if (combinedParams.length() > 1) { combinedParams += "&" + paramString; } else { combinedParams += paramString; } } } HttpGet request = new HttpGet(url + combinedParams); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } executeRequest(request, url, soTime, connTime); break; } case POST: { HttpPost request = new HttpPost(url); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } if (!_json.isEmpty()) { request.addHeader("Content-type", "application/json"); request.setEntity(new StringEntity(_json, "UTF8")); } else if (!params.isEmpty()) { request.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); } executeRequest(request, url, soTime, connTime); break; } case POSTLOG: { HttpPost request = new HttpPost(url); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } MultipartEntity reqEntity = new MultipartEntity(); if (!_json.isEmpty()) { reqEntity.addPart("LogfileRequest", new StringBody(_json, "application/json", Charset.forName("UTF-8"))); } else if (!params.isEmpty()) { // ignore the params for now. } if (_file != null) { reqEntity.addPart("logfile", new FileBody(_file)); } request.setEntity(reqEntity); executeRequest(request, url, soTime, connTime); break; } } }
From source file:com.brightcove.com.uploader.helper.MediaAPIHelper.java
/** * Executes a file upload write api call against the given URI. * This is useful for create_video and add_image * @param json the json representation of the call you are making * @param file the file you are uploading * @param uri the api servlet you want to execute against * @return json response from api//from ww w . java 2 s . co m * @throws BadEnvironmentException * @throws MediaAPIError */ public JsonNode executeWrite(JsonNode json, File file, URI uri) throws BadEnvironmentException, MediaAPIError { mLog.debug("using " + uri.getHost() + " on port " + uri.getPort() + " for api write"); HttpPost method = new HttpPost(uri); MultipartEntity entityIn = new MultipartEntity(); FileBody fileBody = null; if (file != null) { fileBody = new FileBody(file); } try { entityIn.addPart("JSON-RPC", new StringBody(json.toString(), Charset.forName("UTF-8"))); } catch (UnsupportedEncodingException e) { throw new BadEnvironmentException("UTF-8 no longer supported"); } if (file != null) { entityIn.addPart(file.getName(), fileBody); } method.setEntity(entityIn); return executeCall(method); }
From source file:com.dmsl.anyplace.tasks.UploadRSSLogTask.java
@Override protected String doInBackground(Void... params) { try {/*from w w w . j av a 2 s . com*/ JSONObject j; j = new JSONObject(); j.put("username", username); j.put("password", password); String json = j.toString(); File rsslog = new File(this.file); if (rsslog.exists() == false) { exceptionOccured = true; return "File not found"; } Log.d("radio upload", rsslog.toString()); String response; HttpClient httpclient = new DefaultHttpClient(); httppost = new HttpPost(AnyplaceAPI.getRadioUploadUrl()); MultipartEntity entity = new MultipartEntity(); entity.addPart("radiomap", new FileBody(rsslog)); entity.addPart("json", new StringBody(json)); ProgressCallback progressCallback = new ProgressCallback() { @Override public void progress(float progress) { if (currentProgress != (int) (progress)) { currentProgress = (int) progress; publishProgress(currentProgress); } } }; httppost.setEntity(new ProgressHttpEntityWrapper(entity, progressCallback)); HttpResponse httpresponse = httpclient.execute(httppost); HttpEntity resEntity = httpresponse.getEntity(); response = EntityUtils.toString(resEntity); Log.d("radio upload", "response: " + response); j = new JSONObject(response); if (j.getString("status").equalsIgnoreCase("error")) { exceptionOccured = true; return "Error: " + j.getString("message"); } } catch (JSONException e) { exceptionOccured = true; Log.d("upload rss log", e.getMessage()); return "Cannot upload RSS log. JSONException occurred[ " + e.getMessage() + " ]"; } catch (ParseException e) { exceptionOccured = true; Log.d("upload rss log", e.getMessage()); return "Cannot upload RSS log. ParseException occurred[ " + e.getMessage() + " ]"; } catch (IOException e) { exceptionOccured = true; Log.d("upload rss log", e.getMessage()); if (httppost != null && httppost.isAborted()) { return "Uploading cancelled!"; } else { return "Cannot upload RSS log. IOException occurred[ " + e.getMessage() + " ]"; } } return "Successfully uploaded RSS log!"; }
From source file:com.brightcove.zartan.common.helper.MediaAPIHelper.java
/** * Executes a file upload write api call against the given URI. This is useful for create_video * and add_image/*from w w w . j a v a 2 s .co m*/ * * @param json the json representation of the call you are making * @param file the file you are uploading * @param uri the api servlet you want to execute against * @return json response from api * @throws BadEnvironmentException * @throws MediaAPIException */ public JsonNode executeWrite(JsonNode json, File file, URI uri) throws BadEnvironmentException, MediaAPIException { mLog.debug("using " + uri.getHost() + " on port " + uri.getPort() + " for api write"); HttpPost method = new HttpPost(uri); MultipartEntity entityIn = new MultipartEntity(); FileBody fileBody = null; if (file != null) { fileBody = new FileBody(file); } try { entityIn.addPart("JSON-RPC", new StringBody(json.toString(), Charset.forName("UTF-8"))); } catch (UnsupportedEncodingException e) { throw new BadEnvironmentException("UTF-8 no longer supported"); } if (file != null) { entityIn.addPart(file.getName(), fileBody); } method.setEntity(entityIn); return executeCall(method); }
From source file:com.liferay.ide.server.remote.ServerManagerConnection.java
public Object installApplication(String absolutePath, String appName, IProgressMonitor submon) throws APIException { try {/*from ww w . j av a 2s . co m*/ FileBody fileBody = new FileBody(new File(absolutePath)); MultipartEntity entity = new MultipartEntity(); entity.addPart("deployWar", fileBody); HttpPost httpPost = new HttpPost(); httpPost.setEntity(entity); Object response = httpJSONAPI(httpPost, _getDeployURI(appName)); if (response instanceof JSONObject) { JSONObject jsonObject = (JSONObject) response; if (_isSuccess(jsonObject)) { System.out.println("installApplication: Sucess.\n\n"); } else { if (_isError(jsonObject)) { return jsonObject.getString("error"); } else { return "installApplication error " + _getDeployURI(appName); } } } httpPost.releaseConnection(); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } return null; }
From source file:info.guardianproject.net.http.HttpManager.java
public static String uploadFile(String serviceEndpoint, Properties properties, String fileParam, String file) throws Exception { HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(serviceEndpoint); MultipartEntity entity = new MultipartEntity(); Iterator<Map.Entry<Object, Object>> i = properties.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) i.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); entity.addPart(key, new StringBody(val)); }/*from w w w .j a va 2 s.com*/ File upload = new File(file); Log.i("httpman", "upload file (" + upload.getAbsolutePath() + ") size=" + upload.length()); entity.addPart(fileParam, new FileBody(upload)); request.setEntity(entity); HttpResponse response = httpClient.execute(request); int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { } else { } return response.toString(); }
From source file:com.liferay.arquillian.container.LiferayContainer.java
private MultipartEntity createMultipartEntity(Archive<?> archive) { ZipExporter zipView = archive.as(ZipExporter.class); InputStream inputStream = zipView.exportAsInputStream(); MultipartEntity entity = new MultipartEntity(); entity.addPart(archive.getName(), new InputStreamBody(inputStream, archive.getName())); return entity; }
From source file:com.openideals.android.net.HttpManager.java
public static String uploadFile(String serviceEndpoint, Properties properties, String fileParam, String file) throws Exception { HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(serviceEndpoint); MultipartEntity entity = new MultipartEntity(); Iterator<Map.Entry<Object, Object>> i = properties.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) i.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); entity.addPart(key, new StringBody(val)); }/* w w w. j av a 2 s . c o m*/ File upload = new File(file); Log.i("httpman", "upload file (" + upload.getAbsolutePath() + ") size=" + upload.length()); entity.addPart(fileParam, new FileBody(upload)); request.setEntity(entity); HttpResponse response = httpClient.execute(request); int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { } else { } return response.toString(); }
From source file:org.dataconservancy.ui.it.support.DepositRequest.java
public HttpPost asHttpPost() { if (!dataSetSet) { throw new IllegalStateException("DataItem not set: call setDataSet(DataItem) first."); }//from w w w .ja v a 2 s . c o m if (fileToDeposit == null) { throw new IllegalStateException("File not set: call setFileToDeposit(File) first"); } if (collectionId == null || collectionId.isEmpty()) { throw new IllegalStateException("Collection id not set: call setCollectionId(String) first."); } if (isUpdate && (dataItemIdentifier == null || dataItemIdentifier.isEmpty())) { throw new IllegalStateException( "Identifer is not set Identifier must be set: callSetDataSetIdentifier or pass in an ID in the constructor"); } if (null == packageId) packageId = ""; String depositUrl = urlConfig.getDepositUrl().toString() + "?redirectUrl=/pages/usercollections.jsp?currentCollectionId=" + collectionId; HttpPost post = new HttpPost(depositUrl); MultipartEntity entity = new MultipartEntity(); try { entity.addPart("currentCollectionId", new StringBody(collectionId, Charset.forName("UTF-8"))); entity.addPart("dataSet.name", new StringBody(name, Charset.forName("UTF-8"))); entity.addPart("dataSet.description", new StringBody(description, Charset.forName("UTF-8"))); entity.addPart("dataSet.id", new StringBody(dataItemIdentifier, Charset.forName("UTF-8"))); entity.addPart("depositPackage.id", new StringBody(packageId, Charset.forName("UTF-8"))); entity.addPart("isContainer", new StringBody(Boolean.valueOf(isContainer()).toString(), Charset.forName("UTF-8"))); if (isUpdate) { entity.addPart("datasetToUpdateId", new StringBody(dataItemIdentifier, Charset.forName("UTF-8"))); entity.addPart(STRIPES_UPDATE_EVENT, new StringBody("Update", Charset.forName("UTF-8"))); } else { entity.addPart(STRIPES_DEPOSIT_EVENT, new StringBody("Deposit", Charset.forName("UTF-8"))); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } FileBody fileBody = new FileBody(fileToDeposit); entity.addPart("uploadedFile", fileBody); post.setEntity(entity); return post; }