?
Solved

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

Posted on 2006-12-01
2
Medium Priority
?
616 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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