Retrofit - 多部分请求:需要的MultipartFile参数'file'不存在。

32

我正在尝试使用Retrofit2将文件发送到服务器。 按照文档中的说明进行了一切操作,但始终收到400服务器错误。

我尝试像这样做:

RequestBody body =
                RequestBody.create(MediaType.parse("image/png"), photo);
    //..........

    @Multipart
    @POST(ADD_PHOTO)
    Observable<HPSPhotoResponse>
    addPhoto(@Part("file") RequestBody file);

...就像这样:

MultipartBody.Part part = MultipartBody.Part.createFormData("file", "file", body);
     //...........

    @Multipart
    @POST(ADD_PHOTO)
    Observable<HPSPhotoResponse>
    addPhoto(@Part("file") MultipartBody.Part files);

无所谓,结果总是一样的:"Multipart request: Required MultipartFile parameter 'file' is not present" - 服务器响应。

我认为服务器上的Spring工作不太好,但我在Swift(iOS)上做了等效的代码,它可以工作!在这里,服务器可以看到这个“文件”部分。

Alamofire.upload(method, endpoint, headers: headers,
            multipartFormData: { multipartFormData in
                multipartFormData.appendBodyPart(fileURL: self.filePath!, name: "file")
            }

现在我希望它可以在Android上使用Retrofit。

但是,即使我查看了Retrofit请求的日志,的确没有在日志中看到任何“file”文本。

这是怎么回事?


这个回答解决了你的问题吗?使用Retrofit 2.0 POST多部分表单数据,包括图像 - Kaibo
2个回答

53
您可以尝试以下示例代码。在此演示应用程序中,我们将从相册选择照片后上传照片。希望它能有所帮助! build.gradle文件:
dependencies {
    ...
    compile 'com.squareup.retrofit2:retrofit:2.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    ...
}

WebAPIService.java文件:

public interface WebAPIService {

    @Multipart
    @POST("/api/fileupload")
    Call<ResponseBody> postFile(@Part MultipartBody.Part file, @Part("description") RequestBody description);
}

FileActivity.java文件:

...
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class FileActivity extends AppCompatActivity {

    private final Context mContext = this;
    private final String API_URL_BASE = "http://serverip:port";
    private final String LOG_TAG = "BNK";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);

        selectImage(); // selects a photo from Gallery
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            Uri fileUri = data.getData();
            if (fileUri != null) {
                uploadFile(fileUri); // uploads the file to the web service
            }
        }
    }

    private void uploadFile(Uri fileUri) {

        String filePath = getRealPathFromUri(fileUri);
        if (filePath != null && !filePath.isEmpty()) {
            File file = new File(filePath);
            if (file.exists()) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(API_URL_BASE)
                        .build();

                WebAPIService service = retrofit.create(WebAPIService.class);

                // creates RequestBody instance from file
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                // MultipartBody.Part is used to send also the actual filename
                MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
                // adds another part within the multipart request
                String descriptionString = "Sample description";
                RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
                // executes the request
                Call<ResponseBody> call = service.postFile(body, description);                
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call,
                                           Response<ResponseBody> response) {
                        Log.i(LOG_TAG, "success");
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Log.e(LOG_TAG, t.getMessage());
                    }
                });
            }
        }
    }

    private void selectImage() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, 100);
    }

    public String getRealPathFromUri(final Uri uri) {
        // DocumentProvider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(mContext, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(mContext, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{
                        split[1]
                };

                return getDataColumn(mContext, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(mContext, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    private String getDataColumn(Context context, Uri uri, String selection,
                                        String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    private boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    private boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    private boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    private boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }
}

2
太好了!!!我能给的最大感谢 :) !!! 它确实有帮助。...主要问题确实在于Retrofit "...retrofit:2.0.0"而不是"..retrofit:2.0.1"。在这个版本中,"MultipartBody.Part"注释出了一些问题。我已经更改了它,现在它的工作方式与iOS完全相同。再次感谢。 - Yura Buyaroff
如何在参数中发送图像?如果我在参数中添加图像,则会出现java.lang.IllegalArgumentException:@Part参数使用MultipartBody.Part时,注释中不得包含部件名称。 - Suman
@Suman 我已经好几个月没有使用Retrofit进行测试了,但是你可以尝试使用@Part("description") RequestBody description,就像我上面的代码或者在http://square.github.io/retrofit/中提到的那样。 - BNK
1
@BNK,我在使用Retrofit2进行multipart POST时遇到了问题,我在这里发布了我的问题:https://dev59.com/5qDia4cB1Zd3GeqPI8Xy。有什么建议吗?谢谢! - Shuwn Yuan Tee
1
太棒了...你救了我的一天。 - andro-girl

3
在我的情况下,服务器没有处理retrofit发送的一些标头。这就是为什么我不得不从retrofit请求中删除无用的标头。我创建了一个如下所示的接口函数:
@Multipart
@POST("my/files/photo/")
Call<FileUploadResponse> uploadPhoto(@Header("Content-Type") String contentType,
                                          @Header("Authorization") String auth,
                                          @Part MultipartBody body);

并且像这样调用:

ApiClient.ApiInterface client = ApiClient.getClient();
File file = new File(getPathFromUri(fileUri));
RequestBody fileBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
MultipartBody body = new MultipartBody.Builder().addFormDataPart("file-type", "profile")
                .addFormDataPart("photo", "image.png", fileBody)
                .build();
client.uploadPhoto("multipart/form-data; boundary=" + body.boundary(),
                    PrefManager.getInstance().getToken(), body);

详情请查看这里:使用Retrofit 2上传图片到服务器


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接