长度要求411:使用apache(4.3)HttpPost和MultipartEntityBuilder上传文件。
我正在使用HttpClient 4.3.3。我的任务是把文件上传到服务器上。 以下是我的代码:长度要求411:使用apache(4.3)HttpPost和MultipartEntityBuilder上传文件。,apache,apache-httpclient-4.x,Apache,Apache Httpclient 4.x,我正在使用HttpClient 4.3.3。我的任务是把文件上传到服务器上。 以下是我的代码: InputStream inputStream = new FileInputStream(new File("/path/to/file")); byte[] data; data = IOUtils.toByteArray(inputStream); InputStreamBody inputStreamBody = new InputStreamBody(new
InputStream inputStream = new FileInputStream(new File("/path/to/file"));
byte[] data;
data = IOUtils.toByteArray(inputStream);
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data),"file-name-on-ser");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file",inputStreamBody);
HttpPost httpPost = new HttpPost("file upload URL");
httpPost.setEntity(multipartEntity.build());
CloseableHttpResponse response = httpclient.execute(httpPost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
当我运行此代码时,它成功地连接到服务器
<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>server-name</center>
</body>
</html>
411所需长度
411所需长度
服务器名称
当我使用firebug进行调试时,我看到以下请求头
响应标题查看来源
缓存控制无存储,无缓存
连接保持活动状态
日期2014年5月22日星期四08:58:02 GMT
保持活动超时=30
服务器*****
设置Cookie****路径=/
传输编码分块
请求标题查看来源
接受text/html、application/xhtml+xml、application/xml;q=0.9,/;q=0.8
接受编码gzip,放气
接受我们的语言,嗯;q=0.5
连接保持活动状态
饼干****
主人*****
参考https://**/shareupload/
用户代理Mozilla/5.0(Windows NT 6.1;rv:29.0)Gecko/20100101 Firefox/29.0
从上传流请求头文件
内容长度12642
内容类型多部分/表单数据;边界=------------------------------------105143784893
我在堆栈溢出方面也看到过类似的问题
(一)
(二)
但我仍然无法找到解决问题的办法。
是否需要在请求标头中设置内容长度和内容类型参数
先谢谢你 试试看:
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data),"file-name-on-ser"){
@Override
public long getContentLength(){return data.length();}
};
我找到了。这有助于解决问题。