OkHttpClient - 如何将“[object Object]”设置为本地文件

OkHttpClient - How to set "[object Object]" to local file

我从 Postman 生成了这个代码片段并想在 Talend 中使用它,但我不知道如何设置 文件名 以便它从本地驱动器中提取。这是代码:

OkHttpClient client = new OkHttpClient();

File sourceFile = new File("/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png");

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"uploadedFile\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001--");
Request request = new Request.Builder()
  .url("https://secret.attask-ondemand.com/attask/api-internal/upload/?apiKey=secret")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();
String json = response.body().string();
System.out.println(json.toString());

这是我要在 [object Object] 中引用的本地文件:“/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png”

苦苦思索了好几个小时都没有成功。非常感谢任何帮助。

感谢 BrianPipa 的帮助。特别是对于 Workfront,表单键必须是 "uploadedFile" 并且值需要与引用的文件相匹配。与使用字符串相比,使用 MultipartBody 更容易构建请求主体。最终代码:

File sourceFile = new File("/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png");

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

RequestBody body = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)                                     
                   .addFormDataPart("uploadedFile", "Digital Storefront Receipt.png", RequestBody.create(MEDIA_TYPE_PNG ,sourceFile))
                   .build();

Request request = new Request.Builder()
  .url("https://secret.attask-ondemand.com/attask/api-internal/upload/?apiKey=secret")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
  .addHeader("cache-control", "no-cache")
  .build();

OkHttpClient client = new OkHttpClient();

Response response = client.newCall(request).execute();
String json = response.body().string();
System.out.println(json.toString());