Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
spring jars download 1 35
jsp login check 12 43
jmss example java 2 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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