-1
String thisurl ="http://songolum.com/file/ppdVkTxqhwcJu-CAzCgtNeICMi8mHZnKQBgnksb5o2Q/Ed%2BSheeran%2B-%2BPerfect.mp3?r=idz&dl=311&ref=ed-sheran-perfect";
url = null;
try {
     url = new URL(thisurl);
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
              //  urlConnection.setRequestProperty("Transfer-Encoding", "chunked");
                urlConnection.setRequestProperty("Accept-Encoding", "identity");
                urlConnection.setDoOutput(true);
                urlConnection.setChunkedStreamingMode(0);

int l=0;
      InputStream in = new BufferedInputStream(urlConnection.getInputStream());
      while(in.read()!=-1)
            {
            l=l+in.read();
            }
      System.out.println("Content-length" +l);

**I checked with other software and I found it's gzip compressed file and its with 10mb and I'm getting almost 1mb **

user2037091
  • 57
  • 1
  • 2
  • 8

2 Answers2

1

To answer your question directly, you were going wrong because you were calling read() twice, and also because you were adding together the values of each byte read, instead of counting them. InputStream.read() reads one byte and returns its value, or -1 on EOF. You need to read a number of bytes into a buffer and count how many bytes each read() call returned:

  InputStream in = urlConnection.getInputStream();
  byte[] buffer = new byte[4096];
  int countBytesRead;
  while((countBytesRead = in.read(buffer)) != -1) {
      l += countBytesRead;
  }
  System.out.println("Content-length: " + l);

However, I suspect that this is not really what you need to do. The above code will simply return the size of all content in the response, including the HTTP headers and the content. Perhaps what you are looking for is the length of the document (or the file to be downloaded). You can use the Content-length HTTP header for that purpose (see other SO questions for how to get HTTP headers).

Also, note that the content may or may not be gzip-compressed. It depends on what the HTTP request says it accepts.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
  • What you suggested is correct, but the code here doesnt reflect it correctly. You are still adding random byte values together in between reading buffers of them – IAmGroot Nov 22 '17 at 13:26
  • @Doomsknight yes, thanks for pointing that out! I'll correct it. – DodgyCodeException Nov 22 '17 at 13:27
  • Content-length is not working in this case , i I’m looking a alternative method to find a file size without content-length code – user2037091 Nov 22 '17 at 15:02
0

Please try this one hope so it will be helpful for you.

Using a HEAD request, i got my webserver to reply with the correct content-length field which otherwise was wrong. I don't know if this works in general but in my case it does:

private int tryGetFileSize(URL url) {
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.getInputStream();
        return conn.getContentLength();
    } catch (IOException e) {
        return -1;
    } finally {
        conn.disconnect();
    }
}
Dilip
  • 2,622
  • 1
  • 20
  • 27
  • i did't get any result from this , there is 10mb file inside this url http://songolum.com/file/ppdVkTxqhwcJu-CAzCgtNeICMi8mHZnKQBgnksb5o2Q/Ed%2BSheeran%2B-%2BPerfect.mp3?r=idz&dl=311&ref=ed-sheran-perfect" but i only get 1942011bytes – user2037091 Nov 22 '17 at 12:09
  • Ok i will try here and inform you. – Dilip Nov 22 '17 at 12:11
  • Thanks for your time and its a gzip compressed url i tried urlConnection.setRequestProperty("Accept-Encoding", "identity"); and transfer-encoding →chunked identity gzip files but nothing happening – user2037091 Nov 22 '17 at 12:13
  • here i am not able to get response due to restriction of URL so i got this error at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) but i will try from my home. – Dilip Nov 22 '17 at 12:30