通过 HttpConnection POST 将文件从 Android 发送到 Servlet

发布于 2024-09-30 11:23:32 字数 1628 浏览 5 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文