我正在创建一个应用程序,我必须将图像从图库发送到服务器。
这些应该是标题(根据养蜂场)。
Content-Type:image/jpeg
X-Filename:photo.jpg
但是在拦截器的日志中,只有X-Filename,即使我尝试了多种方式,但我的想法已经不多了。
服务器以200响应,但下一次调用(提交图像)在发布结束时返回500并显示错误。
这些是方法:
@Multipart
@POST("uploads")
Observable<String> uploadImage(
@Header("Content-Type") String contentType,
@Header("X-Filename") String fileName,
@Header("Authorization") String token,
@Part MultipartBody.Part file
);
@GET("uploads/{fileId}/commit")
Observable<String> commitFile(
@Header("Content-Type") String type,
@Header("Authorization") String token,
@Path("fileId") String fileId
);
OkHttp客户端:
private OkHttpClient createApiClient(Context context) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(context.getCacheDir(), cacheSize);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(chain -> {
Request request = chain.request().newBuilder().build();
request.newBuilder().addHeader("Content-Type", "application/json");
Timber.d("Request: [%s] %s", request.method(), request.url());
Timber.d("Request: %s", request.headers().toString());
if (request.body() != null) {
Timber.d("Request: %s", request.body().toString());
}
return chain.proceed(request);
});
builder.cache(cache);
return builder.build();
}
产生的日志:
D/NetModule: Request: [POST] https://winged-guild-133523.appspot.com/api/v1/uploads
D/NetModule: Request: X-Filename: asura.png
Authorization: HAYVi3clcqjug53IPS6AYMgzCPGnDE3Si
D/NetModule: Request: retrofit2.RequestBuilder$ContentTypeOverridingRequestBody@7c37f46
Postman发生同样的事情 - Postman screenshot
提交失败并出现此错误:(对于难看的格式化而言)
{
"errorCode": "ImagesServiceFailureException",
"errorParam": "",
"errorMessage":"com.google.appengine.api.images.ImagesServiceFailureException: "
}
正如我所说,我的想法已经不多了,非常感谢有关此上传问题的任何帮助。
(编辑)PS:在app中省略 @Header(“Content-Type”)字符串contentType 并使用 application / x-www-form-邮递员中的urlencoded ,图片上传和提交都是成功的,但返回的网址上的图片没有元数据,因此无法显示。
答案 0 :(得分:0)
这是我使用改装的代码
@Multipart
@POST(ServiceConfig.API_CREATE_DEPENDENT_ACCOUNT)
@Headers({
"Accept: application/json",
"Accept-Encoding: gzip"
})
Call<DataResponse<CreateDependentAccountResponse>> createDependentAccount(@Query(JioConstants.ParamQuery.TOKEN) String token,
@Query(JioConstants.ParamQuery.USER_ID) long userID,
@Query(JioConstants.ParamQuery.FIRST_NAME) String firstName,
@Query(JioConstants.ParamQuery.LAST_NAME) String lastName,
@Query(JioConstants.ParamQuery.GENDER) String gender,
@Query(JioConstants.ParamQuery.DOB) String dob,
@Query(JioConstants.ParamQuery.RELATIONSHIP_ID) long relationshipID,
@Query(JioConstants.ParamQuery.DISPLAY_UNIT) String displayUnit,
@Part("avatar\"; filename=\"picture.jpg\" ") RequestBody file);
和
RequestBody file = null;
if (mFileTemp != null) {
file = RequestBody.create(MediaType.parse("image/*"), mFileTemp);
} else {
file = RequestBody.create(MediaType.parse("image/*"), "");
}
manageCall(ServiceManager.INSTANCE2().
createDependentAccount(token, userID, firstName, lastName, gender, dob, relationshipID, displayUnit, file))
.enqueue(new SimplifiedCallback<CreateDependentAccountResponse>() {
@Override
public void success(CreateDependentAccountResponse data, String message) {
hideLoading();
答案 1 :(得分:0)
请勿使用@Multipart
。相反,请使用以下内容:
@POST("upload/image")
Observable<ResponseBody> uploadImage(@Body RequestBody body);
File imageFile = new File(path);
RequestBody body = RequestBody.create(MediaType.parse("image/jpeg"),imageFile);
api.uploadImage(body);