我在服务器上发送了multipart / form-data,但未发送Error。没什么!简单的200码请求。我尝试发送多部分的自己。
我添加标题:
connection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
并发送正文:
--------b3a2fb0a4742753e0c507301a6bcb2bc
Content-Disposition: form-data; name="Test"
test
--------b3a2fb0a4742753e0c507301a6bcb2bc--
但是PHP没有显示$ _POST变量Test
Java代码
void onWriteBody(String boundary, OutputStream outputStream) throws Exception {
for (int i = 0; i < data.size(); i++) {
// Reference for data
Data data = this.data.get(i);
// Start multi-part/data
outputStream.write((boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
// Add content-disposition
outputStream.write(("Content-Disposition: form-data; name=\"" + URLEncoder.encode(data.getName(), "UTF-8") + "\"").getBytes(StandardCharsets.UTF_8));
// If file then add tag for file
if (data instanceof Data.File) {
outputStream.write(("; filename=\"" + URLEncoder.encode(((Data.File) data).getFileName(), "UTF-8") + "\"\r\n").getBytes(StandardCharsets.UTF_8));
outputStream.write(("Content-Type: " + ((Data.File) data).getContentType()).getBytes(StandardCharsets.UTF_8));
}
// Add new line
outputStream.write("\r\n\r\n".getBytes());
// Write to output stream
this.writeToOutputStream(outputStream, data);
// End multi-part/data
outputStream.write(("\r\n" + boundary + (i == this.data.size() - 1 ? "--" : "\r\n")).getBytes(StandardCharsets.UTF_8));
}
// After end the operation close output stream
outputStream.flush();
outputStream.close();
}