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());
}
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"));
}
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"));
}
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);
}
}
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);
}
}
Aggregations