在Java中将内容类型的文件上传为application / octet-stream?

时间:2019-04-17 16:46:12

标签: java file-upload

我必须将文件上传到服务器,该服务器的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

0 个答案:

没有答案