use of org.apache.http.entity.mime.MultipartEntity in project OpenMEAP by OpenMEAP.
the class FileHandlingHttpRequestExecuterImpl method postData.
@Override
public HttpResponse postData(String url, Hashtable getParams, Hashtable postParams) throws HttpRequestException {
// test to determine whether this is a file upload or not.
Boolean isFileUpload = false;
for (Object o : postParams.values()) {
if (o instanceof File) {
isFileUpload = true;
break;
}
}
if (isFileUpload) {
try {
HttpPost httpPost = new HttpPost(createUrl(url, getParams));
httpPost.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for (Object o : postParams.entrySet()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) o;
if (entry.getValue() instanceof File) {
// For File parameters
File file = (File) entry.getValue();
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String type = fileNameMap.getContentTypeFor(file.toURL().toString());
entity.addPart(entry.getKey(), new FileBody(((File) entry.getValue()), type));
} else {
// For usual String parameters
entity.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), "text/plain", Charset.forName(FormConstants.CHAR_ENC_DEFAULT)));
}
}
httpPost.setEntity(entity);
return execute(httpPost);
} catch (Exception e) {
throw new HttpRequestException(e);
}
} else {
return super.postData(url, getParams, postParams);
}
}
use of org.apache.http.entity.mime.MultipartEntity in project ninja by ninjaframework.
the class NinjaTestBrowser method uploadFileWithForm.
public String uploadFileWithForm(String url, String paramName, File fileToUpload, Map<String, String> formParameters) {
String response = null;
try {
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// For File parameters
entity.addPart(paramName, new FileBody((File) fileToUpload));
// add form parameters:
if (formParameters != null) {
for (Entry<String, String> parameter : formParameters.entrySet()) {
entity.addPart(parameter.getKey(), new StringBody(parameter.getValue()));
}
}
post.setEntity(entity);
// Here we go!
response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
post.releaseConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
use of org.apache.http.entity.mime.MultipartEntity in project ninja by ninjaframework.
the class NinjaTestBrowser method uploadFiles.
public String uploadFiles(String url, String[] paramNames, File[] fileToUploads) {
String response = null;
try {
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// For File parameters
for (int i = 0; i < paramNames.length; i++) {
entity.addPart(paramNames[i], new FileBody(fileToUploads[i]));
}
post.setEntity(entity);
// Here we go!
response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
post.releaseConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.
the class MultiPartTestCase method testMultiPartRequest.
@Test
public void testMultiPartRequest() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
String uri = DefaultServer.getDefaultServerURL() + "/servletContext/1";
HttpPost post = new HttpPost(uri);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, StandardCharsets.UTF_8);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("PARAMS:\r\n" + "parameter count: 1\r\n" + "parameter name count: 1\r\n" + "name: formValue\r\n" + "filename: null\r\n" + "content-type: null\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "size: 7\r\n" + "content: myValue\r\n" + "name: file\r\n" + "filename: uploadfile.txt\r\n" + "content-type: application/octet-stream\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "size: 13\r\n" + "content: file contents\r\n", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.mime.MultipartEntity in project undertow by undertow-io.
the class MultiPartTestCase method testMultiPartRequestWithNoMultipartConfig.
@Test
public void testMultiPartRequestWithNoMultipartConfig() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
String uri = DefaultServer.getDefaultServerURL() + "/servletContext/0";
HttpPost post = new HttpPost(uri);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("EXCEPTION: class java.lang.IllegalStateException", response);
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations