1

我将文件从诺基亚 N97 手机上传到服务器,一切正常,但文件上传后我想从服务器获得响应。问题是我只能在半分钟或更长时间后得到响应。从我看到的 httpConnection.getResponseCode() 中的代码块等待响应。我在索尼爱立信上进行了测试,我得到了非常快的响应,所以我认为是诺基亚 N97 的问题。(不是服务器问题,因为它在其他手机上运行良好)

有谁知道为什么这件事只发生在诺基亚?

这是一个代码片段:

公共上传文件(){

       httpConn = (HttpsConnection) Connector.open(url, Connector.READ_WRITE);
        ...
       //set httpConn parameters and request method 
       ...

       writeParametersAndFileName(os, "text/plain");


       **writeFileToStream(os);** 

       os.write("\r\n".getBytes());
       os.write("--".getBytes());

       os.write(boundary.getBytes());

       os.write("--".getBytes());
       os.write("\r\n".getBytes());

        *//here code blocks on Nokia N97. Same thing happens if I use os.flush() after
        // I send chunks of the file*
         .....
        codeResp = httpConn.getResponseCode();
        // check condition
        checkResponseHeader();

}

公共writeFileToStream() {

        InputStream is = null;
        FileConnection c = null;
        try
        {
           c = (FileConnection) Connector.open("file:///"+ sourcePath, Connector.READ);

           if(c.exists()) {
               is = c.openInputStream();

               long fs = c.fileSize();
               while (total < fs) {
                    byte[] data = new byte[512];
                    int readAmount = is.read(data,0,512);                       
                    total += readAmount;
                    os.write(data, 0, readAmount);


            }

            //os.flush();
        }
        catch (IOException ex) {
           //               
        }
        finally
        {
           //close connection

        }

}

4

1 回答 1

4

您只认为您的文件已上传。

您对 getResponseCode() 的调用是第一个导致 HttpConnection 实际连接到服务器的调用。

它在上传文件之前不会返回,这可能需要一分钟多的时间。

根据 JSR-118 中的 HttpConnection 规范,这是完全有效的行为:

" 当连接处于设置状态时,以下方法会导致转换到已连接状态。

* openInputStream
* openDataInputStream
* getLength
* getType
* getEncoding
* getHeaderField
* getResponseCode
* getResponseMessage
* getHeaderFieldInt
* getHeaderFieldDate
* getExpiration
* getDate
* getLastModified
* getHeaderField
* getHeaderFieldKey 

"

我希望您尝试过的其他手机不如 N97 强大,并且需要刷新 OutputStream,因此在按照规范连接到服务器之前。

于 2009-12-09T11:13:25.817 回答