我正在使用MultiPartEntity进行HttpPut,通过webHDFS REST API将文件写入HDFS。请求本身经过并给出正确的响应,307和201.但是图像的多部分标题也作为其一部分写入,如下所示,它不是一个有效的图像来检索和打开。
- 8DkJ3RkUHahEaNE9Ktw8NC1TFOqegjfA9Ps
内容处理:表格数据; NAME = “文件”;文件名= “advert.jpg”
内容类型: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();
}
感谢任何帮助。
答案 0 :(得分: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,我建议您尝试InputStreamBody
或ByteArrayBody
。
答案 1 :(得分:1)
使用Content-Type&#34; application / octet-stream&#34;将图像添加为FileEntity,ByteArrayEntity或InputStreamEntity。
答案 2 :(得分:0)
这是根据接受的答案为我工作的代码:
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>