I have a function I am using to upload files.  How can I get the
number of bytes of the file that have been uploaded (transferred)?
Can I setup a timer that does this?  If so, what variable to I read to
get the number of bytes uploaded?  Here is my code:

========================================================================

String existingFileName = fileToUpload;
File uploadFile = new File(existingFileName);
FileInputStream fileInputStream = new FileInputStream(uploadFile);

// Set some special vars
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

// URL to upload to
URL connectURL = new URL("http://url/to/upload/script.php?
filename="+fileToUpload);

// connectURL is a URL object
conn = (HttpURLConnection)connectURL.openConnection();

// allow inputs
conn.setDoInput(true);

// allow outputs
conn.setDoOutput(true);

// don't use a cached copy
conn.setUseCaches(false);

// use a post method
conn.setRequestMethod("POST");

// set post headers
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-
data;boundary="+boundary);

// open data output stream
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile
\";filename=\""+existingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);

// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
        // Upload file part(s)
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

// Send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Close streams
fileInputStream.close();
dos.flush();

InputStream is = conn.getInputStream();
int ch;

StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
        b.append( (char)ch );
}

String s=b.toString();
dos.close();

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to