Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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