Отправить файл с Android на сервлет через HOSTPConnection POST - PullRequest
2 голосов
/ 02 ноября 2010

У меня есть этот код на моем андроиде:

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();
    }
}

Этот код для стороны сервлета:

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.Я не уверен, что было бы главной проблемой здесь.Мне нужна помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...