Solved

Get Content-length from downloading a file through URLConnection in Java

Posted on 2006-12-01
2
605 Views
Last Modified: 2008-01-09
Hi,

I want to know how I can get the file size from a download size using URLConnection. What I do is:

public int getGZIPFileSize(String url){
        int fileSize = 0;
        try{

                URL url = new URL(url);
                URLConnection conn = url.openConnection();
               
                conn.setUseCaches(false);
                conn.setRequestProperty("Cookie",cookie);
                conn.setRequestProperty("Accept-Encoding","gzip");
               
                fileSize = conn.getContentLength();

           
            System.out.println("\n\n\n The file size = " +fileSize+ " \n\n\n");
           
        }catch(Exception e){
            e.printStackTrace();
        }
        return fileSize;
    }

It always returns -1. The file is successfully downloaded using IE from url.  Does anyone have any idea?

Thanks,
0
Comment
Question by:a122178
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18057929
content length is not required to be set. -1 means read until eof.
0
 
LVL 3

Author Comment

by:a122178
ID: 18058120
Yes you are right. The content length may not be set on the server side.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now