I currently have the following code which reads a file located on the web and writes it to a file on the phone:
InputStream inputStream = new URL(sourceFileWebAddress).openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
int count;
byte buffer[] = new byte[1024];
while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
fileOutputStream.write(buffer, 0, count);
Does anyone know whether it is possible (using the setup above or otherwise) to determine the total number of bytes which are to be read before commencing with the download (in order to publish percentage progress to the user as the download proceeds)?
you can get a header called Content-Length form the HTTP Response object that you get, this will give you the length of the file. you should note though, that some servers don't return that information, and the only way to know the actual size is to read everything from the response.
Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.
You can retrieve the length of the file with File#length(), which will return a value in bytes, so you need to divide this by 1024*1024 to get its value in mb.
use the getContentLength
method of URLConnection
URL url = new URL(sourceFileWebAddress);
URLConnection connection = url.openConnection();
connection.connect();
int fileLenth = connection.getContentLength();
InputStream inputStream = url.openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
TO determine the total number of bytes which are to be read before commencing with the download, one way is to only get the response headers by sending a HTTP HEAD request as follows:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class SimpleHTTPRequest {
public static void main(String[] args) {
HttpURLConnection connection = null;
URL serverAddress = null;
try {
String sourceFileWebAddress = "http://localhost:8080/mycontents";
serverAddress = new URL(sourceFileWebAddress);
//set up out communications stuff
connection = null;
//Set up the initial connection
connection = (HttpURLConnection)serverAddress.openConnection();
//HEAD request will make sure that the contents are not downloaded.
connection.setRequestMethod("HEAD");
connection.connect();
System.out.println("========"+connection.getContentLength());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
//close the connection, set all objects to null
connection.disconnect();
connection = null;
}
}
}
This will print the size of the content to be downloaded without actually downloading the contents.
try this out using URLConnnection
URLConnection connection = new URL(sourceFileWebAddress).openConnection();
InputStream stream = connection.getInputStream();
System.out.println("total size: "+connection.getContentLength();//size
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
int count;
byte buffer[] = new byte[1024];
while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
fileOutputStream.write(buffer, 0, count);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With