Search in sources :

Example 16 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project camel by apache.

the class HttpBridgeMultipartRouteTest method testHttpClient.

@Test
public void testHttpClient() throws Exception {
    File jpg = new File("src/test/resources/java.jpg");
    String body = "TEST";
    Part[] parts = new Part[] { new StringPart("body", body), new FilePart(jpg.getName(), jpg) };
    PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello");
    MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
    method.setRequestEntity(requestEntity);
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    String responseBody = method.getResponseBodyAsString();
    assertEquals(body, responseBody);
    String numAttachments = method.getResponseHeader("numAttachments").getValue();
    assertEquals(numAttachments, "2");
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Example 17 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project camel by apache.

the class MultiPartFormTest method createMultipartRequestEntity.

private RequestEntity createMultipartRequestEntity() throws Exception {
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    return new MultipartRequestEntity(parts, new HttpMethodParams());
}
Also used : StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Example 18 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project camel by apache.

the class MultiPartFormWithCustomFilterTest method testSendMultiPartFormOverrideEnableMultpartFilterFalse.

@Test
public void testSendMultiPartFormOverrideEnableMultpartFilterFalse() throws Exception {
    HttpClient httpclient = new HttpClient();
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test2");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);
    int status = httpclient.executeMethod(httppost);
    assertEquals("Get a wrong response status", 200, status);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Example 19 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project camel by apache.

the class MultiPartFormWithCustomFilterTest method testSendMultiPartForm.

@Test
public void testSendMultiPartForm() throws Exception {
    HttpClient httpclient = new HttpClient();
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);
    int status = httpclient.executeMethod(httppost);
    assertEquals("Get a wrong response status", 200, status);
    String result = httppost.getResponseBodyAsString();
    assertEquals("Get a wrong result", "A binary file of some kind", result);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Example 20 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project twitter-2-weibo by rjyo.

the class HttpClient method multPartURL.

/**
	 * 支持multipart方式上传图片
	 * 
	 */
public Response multPartURL(String url, PostParameter[] params, ImageItem item) throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
        Part[] parts = null;
        if (params == null) {
            parts = new Part[1];
        } else {
            parts = new Part[params.length + 1];
        }
        if (params != null) {
            int i = 0;
            for (PostParameter entry : params) {
                parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
            }
            parts[parts.length - 1] = new ByteArrayPart(item.getContent(), item.getName(), item.getContentType());
        }
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        return httpRequest(postMethod);
    } catch (Exception ex) {
        throw new WeiboException(ex.getMessage(), ex, -1);
    }
}
Also used : WeiboException(weibo4j.model.WeiboException) PostParameter(weibo4j.model.PostParameter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) JSONException(weibo4j.org.json.JSONException) IOException(java.io.IOException) WeiboException(weibo4j.model.WeiboException)

Aggregations

Part (org.apache.commons.httpclient.methods.multipart.Part)31 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)29 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)28 PostMethod (org.apache.commons.httpclient.methods.PostMethod)25 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)24 HttpClient (org.apache.commons.httpclient.HttpClient)13 File (java.io.File)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)5 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)5 HttpState (org.apache.commons.httpclient.HttpState)4 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ServiceException (com.zimbra.common.service.ServiceException)3 Element (com.zimbra.common.soap.Element)3 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)3 Map (java.util.Map)3 HeaderElement (org.apache.commons.httpclient.HeaderElement)3