我必须将文件上传到服务器,该服务器的API是内容类型为application/octet-stream
的PUT请求。以下是我的代码
private void uploadVideo(File binaryFile, String url) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
FileEntity reqEntity = new FileEntity(binaryFile, "application/octet-stream");
reqEntity.setContentType("application/octet-stream");
put.setEntity(reqEntity);
HttpResponse response = httpclient.execute(put);
HttpEntity entity = response.getEntity();
Log.d("INFO", "rcode:" + response.getStatusLine().toString());
if (response.getStatusLine().getStatusCode() - 200 >= 100)
throw new Exception("bad status code: " + response.getStatusLine().getStatusCode());
String responseString = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
}
}
我总是收到不好的要求,但是当我从邮递员那里尝试时,文件上传就可以了。有解决方案吗?
bad status code: 400