通过webhdfs restapi将图像上传到hdfs的问题

e1xvtsh3  于 2021-06-03  发布在  Hadoop
关注(0)|答案(3)|浏览(557)

我正在使用multipartentity进行httpput,以便通过webhdfs restapi将文件写入hdfs。请求本身通过并给了我正确的回答,307和201。但是,该映像有多部分头文件,也作为其一部分写入,如下所示,并且它不是要检索和打开的有效映像。
--8dkj3rkuhaheane9ktw8nc1tfoqegjfa9ps公司
内容配置:表单数据;name=“文件”;filename=“广告.jpg”
内容类型:应用程序/八位字节流
ÿøÿàjfifhh公司ÿûc//图像内容的其余部分
--8dkj3rkuhaheane9ktw8nc1tfoqegjfa9ps公司
从映像文件中删除多部分头,使其成为有效映像,但我不确定如何从一开始就避免它。我甚至不确定我是否能控制它,因为webhdfs负责实际编写文件。
这是我的密码。我还有别的事要做吗?

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

感谢您的帮助。

iezvtpos

iezvtpos1#

将图像添加为fileentity、bytearrayentity或inputstreamentity,内容类型为“application/octet stream”。

nlejzf6q

nlejzf6q2#

我在python请求中遇到了同样的问题。我最终解决这个问题的方法是先把图像读入内存,然后再把它发送出去。使用一步调用webhdfsapi而不是两步调用。希望这能有点帮助。

host_url = current_app.config.get('HDFS_URL', '')
adx_img_path = current_app.config.get('ADX_CUSTOMER_IMAGE', '')
real_path = adx_img_path + remotefile
hdfs_username = current_app.config.get('HDFS_USERNAME', 'xdisk')
parameters = '?user.name=' + hdfs_username + '&op=CREATE&data=true'
img = open(localfile, 'rb').read()
url = host_url + real_path + parameters
r = requests.put(url, data=img, headers={"Content-Type": "application/octet-stream"})

通过以二进制/字节的形式读取图像,似乎不会将奇怪的头添加到文件头中。对于您正在使用的httpclient,我建议您尝试 InputStreamBody 或者 ByteArrayBody .

bejyjqdl

bejyjqdl3#

这是基于公认答案的代码:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.File;
import java.io.IOException;

public class Test {

    public void Test(){
        try {

            final String writeURI = "http://<IP>:50075/webhdfs/v1/user/sample.xml?op=CREATE&user.name=istvan&namenoderpcaddress=quickstart.cloudera:8020&overwrite=true";

            HttpClient client = HttpClientBuilder.create().build();

            HttpPut put = new HttpPut(writeURI);
            put.setEntity(buildFileEntity("C:\\sample.xml"));
            put.setHeader("Content-Type", "application/octet-stream");
            HttpResponse response = client.execute(put);

            System.out.println(response);

        }catch(IOException e){
            e.printStackTrace();
        }
    }

    private static FileEntity buildFileEntity (String fileName)
    {
        FileEntity inputData = new FileEntity(new File(fileName));

        return inputData;
    }

    public static void main(String[] args) {
        new Test().Test();
    }
}

Maven:

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.1</version>
        </dependency>

相关问题