Can anybody tell me how to get the size of a file before downloading it from server(may be http,ftp or anything) in android?.Does streaming works?. Please Help me..
Regards
Varnesh
Try this will work in case the http server is giving the file size
URL myUrl = new URL("http://jamants.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Or second Version try this.
URL myUrl = new URL("http://jamants.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");
It Works in my project.
If you are using the built-in Apahce HTTP library to make a request for a file, you can get the file size by requesting the headers only. Inside the headers will be a "content-length" attribute that will indicate the number of bytes of the requested file. Requesting the headers will not request the file itself.
Related
i don't know if the following is correct to the connection, I have a IOException.
sb=ftp://IDUSER:password#ftp.fercode.com/manolo;type=i
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
urlc.getOutputStream();// this line throws a IOException
You need to use FTP client like Apache to properly upload and downland files.
Take look here:
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html
I am getting a java.io.IOException: Value too large for defined data type when uploading a large file almost 2.1GB to a server. and My code is :
URL oUrl = new URL(servierUrl);
HttpURLConnection connection = (HttpURLConnection) oUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form- data");
connection.setRequestProperty("Content-Length", String.valueOf(nChunkSize));
connection.setConnectTimeout(1800000);
InputStream oFileInputStream = new FileInputStream(new File(sFilePath));
OutputStream outputStream = new DataOutputStream(connection.getOutputStream());
.
.
. // here I'm dividing the stream chunks, every chunk 5Mb and uploading the chuck to server
but in the chunk #410 (5Mb * 410) it cause an exception return the exception as I mentioned above.
before that every chunk I upload it success
.
.
So any help please.
My guess- something is using an int to hold the length of the file. This is overflowing at 2^31-1, or about 2 billion bytes. Looks like an issue in either the http library or the output stream. You're going to need to either break up that file or replace the breaking component by one that works. Which component is breaking can be found by looking higher up in the stack trace and seeing where the crash comes from.
I want to get the video size in Mega Bytes from a Url in an android app. I have a Player that plays a video inside an app. I want to display next to my player the current videos size. So an example would me 23mb. If the video is 23 mg than I would have text next to the video that says 23mb. I tried looking through all the android mp.get functions but could not find what Iam looking for. Please help. Maybe I missed a function in Android. Or mayber there is anouther way to accomplush this. Thanks.
The value of the HTTP content-length header will provide the size of the file being downloaded.
Take a look at URLConnection.getHeaderField(String key) or HttpMessage.getFirstHeader(String name) depending on your server access code.
Try this will work in case the http server is giving the file size
URL myUrl = new URL("http://your_url.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
file_size = file_size /1024;
Or second Version try this.
URL myUrl = new URL("http://your_url.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");
I'm downloading a file in my android app and I want to display a progressbar. I need to know the total file size by using this method : here.
The problem is that I always get -1 despite the fact that it's set on the server and I can see it from my computer.
This is the code :
URL url = new URL(fileUrl);
URLConnection ucon = url.openConnection();
ucon.connect();
int length = ucon.getContentLength();
What is the problem ?
This is not related to Android, most probably it was not sent from the server..please use any sniffing tool to make sure the content length header is there.
I'm having a strange problem here. Here's the code, I'm using to fetch a url content:
URL u = new URL(url);
InputStream is = new BufferedInputStream(u.openStream());
I've got two urls, I want to fetch with this code. Both contain xml data. To be specific, the first one is http://www.berlingske.dk/unwire/latest/news_article/2/10, the second one is http://www.bt.dk/mecommobile/latest/news_article/1368/10?output_type=xml. The first one gets fetched correctly, the second one does not. I added some logging, and found out, that for the second url some weird html page gets fetched, instead of the expected xml. How can that be even possible?
I think you're talking about URL redirects, which was a problem I was having. Try the following code:
URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();
InputStream is = new BufferedInputStream(conn.openStream());
The "magic" here happens in these 2 steps:
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
By default InstanceFollowRedirects are set to true, but you want to set it to false to capture the second url. To be able to get that second url from the "weird html page", you need to get the header field called "Location".
Unless i misunderstood your problem, I hope this helps!