【发布时间】:2015-09-18 16:22:52
【问题描述】:
首先,我问了这个问题What did I miss to send a http part post request,似乎我不明白从客户端向服务器发送多部分的逻辑。使用这个帖子https://developer.constantcontact.com/docs/mylibrary-files-api/file-add-multipart-post.html我无法看到每个部分的必填字段。
我构建了我的请求并添加了您在此处看到的所有必填字段
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(
"http://localhost:8080/ServletExample1/multipart1");
httpPost.addHeader("Content-Type",
"multipart/related; boundary=HereItGoes");
httpPost.addHeader("Accept", MediaType.TEXT_PLAIN);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
FileBody bin = new FileBody(new File("./test.txt"));
builder.addPart("source", new StringBody("MyComputer",
ContentType.TEXT_PLAIN));
builder.addPart("folder_id", new StringBody("-1",
ContentType.TEXT_PLAIN));
builder.addPart("file_type", new StringBody("txt",
ContentType.TEXT_PLAIN));
builder.addPart("file_name", new StringBody("test.txt",
ContentType.TEXT_PLAIN));
builder.addPart("description", new StringBody("The file to test",
ContentType.TEXT_PLAIN));
builder.addPart("data", bin);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
String responseString = new BasicResponseHandler()
.handleResponse(response);
System.out.println(responseString);
我的问题出在服务器上,我一直收到零件数为零的消息。对于服务器未收到零件的某些人(请注意,我并不是说服务器中存在异常)
我这样说是因为我在服务器(我的 servlet)上这样做
Iterator<Part> partsIterator = request.getParts().iterator();
System.out.println("The number of parts is :"
+ request.getParts().size());
而且打印的结果总是零,总是
请问我错过了什么?
【问题讨论】:
标签: java servlets http-post multipartform-data multipart