use of org.apache.http.entity.mime.MultipartEntity in project product-iots by wso2.
the class HTTPInvoker method uploadFile.
public static HTTPResponse uploadFile(String url, String fileName, String fileContentType) {
HttpPost post = null;
HttpResponse response = null;
HTTPResponse httpResponse = new HTTPResponse();
CloseableHttpClient httpclient = null;
try {
httpclient = (CloseableHttpClient) createHttpClient();
post = new HttpPost(url);
File file = new File(fileName);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, fileContentType);
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);
post.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken);
// post.setHeader(Constants.Header.CONTENT_TYPE, "multipart/form-data");
post.setHeader("Accept", Constants.ContentType.APPLICATION_JSON);
response = httpclient.execute(post);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer result = new StringBuffer();
String line = "";
try {
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
httpResponse.setResponseCode(response.getStatusLine().getStatusCode());
httpResponse.setResponse(result.toString());
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
return httpResponse;
}
use of org.apache.http.entity.mime.MultipartEntity in project nanohttpd by NanoHttpd.
the class HttpServerTest method testMultipartFormData.
@Test
public void testMultipartFormData() throws IOException {
final int testPort = 4589;
NanoHTTPD server = null;
try {
server = new NanoHTTPD(testPort) {
final Map<String, String> files = new HashMap<String, String>();
@Override
public Response serve(IHTTPSession session) {
StringBuilder responseMsg = new StringBuilder();
try {
session.parseBody(this.files);
for (String key : files.keySet()) {
responseMsg.append(key);
}
} catch (Exception e) {
responseMsg.append(e.getMessage());
}
return Response.newFixedLengthResponse(responseMsg.toString());
}
};
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:" + testPort);
final String fileName = "file-upload-test.htm";
FileBody bin = new FileBody(new File(getClass().getClassLoader().getResource(fileName).getFile()));
StringBody comment = new StringBody("Filename: " + fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
String line = reader.readLine();
assertNotNull(line, "Invalid server reponse");
assertEquals("Server failed multi-part data parse" + line, "bincomment", line);
reader.close();
instream.close();
}
} finally {
if (server != null) {
server.stop();
}
}
}
use of org.apache.http.entity.mime.MultipartEntity in project nanohttpd by NanoHttpd.
the class HttpServerTest method testTempFileInterface.
@Test
public void testTempFileInterface() throws IOException {
final int testPort = 4589;
NanoHTTPD server = new NanoHTTPD(testPort) {
final Map<String, String> files = new HashMap<String, String>();
@Override
public Response serve(IHTTPSession session) {
String responseMsg = "pass";
try {
session.parseBody(this.files);
for (String key : files.keySet()) {
if (!(new File(files.get(key))).exists()) {
responseMsg = "fail";
}
}
} catch (Exception e) {
responseMsg = e.getMessage();
}
return Response.newFixedLengthResponse(responseMsg.toString());
}
};
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:" + testPort);
final String fileName = "file-upload-test.htm";
FileBody bin = new FileBody(new File(getClass().getClassLoader().getResource(fileName).getFile()));
StringBody comment = new StringBody("Filename: " + fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
String line = reader.readLine();
assertNotNull(line, "Invalid server reponse");
assertEquals("Server file check failed: " + line, "pass", line);
reader.close();
instream.close();
} else {
fail("No server response");
}
server.stop();
}
use of org.apache.http.entity.mime.MultipartEntity in project selenium_java by sergueik.
the class RestClient method request.
private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments) throws RestException, IOException {
if (attachments != null) {
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
for (Issue.NewAttachment attachment : attachments) {
String filename = attachment.getFilename();
Object content = attachment.getContent();
if (content instanceof byte[]) {
ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
} else if (content instanceof InputStream) {
ent.addPart("file", new InputStreamBody((InputStream) content, filename));
} else if (content instanceof File) {
ent.addPart("file", new FileBody((File) content, filename));
} else if (content == null) {
throw new IllegalArgumentException("Missing content for the file " + filename);
} else {
throw new IllegalArgumentException("Expected file type byte[], java.io.InputStream or java.io.File but provided " + content.getClass().getName() + " for the file " + filename);
}
}
req.setEntity(ent);
}
return request(req);
}
use of org.apache.http.entity.mime.MultipartEntity in project selenium_java by sergueik.
the class RestClient method request.
private JSON request(HttpEntityEnclosingRequestBase req, File file) throws RestException, IOException {
if (file != null) {
File fileUpload = file;
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
ent.addPart("file", new FileBody(fileUpload));
req.setEntity(ent);
}
return request(req);
}
Aggregations