文件上传与okhttp [英] File upload with okhttp

查看:330
本文介绍了文件上传与okhttp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林完成这个项目,该项目是利用okhttp用于与web服务。

Im finishing this project which is using okhttp for communication with a webservice.

一切是怎么回事精定期GET和POST,但我不能够正确地上传文件。

All is going fine for regular GETs and POSTs, but I'm not being able to properly upload a file.

在okhttp文档都非常缺乏有关这些主题和一切,我发现这里或其他地方似乎没有在我的情况下工作。

The okhttp docs are very lacking on these subjects and everything I found here or anywhere don't seem to work in my case.

这应该是简单的:我要送这两个文件和一些字符串值。但我不能想出如何做到这一点。

It's supposed to be simple: I have to send both the file and some string values. But I can't figured out how to do it.

继一些样品,我发现,我第一次尝试这样的:

Following the some samples I found, I first tried this:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
    .addFormDataPart("group", getGroup())
    .addFormDataPart("type", getType())
    .addFormDataPart("entity", Integer.toString(getEntity()))
    .addFormDataPart("reference", Integer.toString(getReference()))
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"task_file\""), RequestBody.create(MediaType.parse("image/png"), getFile()))
    .build();

这给了我一个400错误的请求的错误。

It gives me a "400 bad request" error.

所以,我想这个从okhttp食谱:

So I tried this from the okhttp recipes:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"group\""), RequestBody.create(null, getGroup()))
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"type\""), RequestBody.create(null, getType()))
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"entity\""), RequestBody.create(null, Integer.toString(getEntity())))
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"reference\""), RequestBody.create(null, Integer.toString(getReference())))
    .addPart(Headers.of("Content-Disposition", "form-data; name=\"task_file\""), RequestBody.create(MediaType.parse("image/png"), getFile()))
    .build();

同样的结果。

Same result.

不知道还有什么尝试,或什么考虑调试此。

Don't know what else to try or what look into to debug this.

请求与此code完成的:

The request is done with this code:

// adds the required authentication token
Request request = new Request.Builder().url(getURL()).addHeader("X-Auth-Token", getUser().getToken().toString()).post(requestBody).build();
Response response = client.newCall(request).execute();

但林pretty的肯定,问题是林如何构建请求主体。

But Im pretty sure that the problem is how Im building the request body.

我是什么做错了吗?

编辑:的getFile()上方返回一个文件对象的方式。该参数的其余部分都是字符串和整数。

"getFile()" above returns the a File object, by the way. The rest of the parameters are all strings and ints.

推荐答案

我找到答案我自己的问题了一下最初的帖子后。

I found answer to my own question a bit after initial post.

我马上离开这里,因为它可以对别人有用,因为有这样几个okhttp上传身边的例子:

I´ll leave it here, because it can be useful to others, given that there is such a few okhttp upload examples around:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
        .addFormDataPart("group", getGroup())
        .addFormDataPart("type", getType())
        .addFormDataPart("entity", Integer.toString(getEntity()))
        .addFormDataPart("reference", Integer.toString(getReference()))
        .addFormDataPart("task_file", "file.png", RequestBody.create(MediaType.parse("image/png"), getFile()))
                                                .build();

没有理由使用addPart与Headers.of等类似的食谱,addFormDataPart是卓有成效的。

There is no reason to use "addPart" with "Headers.of" etc like in the recipes, addFormDataPart does the trick.

和文件领域本身,它需要3个参数:名称,文件名的文件,然后身体。就是这样。

And for the file field itself, it takes 3 arguments: name, filename and then the file body. That's it.

这篇关于文件上传与okhttp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆