Search in sources :

Example 16 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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 17 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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 18 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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 19 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity 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)

Example 20 with MultipartRequestEntity

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

the class HttpClient method multPartURL.

public Response multPartURL(String fileParamName, String url, PostParameter[] params, File file, boolean authenticated) 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());
            }
        }
        FilePart filePart = new FilePart(fileParamName, file.getName(), file, new MimetypesFileTypeMap().getContentType(file), "UTF-8");
        filePart.setTransferEncoding("binary");
        parts[parts.length - 1] = filePart;
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        return httpRequest(postMethod);
    } catch (Exception ex) {
        throw new WeiboException(ex.getMessage(), ex, -1);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) 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) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) JSONException(weibo4j.org.json.JSONException) IOException(java.io.IOException) WeiboException(weibo4j.model.WeiboException)

Aggregations

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)31 Part (org.apache.commons.httpclient.methods.multipart.Part)29 PostMethod (org.apache.commons.httpclient.methods.PostMethod)27 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)26 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)24 HttpClient (org.apache.commons.httpclient.HttpClient)15 IOException (java.io.IOException)11 File (java.io.File)9 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