Solved

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

Posted on 2006-12-01
2
601 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

20 Experts available now in Live!

Get 1:1 Help Now