我在我的android上有这个代码:
try {
http = (HttpConnection) Connector.open("http://35.9.22.102:8080/Services/SubmitMedia/");
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Connection", "keep-alive");
http.setRequestProperty("Content-Type", "multipart/form-data");
http.setRequestProperty("Content-Length", file.length + "");
dos = http.openDataOutputStream();
dos.write(file);
int response = ((HttpConnection) http).getResponseCode();
if (response == HttpConnection.HTTP_OK) System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (http != null) http.close();
if (dos != null) dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
servlet端的代码:
DataInputStream din = new DataInputStream(request.getInputStream());
byte[] data = new byte[0];
byte[] buffer = new byte[512];
int bytesRead;
while ((bytesRead = din.read(buffer)) > 0 ) {
// construct large enough array for all the data we now have
byte[] newData = new byte[data.length + bytesRead];
// copy data previously read
System.arraycopy(data, 0, newData, 0, data.length);
// append data newly read
System.arraycopy(buffer, 0, newData, data.length, bytesRead);
// discard the old array in favour of the new one
data = newData;
}
问题是代码在Android部件上达到Connector.open("http://35.9.22.102:8080/Services/SubmitMedia/")
时破了。程序跳过整个过程并转到finally
。我不确定这里的主要问题是什么。我需要一些帮助。