android - 改造如何上传这样的文件 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);
但我不知道这种类型如何上传图片。谢谢提前
解决方案
试试这个代码。使用 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);
}
}
`
推荐阅读
- c# - 每次不使用创建不同的数据库上下文有什么问题吗
- java - 检查流的所有对象是否符合规则
- elasticsearch - 嵌套对象中的脚本字段,特别是 geo_shapes
- c++ - 该程序不返回任何内容
- java - 如何在 VS Code 中为 Java 启用语法高亮
- kubernetes - Kubernetes nginx 入口重写问题
- c# - 在构造函数中启动任务是否有问题?
- sql - DB2 BLOB 如何做 MD5?
- html - 在 Web 浏览器 MFC 中打开新窗口时如何获取新网页的 URL
- java - 尝试在 WSO2 身份服务器中创建 ServiceProvider 时出现 ArrayIndexOutOfBoundsException