Android 如何使用 Retrofit 将图像上传到服务器
How can upload image to server with Retrofit on Android
在我的应用程序中,我想将图像上传到服务器,为此,我使用了 Retrofit2。
我写了下面的代码,但是上传图片后显示服务器错误!
这个错误是:媒体字段为空,请填写这个!
这个错误来自我的服务器,告诉我媒体字段是空的!
我的API代码:
@Multipart
@POST("/media")
fun uploadImage(
@Header(AUTHORIZATION) auth: String, @Header(ACCEPT) accept: String, @Header(CONTENT_TYPE) contentType: String,
@PartMap map: LinkedHashMap<String, RequestBody>
): Single<Response<ResponseModelUploadImage>>
我的Activity代码:
var requestBody: RequestBody
var body: MultipartBody.Part
val mapRequestBody = LinkedHashMap<String, RequestBody>()
Log.e("filePath",uploadNaturalImageFile.toString())
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), uploadNaturalImageFile);
mapRequestBody.put("media\"; media=\"" + uploadNaturalImageFile.name, requestBody);
presenter.callUploadImage(userToken, APPLICATION_JSON, APPLICATION_JSON, mapRequestBody)
但是用Postman上传这张图片,一切正常,没有任何问题!
邮递员请求图片:
更新: 我看到我的日志并显示给我 name=media
,但服务器媒体再次为空!
我的 logcat 消息:
D/OkHttp: Content-Disposition: form-data; name="media"; filename="JPEG_20201108_1623315560915977415445829.jpg"
为什么要显示这个错误?我该如何解决?
在你的 RetrofitService.java
@Multipart
@POST("/app/uploadFile.do")
Call<JsonObject> uploadFile(@PartMap() LinkedHashMap<String, RequestBody> partMap, @Part List<MultipartBody.Part> names);
并在您的 activity、
public static void fileUpload (File file) {
Log.d(TAG, "file===" + file.getName());
RequestBody requestBody;
MultipartBody.Part body;
LinkedHashMap<String, RequestBody> mapRequestBody = new LinkedHashMap<String, RequestBody>();
List<MultipartBody.Part> arrBody = new ArrayList<>();
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
mapRequestBody.put("file\"; filename=\"" + file.getName(), requestBody);
mapRequestBody.put("test", RequestBody.create(MediaType.parse("text/plain"), "gogogogogogogog"));
body = MultipartBody.Part.createFormData("fileName", file.getName(), requestBody);
arrBody.add(body);
Call<JsonObject> call = RetrofitImg.getInstance().getService().uploadFile(mapRequestBody, arrBody);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.body() != null) {
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e(TAG + "Err", t.getMessage());
}
});
}
更新:我找到了其他例子
@POST("my/files/photo/")
Call<FileUploadResponse> uploadPhoto(@Header("Content-Type") String contentType,
@Header("Authorization") String auth,
@Body 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);
在我的应用程序中,我想将图像上传到服务器,为此,我使用了 Retrofit2。
我写了下面的代码,但是上传图片后显示服务器错误! 这个错误是:媒体字段为空,请填写这个!
这个错误来自我的服务器,告诉我媒体字段是空的!
我的API代码:
@Multipart
@POST("/media")
fun uploadImage(
@Header(AUTHORIZATION) auth: String, @Header(ACCEPT) accept: String, @Header(CONTENT_TYPE) contentType: String,
@PartMap map: LinkedHashMap<String, RequestBody>
): Single<Response<ResponseModelUploadImage>>
我的Activity代码:
var requestBody: RequestBody
var body: MultipartBody.Part
val mapRequestBody = LinkedHashMap<String, RequestBody>()
Log.e("filePath",uploadNaturalImageFile.toString())
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), uploadNaturalImageFile);
mapRequestBody.put("media\"; media=\"" + uploadNaturalImageFile.name, requestBody);
presenter.callUploadImage(userToken, APPLICATION_JSON, APPLICATION_JSON, mapRequestBody)
但是用Postman上传这张图片,一切正常,没有任何问题!
邮递员请求图片:
更新: 我看到我的日志并显示给我 name=media
,但服务器媒体再次为空!
我的 logcat 消息:
D/OkHttp: Content-Disposition: form-data; name="media"; filename="JPEG_20201108_1623315560915977415445829.jpg"
为什么要显示这个错误?我该如何解决?
在你的 RetrofitService.java
@Multipart
@POST("/app/uploadFile.do")
Call<JsonObject> uploadFile(@PartMap() LinkedHashMap<String, RequestBody> partMap, @Part List<MultipartBody.Part> names);
并在您的 activity、
public static void fileUpload (File file) {
Log.d(TAG, "file===" + file.getName());
RequestBody requestBody;
MultipartBody.Part body;
LinkedHashMap<String, RequestBody> mapRequestBody = new LinkedHashMap<String, RequestBody>();
List<MultipartBody.Part> arrBody = new ArrayList<>();
requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
mapRequestBody.put("file\"; filename=\"" + file.getName(), requestBody);
mapRequestBody.put("test", RequestBody.create(MediaType.parse("text/plain"), "gogogogogogogog"));
body = MultipartBody.Part.createFormData("fileName", file.getName(), requestBody);
arrBody.add(body);
Call<JsonObject> call = RetrofitImg.getInstance().getService().uploadFile(mapRequestBody, arrBody);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.body() != null) {
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e(TAG + "Err", t.getMessage());
}
});
}
更新:我找到了其他例子
@POST("my/files/photo/")
Call<FileUploadResponse> uploadPhoto(@Header("Content-Type") String contentType,
@Header("Authorization") String auth,
@Body 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);