我正在尝试使用帖子将数据发送到网络服务器,这是我到目前为止所拥有的:
private void entity(String id, String file)
throws JSONException, UnsupportedEncodingException, FileNotFoundException {
// Add your data
File myFile = new File(
Environment.getExternalStorageDirectory(), file);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(myFile), myFile.length());
reqEntity.setContentType("text/csv");
reqEntity.setChunked(true); // Send in multiple parts if needed
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("project", id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(reqEntity);
//httppost.setHeader("Content-Length", String.valueOf(myFile.length()));
}
当我发送帖子请求时,它会返回所需的内容长度,但这里不是设置的吗?
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(myFile), myFile.length());
我不知道我这样做是否正确,请帮忙,谢谢
<小时/>编辑-
当我尝试使用
自己设置内容长度时httppost.setHeader("Content-Length", String.valueOf(myFile.length()));
返回时 header 已设置。
这就是为什么它被注释掉
最佳答案
就我而言,使用
reqEntity.setChunked(false);
解决了问题。
关于java - 内容长度?长度需要但已设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10237131/