我正在使用httpURLConnection上传图像文件,该图像文件包含所有标头的5MB文件大约需要3秒,但是当我使用.getinputStream()打开inputStream时,该方法大约需要8秒才能返回流.这是一个问题,因为如果我要上传多个图像,则上传进度栏似乎提供了错误的UX,每次上传之间它们都有相当大的暂停时间,因此进度栏在两次上传之间仅停留了几秒钟.我已经进行了一些谷歌搜索,但是似乎没有其他人对此有疑问吗?
通常,我会假设服务器运行缓慢,但是看到上传仅需几秒钟,下载“成功”或“失败”一词实际上并不是什么大问题!
这是一些代码!最初我设置错误吗?
注意:这也在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"); }
解决方法:
normally I would assume the server is slow, but seeing as uploading only takes a couple of seconds, downloading the word ‘success’ or ‘fail’ shouldn’t really be that much of an issue!
我怀疑这确实是服务器速度慢或过载.
>服务器可能正在排队http请求,并且一次只能并行处理少量请求.
>否则在将包含文件的响应写入响应之前执行的某些数据库活动中可能存在瓶颈.
>或者它可能正在动态地将文件生成到内存缓冲区中(缓慢),然后从缓冲区流式传输(快速)到http响应.
>或其他类似的解释…
(理论上也可能发生一些有趣的事情,这会减慢将请求发送到服务器的速度.不过,我认为这不太可能.)
您是否尝试过使用网络浏览器下载相同的文件?你在那得到同样的行为吗?
总结以上是内存溢出为你收集整理的java-HTTPURLConnection.getInputStream()需要很长时间吗?全部内容,希望文章能够帮你解决java-HTTPURLConnection.getInputStream()需要很长时间吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)