我想上传带有 JSON 对象的文件。我正在使用 Retrofit2,但收到 400 Bad request。我使用 curl 的请求示例:
curl -X POST http://localhost:8082/attachment -F filename=37.pdf -F 'data={"DocumentTypeID":2, "DocumentID":1, "Description":"описание","AttachmentTypeId":2}'
我也向邮递员提出了请求,它也有效:
我的Java代码:
Uri path = Uri.fromFile(file);
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getMimeType(path)),
file
);
MultipartBody.Part body =
MultipartBody.Part.createFormData("filename", file.getName(), requestFile);
FileDescriptionObject fdo = new FileDescriptionObject();
fdo.setDescription("test");
fdo.setDocumentId(fileModel.Id);
fdo.setDocumentTypeId(1);
fdo.setAttachmentTypeId(2);
Gson gson = new Gson();
String ds1 = gson.toJson(fdo);
RequestBody description =
RequestBody.create(
MediaType.parse("text/plain"), ds1);
Call<ResponseBody> call = activity.getAsyncHelper().getWebService().postFile(
"http://localhost:8082/attachment",
body,
description);
我的API:
@Multipart
@POST
Call<ResponseBody> postFile(@Url String url,
@Part MultipartBody.Part file,
@Part("data")RequestBody data);
我的日志:
D/OkHttp: --> POST http://localhost:8082/attachment
Content-Type: multipart/form-data; boundary=520da8f2-5fac-4567-be0f-61618cc881bd
D/OkHttp: Content-Length: 468
D/OkHttp: --520da8f2-5fac-4567-be0f-61618cc881bd
D/OkHttp: Content-Disposition: form-data; name="filename"; filename="4.pdf"
Content-Type: application/pdf
Content-Length: 3
323
--520da8f2-5fac-4567-be0f-61618cc881bd
也许问题出在这个来自 json 数据的附加头文件中?我认为是因为在邮递员中没有添加它们。
慕尼黑8549860
相关分类