2

我的 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/")Android 部分时损坏了。程序跳过整个过程并转到finally. 我不确定这里的主要问题是什么。我需要一些帮助。

4

0 回答 0