우체부에서 파일을 보내고 일부 정보를 내 서버에 보내도록 쿼리를 생성했습니다. 그것은 내 프로그램에서 잘 작동합니다.PartMap의 Retrofit 및 WebKitFormBoundary로 서버에 파일 보내기
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType,
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"C:\\testFile.doc\"\r\nContent-Type: application/msword\r\n\r\n\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n7\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\n3\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"note\"\r\n\r\nafejhejfh\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"list[0][id]\"\r\n\r\n1\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"list[0][is_checked]\"\r\n\r\n0\r\n" +
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"list[0][filename]\"; filename=\"C:\\test.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
개조 방법
@Headers({"Accept: application/json", "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"})
@POST("api/save")
Call<SaveResponse> save(@Header("Authorization") String authorization,
@Body RequestBody map);
최초는 webkitformboundary 직면 동적 Partmap를 생성하는 샘플 위에 변경할 필요가있다. 왜냐하면 다른 목록의 한주기에 일부 매개 변수를 추가해야하기 때문입니다. 서버
쿼리지도에서 코드 반환 (422) 오류가 아래 API 문서
--header 'Authorization: {{authorization}}' \
--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
에 쓴 것처럼 나는 예를 아래와 같이 뭔가 partmap를 생성하고 헤더에 "WebKitFormBoundary7MA4YWxkTrZu0gW"를 추가했습니다.
Map<String, RequestBody> requestMap = new HashMap<>();
RequestBody file = null;
if (file != null) {
file = RequestBody.create(MediaType.parse("application/msword"), testFile);
requestMap.put("filename", file);
}
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), "18");
RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "3");
RequestBody notes = RequestBody.create(MediaType.parse("text/plain"), notesTextArea.getText());
RequestBody list0id = RequestBody.create(MediaType.parse("text/plain"), "1");
RequestBody list0is_checked = RequestBody.create(MediaType.parse("text/plain"), "0");
RequestBody pdfFilename = null;
if (pdfFile != null) {
pdfFilename = RequestBody.create(MediaType.parse("application/pdf"), pdfFile);
requestMap.put("list[0][filename]", pdfFilename);
}
requestMap.put("file", file);
requestMap.put("id", id);
requestMap.put("type", type);
requestMap.put("notes", notes);
requestMap.put("list[0]id", list0id);
requestMap.put("list[0]is_checked", list0is_checked);
개조 방법 :
@Multipart
@Headers({"Accept: application/json", "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"})
@POST("api/save")
Call<SaveResponse> save(@Header("Authorization") String authorization,
@PartMap Body map);
그래서 내가 WebKitFormBoundary7MA4YWxkTrZu0gW 내 Partmap 다른 필요한 정보를 넣을 수있는 방법을 이해하는 데 어떤 도움이 필요합니다.
도움을 주셔서 미리 감사드립니다.
p.s. 새해 복 많이 받으세요 코더)