我将诺基亚N97手机上传的文件上传到服务器,一切正常但文件上传后我想从服务器获得响应。问题是我只在半分钟或更长时间后得到回应。从我看到的httpConnection.getResponseCode()中的代码块等待响应。我在索尼爱立信上测试了它,我得到了非常快的反应,所以我认为是诺基亚N97问题。 (这不是服务器问题,因为它可以在其他手机上正常工作)
有谁知道为什么这件事只发生在诺基亚?
以下是代码段:
public uploadFile(){
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();
}
public 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
}
}
答案 0 :(得分:4)
您只认为您的文件已上传。
您对getResponseCode()的调用是第一个导致HttpConnection实际连接到服务器的。
在上传文件之前不会返回,大概需要一分多钟。
根据JSR-118中的HttpConnection规范,这是完全有效的行为:
” 当连接处于安装状态时,以下方法会导致转换为已连接状态。
* openInputStream
* openDataInputStream
* getLength
* getType
* getEncoding
* getHeaderField
* getResponseCode
* getResponseMessage
* getHeaderFieldInt
* getHeaderFieldDate
* getExpiration
* getDate
* getLastModified
* getHeaderField
* getHeaderFieldKey
“
我希望你尝试过的其他手机功能不如N97,需要刷新OutputStream,因此应该根据规范连接到服务器。