Java – HTTPURLConnection.getInputStream() takes a long time?

HTTPURLConnection.getInputStream() takes a long time?… here is a solution to the problem.

HTTPURLConnection.getInputStream() takes a long time?

I’m uploading an image file using HttpURLConnection that takes about 3 seconds for a 5MB file with all the headers, but when I open an InputStream with .getInputStream(), the method takes about 8 seconds over to return a stream with. This is a problem because if I have multiple images to upload, the upload progress bar seems to provide a poor user experience, with quite a long pause between each upload, so the progress bar will only stop for a few seconds between uploads. I did some googling but no one else seems to dispute this?

Usually I’d assume the server is slow, but given that the upload only takes a few seconds, the word “success” or “failure” for downloading shouldn’t be a big deal!

Here are some code! Am I initially set it wrong?
Note: This is also in 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");
    }

Solution

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!

I suspect the server is really slow or overloaded.

  • The server may be queuing HTTP requests and can only process a small number of requests in parallel at a time.

  • Or there may be bottlenecks in some database activities that execute before the response containing the file is written to the response.

  • Or it can dynamically generate files in memory buffers (slow) and then stream (fast) from the buffer to an HTTP response.

  • Or other explanations like this….

(It’s also theoretically possible that something interesting could happen that slows down sending requests to the server.) I don’t think that’s likely, though. )


Have you tried downloading the same file using a web browser? Did you have the same behavior there?

Related Problems and Solutions