Code example for DataOutputStream

Methods: close, flush, write, writeBytes

0
    	connection.setRequestMethod("POST");
 
    	connection.setRequestProperty("Connection", "Keep-Alive");
    	connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
     
    	outputStream = new DataOutputStream( connection.getOutputStream() );
     
    	outputStream.writeBytes(twoHyphens + boundary + lineEnd);
     
    	 
    	outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName +"\"" + lineEnd);
    	outputStream.writeBytes(lineEnd);
     
    	 
    	/* 
    	bytesAvailable = fileInputStream.available(); 
    	bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    	buffer = new byte[bufferSize]; 
    	*/ 
    	 
    	buffer = decodeFile(data); 
    	bufferSize = Math.min(buffer.length, maxBufferSize);
    	bytesAvailable  = buffer.length; 
    	 
    	outputStream.write(buffer, 0, buffer.length);
    	 
    /* 
    	// Read file 
    	bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    	while (bytesRead > 0)  	{ 
	    	outputStream.write(buffer, 0, bufferSize); 
	    	bytesAvailable = fileInputStream.available(); 
	    	bufferSize = Math.min(bytesAvailable, maxBufferSize); 
	    	bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    	} 
    */ 
    	outputStream.writeBytes(lineEnd);
    	outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
 
    	// Responses from the server (code and message) 
    	int serverResponseCode = connection.getResponseCode();
    	String serverResponseMessage = connection.getResponseMessage();
    	//Toast.makeText(getBaseContext(), serverResponseMessage, Toast.LENGTH_LONG).show(); 
    	//fileInputStream.close(); 
    	outputStream.flush();
    	outputStream.close();
    	 
    	Log.i("vg", "upload done: "+fileName+"; response code: "+serverResponseCode+"; response message: "+serverResponseMessage);
    	} 
    	catch (Exception ex){
    		Log.e("vg", "error in upload "+fileName+"; msg: "+ex.getMessage(),ex);