如何使用Unirest库在服务器上上传图像

时间:2017-03-06 13:10:51

标签: java android unirest

我使用Unirest库在服务器上上传图片,但收到错误

{"code":400,"message":"Unknown content type [contentType=application/octet-stream]"}

我的代码是

 Unirest.post(url).header("Content-Type", "multipart/form-data")                     .header("x-mysolomeo-session-key", "77e02e5c-bdac-4906-bb6c-90c6001b75a3.2017-03-03T17:15:10.101Z").field("token", "77e02e5c-bdac-4906-bb6c-90c6001b75a3")                     .field("documentType","Picture ID").field("documentImage", new File(new URI("file:///storage/emulated/0/logo.png"))).asString()

有人可以帮我吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

我使用Java中的Unirest上传文件大致如下:

    File file =  somefile;
    byte[] data = Files.readAllBytes(file.toPath());
    final HttpResponse<String> stringHttpResponse = Unirest.put("Your Url")
        .header("Content-Type", mediaType)
        .body(data)
        .asString();
    if (stringHttpResponse.getStatus() != 200)
    {
        // error
    }