Search in sources :

Example 31 with FileBody

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) HttpResponse(org.apache.http.HttpResponse) File(java.io.File)

Example 32 with FileBody

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;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 33 with FileBody

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) File(java.io.File)

Example 34 with FileBody

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();
    }
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) FileBody(org.apache.http.entity.mime.content.FileBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) BasicHeader(org.apache.http.message.BasicHeader) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 35 with FileBody

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) JSONObject(org.json.JSONObject) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) JSONObject(org.json.JSONObject) File(java.io.File) APIException(com.liferay.ide.core.remote.APIException) JSONException(org.json.JSONException)

Aggregations

FileBody (org.apache.http.entity.mime.content.FileBody)61 HttpPost (org.apache.http.client.methods.HttpPost)46 File (java.io.File)41 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)37 HttpResponse (org.apache.http.HttpResponse)31 StringBody (org.apache.http.entity.mime.content.StringBody)30 HttpEntity (org.apache.http.HttpEntity)18 IOException (java.io.IOException)17 Test (org.junit.Test)17 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 HttpClient (org.apache.http.client.HttpClient)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 JsonObject (com.google.gson.JsonObject)5 JSONObject (org.json.JSONObject)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4