HTTPURLConnection.getInputStream()需要很长时间?
问题描述:
我使用HttpURLConnection的这需要大约3秒,以用于所有的头一个5MB的文件上传的图像文件,但我打开与.getInputStream()的InputStream的那一刻,该方法需要大约8+秒返回流用。这是一个问题,因为如果我有多个上传图片,上传进度条会给用户带来不好的用户体验,他们在每次上传之间会有相当大的停顿时间,因此进度条仅停留在上传之间几秒钟。我做了一些Google搜索,但没有人似乎有问题呢?HTTPURLConnection.getInputStream()需要很长时间?
通常我会假定服务器是缓慢的,但看到上载只需要几秒钟,下载单词“成功”或“失败”真的不应该是太大的问题的!
继承人一些代码!我最初是否设置了错误? 注:这是又一个的AsyncTask内
ByteArrayInputStream fileInputStream = null;
try {
fileInputStream = new ByteArrayInputStream(dObject.Data);
} catch (Exception e) {
e.printStackTrace();
}
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="3rd";
try
{
//------------------ CLIENT RE QUEST
Log.e(Tag,"Inside second Method");
// Open a HTTP connection to the URL
URL url = new URL(_urlString);
//connectURL is a URL object
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
//dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + _fileLocation +"\"" + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + _fileLocation +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
//int value = (int)(((float)((float)totalRead/(float) fileSize)) * 100);
totalRead += bytesRead;
//Publish the progress out to be displayed
publishProgress(totalRead);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e(Tag,"File is written");
fileInputStream.close();
dos.flush();
Log.e("TIME", "PRE GETINPUTSTREAM");
InputStream is = conn.getInputStream();
Log.e("TIME", "POST GETINPUTSTREAM");
// retrieve the response from server
int ch;
//Build the respose and log
StringBuilder b =new StringBuilder();
while((ch = is.read()) != -1){
b.append((char)ch);
}
String s=b.toString();
Log.i("Response",s);
dos.close();
return;
}
catch (MalformedURLException ex)
{
ErrorHandler.get().e("3");
}
catch (IOException ioe)
{
ErrorHandler.get().e("2");
}
答
通常我会假定服务器是缓慢的,但看到只上传需要几秒钟,下载单词“成功”或“失败”应该不是真正的问题!
我怀疑它确实是服务器速度慢或超载。
服务器可能会排队HTTP请求,并且只能并行处理一个小数。
或者,它可能在包含该文件的响应写入响应之前进行一些数据库活动的瓶颈。
或者它可以是产生上飞的文件到从缓冲器到HTTP响应的内存中缓冲液(慢),然后流(快速)。
或者其他的解释是这样的...
(理论上也有可能是有一些有趣的事情,减慢了请求到服务器的传递。我认为这是尽管不太可能)
您是否尝试过使用网络浏览器下载相同的文件?你在那里得到同样的行为吗?
答
在我来说,我发现的getInputStream看起来慢,因为这种方法初始化SSL握手(在HTTPS URL调用)。第一个电话后,其他电话是OK
事实上它的服务器正在缓慢其实..我已经受够了说“上传”,而上传,然后显示具有不确定进度栏的‘敲定’在等待掰过来服务器响应!我认为我的公司应该升级主机! – Micky