通过 HttpConnection POST 将文件从 Android 发送到 Servlet
我的 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;
}
问题是代码在到达 Connector.open("http://35.9.22.102:8080/Services/SubmitMedia/")。程序会跳过整个过程并转到
finally
。我不确定这里的主要问题是什么。我需要一些帮助。
I have this code on my 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();
}
}
This code for the servlet side:
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;
}
The problem is the code broke when it reaches Connector.open("http://35.9.22.102:8080/Services/SubmitMedia/")
on the Android part. The program skips whole thing and goes to finally
. I am not sure what would be the main problem in here. I need some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论