通过webHDFS REST API将图像上传到HDFS的问题 [英] Issues with Uploading an image to HDFS via webHDFS REST API

查看:1306
本文介绍了通过webHDFS REST API将图像上传到HDFS的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MultiPartEntity的HttpPut通过webHDFS REST API将文件写入HDFS。请求本身经过并给了我正确的回应,307和201.然而,图像具有多部分标题,也作为其一部分写入,如下所示,它不是一个有效的图像来检索和打开。

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 = 文件; filename =advert.jpg

Content-Type:application / octet-stream


ÿØÿàJFIFHHÿÛC
//其余的图像内容/>
--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps

从图像文件中删除多段标题,使其成为有效图像,但我不确定可以避免它开始。我甚至不确定是否可以控制这个,因为webHDFS负责实际编写文件。

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.

推荐答案

使用Content-Typeapplication / octet-stream将图像添加为FileEntity,ByteArrayEntity或InputStreamEntity。

Add the image as FileEntity, ByteArrayEntity or InputStreamEntity with Content-Type "application/octet-stream".

这篇关于通过webHDFS REST API将图像上传到HDFS的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆