I am doing HttpPut with MultiPartEntity to write a file to HDFS via the webHDFS REST API. The request itself goes through and gives me the right responses, 307 and 201. However the image has multipart headers also written as part of it as shown below and its not a valid image to retrieve and open.
--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps
Content-Disposition: form-data; name="file"; filename="advert.jpg"
Content-Type: application/octet-stream
ÿØÿàJFIFHHÿÛC
// Rest of the image content
--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps
Removing the multipart headers from the image file, makes it a valid image, but I am not sure how I can avoid it to begin with. I am not even sure if I have control over this since the webHDFS is responsible for actually writing the file.
Here is my code for it. Is there something else I should be doing?
final String LOCATION = "Location";
final String writeURI = "http://<ip>:50070/webhdfs/v1/user/hadoop/advert.jpg";
HttpPut put = new HttpPut(writeURI);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(put);
put.releaseConnection();
String redirectUri = null;
Header[] headers = response.getAllHeaders();
for(Header header : headers)
{
if(LOCATION.equalsIgnoreCase(header.getName()))
{
redirectUri = header.getValue();
}
}
HttpPut realPut = new HttpPut(redirectUri);
realPut.setEntity(buildMultiPartEntity("advert.jpg"));
HttpResponse response2 = client.execute(realPut);
private HttpEntity buildMultiPartEntity(String fileName)
{
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", new FileBody(new File(fileName)));
return multipartEntity.build();
}
Any help is appreciated.
Add the image as FileEntity, ByteArrayEntity or InputStreamEntity with Content-Type "application/octet-stream".