use of org.apache.http.entity.mime.content.FileBody in project tdi-studio-se by Talend.
the class RestClient method uploadGeneratedBar.
private String uploadGeneratedBar(String path) throws IOException, ClientProtocolException {
// build the process example
File barFile = new File(path);
HttpPost post = new HttpPost(bonitaURI + "/portal/processUpload");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(barFile));
post.setEntity(entity);
HttpResponse response = httpClient.execute(post, httpContext);
return extractUploadedFilePathFromResponse(response);
}
use of org.apache.http.entity.mime.content.FileBody in project instructure-android by instructure.
the class UploadFileSynchronousAPI method uploadFile.
// STEPS 2 AND 3 OF API
public static String uploadFile(File image, String url, String contentType, ArrayList<BasicNameValuePair> pairs, Context context) {
try {
// STEP 2 of API
HttpPost httppost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity();
for (BasicNameValuePair pair : pairs) {
String value = pair.getValue();
multipartEntity.addPart(pair.getName(), new StringBody(pair.getValue()));
}
multipartEntity.addPart("file", new FileBody(image));
httppost.setEntity(multipartEntity);
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
if (mHttpClient == null) {
mHttpClient = AndroidHttpClient.newInstance("agent");
mHttpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
mHttpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
}
HttpResponse httpResponse = mHttpClient.execute(httppost);
String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
// STEP 3 of API
Header[] headerLocation = httpResponse.getHeaders("Location");
if (headerLocation.length <= 0) {
return "Header error";
}
String location = headerLocation[0].getValue();
Header[] headerContent = httpResponse.getHeaders("Content-Length");
if (headerContent.length <= 0) {
return "Header content error";
}
BasicNameValuePair pair = new BasicNameValuePair(headerContent[0].getName(), headerContent[0].getValue());
ArrayList<BasicNameValuePair> paramPairs = new ArrayList<BasicNameValuePair>();
pairs.add(pair);
APIHttpResponse postResponse = HttpHelpers.httpPost(location, paramPairs, context);
return postResponse.responseBody;
} catch (Exception e) {
return null;
}
}
use of org.apache.http.entity.mime.content.FileBody in project tmdm-studio-se by Talend.
the class HttpClientUtil method createUploadRequest.
private static HttpUriRequest createUploadRequest(String URL, String userName, String localFilename, String filename, String imageCatalog, HashMap<String, String> picturePathMap) {
HttpPost request = new HttpPost(URL);
MultipartEntity entity = new MultipartEntity();
if (!Messages.Util_24.equalsIgnoreCase(localFilename)) {
File file = new File(localFilename);
if (file.exists()) {
// $NON-NLS-1$
entity.addPart("imageFile", new FileBody(file));
}
}
if (imageCatalog != null) {
// $NON-NLS-1$
entity.addPart("catalogName", StringBody.create(imageCatalog, STRING_CONTENT_TYPE, null));
}
if (filename != null) {
int pos = filename.lastIndexOf('.');
if (pos != -1) {
filename = filename.substring(0, pos);
}
// $NON-NLS-1$
entity.addPart("fileName", StringBody.create(filename, STRING_CONTENT_TYPE, null));
}
request.setEntity(entity);
addStudioToken(request, userName);
return request;
}
use of org.apache.http.entity.mime.content.FileBody in project ais-sdk by huaweicloudsdk.
the class TokenDemo method requestOcrCustomsFormEnBase64.
/**
* 英文海关识别,使用Base64编码后的文件方式,使用Token认证方式访问服务
* @param token token认证串
* @param formFile 文件路径
* @throws IOException
*/
public static void requestOcrCustomsFormEnBase64(String token, String formFile) {
// 1.构建英文海关单据识别服务所需要的参数
String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/action/ocr_form";
Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token) };
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(new File(formFile), ContentType.create("image/jpeg", "utf-8"));
multipartEntityBuilder.addPart("file", fileBody);
// 2.传入英文海关单据识别服务对应的参数, 使用POST方法调用服务并解析输出识别结果
HttpResponse response = HttpClientUtils.post(url, headers, multipartEntityBuilder.build());
System.out.println(response);
String content = IOUtils.toString(response.getEntity().getContent());
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.http.entity.mime.content.FileBody in project liferay-ide by liferay.
the class ServerManagerConnection method installApplication.
public Object installApplication(String absolutePath, String appName, IProgressMonitor submon) throws APIException {
try {
FileBody fileBody = new FileBody(new File(absolutePath));
MultipartEntity entity = new MultipartEntity();
// $NON-NLS-1$
entity.addPart("deployWar", fileBody);
HttpPost httpPost = new HttpPost();
httpPost.setEntity(entity);
Object response = httpJSONAPI(httpPost, getDeployURI(appName));
if (response instanceof JSONObject) {
JSONObject json = (JSONObject) response;
if (isSuccess(json)) {
// $NON-NLS-1$
System.out.println("installApplication: Sucess.\n\n");
} else {
if (isError(json)) {
// $NON-NLS-1$
return json.getString("error");
} else {
// $NON-NLS-1$
return "installApplication error " + getDeployURI(appName);
}
}
}
httpPost.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return null;
}
Aggregations