1

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

--8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps
内容配置:表单数据;名称=“文件”;filename="advert.jpg"
Content-Type: application/octet-stream

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

任何帮助表示赞赏。

4

3 回答 3

1

我在 python 请求中遇到了同样的问题。我最终解决它的方法是在发送之前将图像读入内存。并使用一步调用 webhdfs api 而不是两步。希望这可以有点帮助。

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,我建议您尝试InputStreamBodyByteArrayBody.

于 2014-04-29T09:32:25.577 回答
1

将图像添加为 FileEntity、ByteArrayEntity 或 InputStreamEntity,Content-Type 为“application/octet-stream”。

于 2014-05-02T17:17:34.383 回答
1

这是基于接受的答案对我有用的代码:

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

马文:

        <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>
于 2016-11-17T09:56:58.367 回答