Return to Snippet

Revision: 51455
at September 25, 2011 01:17 by sperales


Updated Code
int maxBufferSize = 1*256*1024;
int headerSize=89;
String urlString = "http://xxxxxxxx.com/upload.php";
int fileSize=0;
int infoSize=0;

protected Integer doInBackground(String... file) {
    		Integer ret=0;
    		
    		HttpURLConnection conn = null;
    		DataOutputStream dos = null;
    		DataInputStream inStream = null;

    		String exsistingFileName = file[0];

    		String lineEnd = "
";
    		String twoHyphens = "--";
    		String boundary =  "*****";

    		int bytesRead, bytesAvailable, bufferSize;
    		byte[] buffer;    		
    		
    		try
    		{
    			Log.i("MyAPP","Start uploading file: " + file[0]);

    			FileInputStream fileInputStream = new FileInputStream(
    				new File(exsistingFileName));

    			// open a URL connection to the Servlet 
    			URL url = new URL(urlString);

    			//Open a HTTP connection to the URL
    			conn = (HttpURLConnection) url.openConnection();

    			// Allow Inputs
    			conn.setDoInput(true);

    			// Allow Outputs
    			conn.setDoOutput(true);

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

    			// Use a post method.
    			fileSize=fileInputStream.available();
    			infoSize=fileSize + headerSize + file[0].length();
    			conn.setFixedLengthStreamingMode(infoSize);
    			conn.setRequestMethod("POST");

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

    			dos = new DataOutputStream( conn.getOutputStream() );

    			dos.writeBytes(twoHyphens + boundary + lineEnd);
    			dos.writeBytes("Content-Disposition: form-data; 
				name='uploadedfile';filename='" + file[0] + "'" + lineEnd);
    			dos.writeBytes(lineEnd);

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

    			// read file and write it into form...
    			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    			int bytesSent=0;
    			while (bytesRead > 0)
    			{
    				if (bytesSent > 0){
    					int pg=(bytesSent*100)/infoSize;
    					publishProgress(pg);
    				}
    				bytesSent+=bufferSize;
    				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();
    			dos.close();

    		}
    		catch (MalformedURLException ex)
    		{
    			Log.e("MyAPP", "error: " + ex.getMessage(), ex);
    			ret=1;
    		}
    		catch (IOException ioe)
    		{
    			Log.e("MyAPP", "error: " + ioe.getMessage(), ioe);
    			ret=1;
    		}
    		
    		try {
    			inStream = new DataInputStream ( conn.getInputStream() );
    			String str;
    			   
    			while (( str = inStream.readLine()) != null)
    			{
    				Log.i("MyAPP","Server Response"+str);
    			}
    			inStream.close();
    		}
    		catch (IOException ioex){
    			Log.e("MyAPP", "error: " + ioex.getMessage(), ioex);
    			ret=1;
    		}
    		
    		return ret;
    	}

Revision: 51454
at September 25, 2011 01:01 by sperales


Updated Code
int maxBufferSize = 1*256*1024;
int headerSize=89;
String urlString = "http://xxxxxxxx.com/upload.php";
int fileSize=0;
int infoSize=0;

protected Integer doInBackground(String... file) {
    		Integer ret=0;
    		
    		HttpURLConnection conn = null;
    		DataOutputStream dos = null;
    		DataInputStream inStream = null;

    		String exsistingFileName = file[0];

    		String lineEnd = "
";
    		String twoHyphens = "--";
    		String boundary =  "*****";

    		int bytesRead, bytesAvailable, bufferSize;
    		byte[] buffer;    		
    		
    		try
    		{
    			Log.i("MyAPP","Start uploading file: " + file[0]);

    			FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));

    			// open a URL connection to the Servlet 
    			URL url = new URL(urlString);

    			//Open a HTTP connection to the URL
    			conn = (HttpURLConnection) url.openConnection();

    			// Allow Inputs
    			conn.setDoInput(true);

    			// Allow Outputs
    			conn.setDoOutput(true);

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

    			// Use a post method.
    			fileSize=fileInputStream.available();
    			infoSize=fileSize + headerSize + file[0].length();
    			conn.setFixedLengthStreamingMode(infoSize);
    			conn.setRequestMethod("POST");

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

    			dos = new DataOutputStream( conn.getOutputStream() );

    			dos.writeBytes(twoHyphens + boundary + lineEnd);
    			dos.writeBytes("Content-Disposition: form-data; name='uploadedfile';filename='" + file[0] + "'" + lineEnd);
    			dos.writeBytes(lineEnd);

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

    			// read file and write it into form...
    			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    			int bytesSent=0;
    			while (bytesRead > 0)
    			{
    				if (bytesSent > 0){
    					int pg=(bytesSent*100)/infoSize;
    					publishProgress(pg);
    				}
    				bytesSent+=bufferSize;
    				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();
    			dos.close();

    		}
    		catch (MalformedURLException ex)
    		{
    			Log.e("MyAPP", "error: " + ex.getMessage(), ex);
    			ret=1;
    		}
    		catch (IOException ioe)
    		{
    			Log.e("MyAPP", "error: " + ioe.getMessage(), ioe);
    			ret=1;
    		}
    		
    		try {
    			inStream = new DataInputStream ( conn.getInputStream() );
    			String str;
    			   
    			while (( str = inStream.readLine()) != null)
    			{
    				Log.i("MyAPP","Server Response"+str);
    			}
    			inStream.close();
    		}
    		catch (IOException ioex){
    			Log.e("MyAPP", "error: " + ioex.getMessage(), ioex);
    			ret=1;
    		}
    		
    		return ret;
    	}

Revision: 51453
at September 24, 2011 23:53 by sperales


Updated Code
protected Integer doInBackground(String... file) {
    		Integer ret=0;
    		
    		HttpURLConnection conn = null;
    		DataOutputStream dos = null;
    		DataInputStream inStream = null;

    		String exsistingFileName = Environment.getExternalStorageDirectory().getAbsolutePath()+file[0];

    		String lineEnd = "
";
    		String twoHyphens = "--";
    		String boundary =  "*****";

    		int bytesRead, bytesAvailable, bufferSize;
    		byte[] buffer;    		
    		
    		try
    		{
    			Log.i("MyAPP","Start uploading file: " + file[0]);

    			FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

    			// open a URL connection to the Servlet 
    			URL url = new URL(urlString);

    			//Open a HTTP connection to the URL
    			conn = (HttpURLConnection) url.openConnection();

    			// Allow Inputs
    			conn.setDoInput(true);

    			// Allow Outputs
    			conn.setDoOutput(true);

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

    			// Use a post method.
    			fileSize=fileInputStream.available();
    			infoSize=fileSize + headerSize + file[0].length();
    			conn.setFixedLengthStreamingMode(infoSize);
    			conn.setRequestMethod("POST");

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

    			dos = new DataOutputStream( conn.getOutputStream() );

    			dos.writeBytes(twoHyphens + boundary + lineEnd);
    			dos.writeBytes("Content-Disposition: form-data; name='uploadedfile';filename='" + file[0] + "'" + lineEnd);
    			dos.writeBytes(lineEnd);

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

    			// read file and write it into form...
    			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    			int bytesSent=0;
    			while (bytesRead > 0)
    			{
    				if (bytesSent > 0){
    					int pg=(bytesSent*100)/infoSize;
    					publishProgress(pg);
    				}
    				bytesSent+=bufferSize;
    				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();
    			dos.close();

    		}
    		catch (MalformedURLException ex)
    		{
    			Log.e("MyAPP", "error: " + ex.getMessage(), ex);
    			ret=1;
    		}
    		catch (IOException ioe)
    		{
    			Log.e("MyAPP", "error: " + ioe.getMessage(), ioe);
    			ret=1;
    		}
    		
    		try {
    			inStream = new DataInputStream ( conn.getInputStream() );
    			String str;
    			   
    			while (( str = inStream.readLine()) != null)
    			{
    				Log.i("MyAPP","Server Response"+str);
    			}
    			inStream.close();
    		}
    		catch (IOException ioex){
    			Log.e("MyAPP", "error: " + ioex.getMessage(), ioex);
    			ret=1;
    		}
    		
    		return ret;
    	}

Revision: 51452
at September 24, 2011 23:46 by sperales


Initial Code
protected Integer doInBackground(String... file) {
    		Integer ret=0;
    		
    		HttpURLConnection conn = null;
    		DataOutputStream dos = null;
    		DataInputStream inStream = null;

    		String exsistingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Signature/" + file[0];

    		String lineEnd = "
";
    		String twoHyphens = "--";
    		String boundary =  "*****";

    		int bytesRead, bytesAvailable, bufferSize;
    		byte[] buffer;    		
    		
    		try
    		{
    			Log.i("DigitalPhoto","Start uploading file: " + file[0]);

    			FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

    			// open a URL connection to the Servlet 
    			URL url = new URL(urlString);

    			//Open a HTTP connection to the URL
    			conn = (HttpURLConnection) url.openConnection();

    			// Allow Inputs
    			conn.setDoInput(true);

    			// Allow Outputs
    			conn.setDoOutput(true);

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

    			// Use a post method.
    			fileSize=fileInputStream.available();
    			infoSize=fileSize + headerSize + file[0].length();
    			conn.setFixedLengthStreamingMode(infoSize);
    			conn.setRequestMethod("POST");

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

    			dos = new DataOutputStream( conn.getOutputStream() );

    			dos.writeBytes(twoHyphens + boundary + lineEnd);
    			dos.writeBytes("Content-Disposition: form-data; name='uploadedfile';filename='" + file[0] + "'" + lineEnd);
    			dos.writeBytes(lineEnd);

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

    			// read file and write it into form...
    			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    			int bytesSent=0;
    			while (bytesRead > 0)
    			{
    				if (bytesSent > 0){
    					int pg=(bytesSent*100)/infoSize;
    					publishProgress(pg);
    				}
    				bytesSent+=bufferSize;
    				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();
    			dos.close();

    		}
    		catch (MalformedURLException ex)
    		{
    			Log.e("DigitalPhoto", "error: " + ex.getMessage(), ex);
    			ret=1;
    		}
    		catch (IOException ioe)
    		{
    			Log.e("DigitalPhoto", "error: " + ioe.getMessage(), ioe);
    			ret=1;
    		}
    		
    		try {
    			inStream = new DataInputStream ( conn.getInputStream() );
    			String str;
    			   
    			while (( str = inStream.readLine()) != null)
    			{
    				Log.i("DigitalPhoto","Server Response"+str);
    			}
    			inStream.close();
    		}
    		catch (IOException ioex){
    			Log.e("DigitalPhoto", "error: " + ioex.getMessage(), ioex);
    			ret=1;
    		}
    		
    		return ret;
    	}

Initial URL


Initial Description


Initial Title
Upload large images in background

Initial Tags
background, android

Initial Language
Java