Can I determine the size of the file to upload?

Currently, I have the following code that reads a file located on the Internet 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 if it is possible (using the setup above or otherwise) to determine the total number of bytes that should be read before starting with the download (to post the percentage progress for the user during the download process)

+5
source share
5 answers

use method getContentLength 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);
+6
source

try this using URLConnnection

URLConnection connection = new URL(sourceFileWebAddress).openStream();
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);
+3
source

, , , , HTTP HEAD :

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;
      }
  }
}

.

+3

, . . , asyc. , fielszie, progressUpdate, progressbar, progressUpdate(), .

+2

The getContentLengthof method URLConnectionshould provide you with the size of the uploaded file. From this, you can draw your ProgressBar as you wish by updating it (in onProgressUpdate, assuming you are doing this inside AsyncTask.) When new data is processed by your file stream.

If the server does not give you the value in getContentLength (-1 in most cases, but at least checks to be less than or equal to zero), just make your ProgressBar undefined.

+1
source

All Articles