我将文件从诺基亚 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
}
}