我正在尝试使用我在网上找到的这个类将多部分表单数据从Android活动上传到Web服务器。该类可以随时使用它将三个或更多表单数据发送到Web服务器,但是当我尝试使用它来提交一个只包含两个字段的表单时,它只发送一个表单数据(第一个)。我不太了解网络协议,除了基础知识(200等),这就是为什么我首先需要第三方课程,或者我会写我的,加上它很容易使用。如果有人可以为我重写并测试它,我将非常感激。一个非常简单易用的替代品也将受到赞赏。谢谢。班级:
public class MultipartUtility {
private final Logger log = getLogger(MultipartUtility.class
.getName());
private static final String CRLF = "\n";
private static final String CHARSET = "UTF-8";
private static final int CONNECT_TIMEOUT = 15000;
private static final int READ_TIMEOUT = 15000;
private final HttpURLConnection connection;
private final OutputStream outputStream;
private final PrintWriter writer;
private final String boundary;
// for log formatting only
private final URL url;
private final long start;
public MultipartUtility(final URL url) throws IOException {
start = currentTimeMillis();
this.url = url;
boundary = "---------------------------" + currentTimeMillis();
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", CHARSET);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
outputStream = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET),
true);
}
public void addFormField(final String name, final String value) {
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"").append(name)
.append("\"").append(CRLF)
.append("Content-Type: text/plain; charset=").append(CHARSET)
.append(CRLF).append(CRLF).append(value).append(CRLF);
}
public void addFilePart(final String fieldName, final File uploadFile)
throws IOException {
final String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(CRLF)
.append("Content-Disposition: form-data; name=\"")
.append(fieldName).append("\"; filename=\"").append(fileName)
.append("\"").append(CRLF).append("Content-Type: ")
.append(guessContentTypeFromName(fileName)).append(CRLF)
.append("Content-Transfer-Encoding: binary").append(CRLF)
.append(CRLF);
writer.flush();
outputStream.flush();
try {
final FileInputStream inputStream = new FileInputStream(uploadFile);
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
}catch (IOException e) {
e.printStackTrace();
}
writer.append(CRLF);
}
public void addHeaderField(String name, String value) {
writer.append(name).append(": ").append(value).append(CRLF);
}
public byte[] finish() throws IOException {
writer.append(CRLF).append("--").append(boundary).append("--")
.append(CRLF);
writer.close();
final int status = connection.getResponseCode();
if (status != HTTP_OK) {
throw new IOException(format("{0} failed with HTTP status: {1}",
url, status));
}
try {
final InputStream is = connection.getInputStream();
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
bytes.write(buffer, 0, bytesRead);
}
is.close(); //close
log.log(INFO,
format("{0} took {4} ms", url,
(currentTimeMillis() - start)));
return bytes.toByteArray();
} finally {
connection.disconnect();
}
}
}