我是从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"
在没有运气的情况下咀嚼了几个小时。非常感谢任何帮助。
答案 0 :(得分:0)
感谢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());