首页 > 解决方案 > 改造如何上传这样的文件 https://xxxxxx?filename=myfile.dat

问题描述

当我将图像上传到服务器时,总是出错。请求地址为https://xxx/uploads.json?filename=myfile.dat “Content-Type: application/binary”

我的代码是

@Headers("Content-Type: application/binary")
    @POST("/xxx/uploads.json")
    @Multipart
    Observable<UploadResponse> upload(@Header("Authorization") String Authorization, @Part("filename") RequestBody file);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),file);
service.upload(token,requestBody)
...

但总是告诉我文件名值是空的。

我这样测试没问题

@Headers("Content-Type: application/binary")
    @POST("/xxx/uploads.json?123.png")
    @Multipart
    Observable<UploadResponse> upload(@Header("Authorization") String Authorization, @Part("filename") RequestBody file);

但我不知道这种类型如何上传图片。谢谢提前

标签: androidretrofit

解决方案


试试这个代码。使用 RXjava 进行改造

MultipartBody.Part body;
RequestBody rBody;
if (file != null) {
        ind_progressBar.setVisibility(View.VISIBLE);
        btn_ok.setEnabled(false);
        alertDialog.show();
        rBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        body = MultipartBody.Part.createFormData("FileName", file.getName(), rBody);
}
RequestBody Name = RequestBody.create(okhttp3.MultipartBody.FORM, name);
    RequestBody Email = RequestBody.create(okhttp3.MultipartBody.FORM, email);`
SendInquiryAPI.RetrofitAdapter.CustomApi().sendInqiry(AppID, Comments, Name, Email,
            YouTubeLink, SectionName, body, FileType)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<MessageFromResponces>() {
                @Override
                public void onSubscribe(Disposable d) {
                    finalBtn_cancel.setOnClickListener(view -> {
                        d.dispose();
                        alertDialog.dismiss();
                    });
                }

                @Override
                public void onNext(MessageFromResponces message) { 
 // here you will get response

  }
// here is the Retrofit Adapter 
 String baseUrl = "your base Url here";
  @Multipart
@POST("your link address")
Observable<Signupmodel> getSignupProfile(@Part("AppId") RequestBody AppId, 
@Part("Name") RequestBody name, @Part("email") RequestBody email
        , @Part("phone") RequestBody phone, @Part("cityID") RequestBody cityId,  
@Part("pswd") RequestBody password, @Part MultipartBody.Part file);
class RetrofitAdapter {
    public static SignUpApi CustomApi() {
        final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        return retrofit.create(SignUpApi.class);

    }
}

`


推荐阅读