Advertisement
Guest User

kkudi

a guest
Mar 15th, 2011
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package uk.ac.ic.doc.markitanis.learning;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10.  
  11. import org.apache.commons.codec.binary.Base64;
  12.  
  13. import android.util.Log;
  14.  
  15. public class HttpFileUploader {
  16.  
  17.     private URL connectURL;
  18.    
  19.     private String responseString;
  20.    
  21.     private String fileName;
  22.  
  23.     private FileInputStream fileInputStream = null;
  24.    
  25.     private final String lineEnd = "\r\n";
  26.  
  27.     private final String twoHyphens = "--";
  28.  
  29.     private final String boundary = "*****";
  30.  
  31.     private String parameters;
  32.    
  33.     private final String TAG = Constants.HTTP_FILE_UPLOADER;
  34.  
  35.     public HttpFileUploader(String urlString, String fileName, String parameters) throws MalformedURLException{
  36.  
  37.             connectURL = new URL(urlString);
  38.  
  39.             this.fileName = fileName;
  40.            
  41.             this.parameters = parameters;
  42.  
  43.     }
  44.  
  45.  
  46.     public boolean doStart(FileInputStream stream){
  47.        
  48.         fileInputStream = stream;
  49.  
  50.         return upload();
  51.     }
  52.  
  53.    
  54.     private boolean upload(){
  55.  
  56.         boolean success = false;
  57.        
  58.         try
  59.         {
  60.             //------------------ CLIENT REQUEST
  61.             Log.i(TAG, "About to upload database...");
  62.            
  63.             // Open a HTTP connection to the URL
  64.             HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
  65.            
  66.             // Allow Inputs
  67.             conn.setDoInput(true);
  68.  
  69.             // Allow Outputs
  70.             conn.setDoOutput(true);
  71.  
  72.             // Don't use a cached copy.
  73.             conn.setUseCaches(false);
  74.  
  75.             // Use a post method.
  76.             conn.setRequestMethod("POST");
  77.  
  78.             conn.setRequestProperty("Connection", "Keep-Alive");
  79.  
  80.             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  81.            
  82.               // stuff the Authorization request header
  83.             byte[] encodedPassword = ( "************" + ":" + "***********" ).getBytes();
  84.            
  85.             String encoded = new String(Base64.encodeBase64(encodedPassword));
  86.  
  87.             conn.setRequestProperty( "Authorization", "Basic " + encoded);
  88.  
  89.             DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
  90.  
  91.             dos.writeBytes(twoHyphens + boundary + lineEnd);
  92.  
  93.             dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + this.fileName+"\"" + lineEnd);
  94.            
  95.             //TODO:
  96.             //dos.writeBytes(this.parameters);
  97.            
  98.             dos.writeBytes(lineEnd);   
  99.            
  100.             Log.i(TAG ,"Headers have been written");
  101.  
  102.             // create a buffer of maximum size
  103.             int bytesAvailable = fileInputStream.available();
  104.  
  105.             int maxBufferSize = 1024;
  106.            
  107.             int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  108.            
  109.             byte[] buffer = new byte[bufferSize];
  110.  
  111.             // read file and write it into form...
  112.             int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  113.  
  114.             while (bytesRead > 0)
  115.             {
  116.                 dos.write(buffer, 0, bufferSize);
  117.  
  118.                 bytesAvailable = fileInputStream.available();
  119.                
  120.                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
  121.                
  122.                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  123.             }
  124.  
  125.             // send multipart form data necesssary after file data...
  126.             dos.writeBytes(lineEnd);
  127.  
  128.             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  129.  
  130.             // close streams
  131.            
  132.             Log.i(TAG ,"Database file with name " + this.fileName + " has been successfully written");
  133.            
  134.             fileInputStream.close();
  135.            
  136.             dos.flush();
  137.  
  138.             InputStream is = conn.getInputStream();
  139.            
  140.             // retrieve the response from server
  141.            
  142.             int ch;
  143.  
  144.             StringBuffer b = new StringBuffer();
  145.            
  146.             while( ( ch = is.read() ) != -1 ){
  147.                 b.append( (char)ch );
  148.             }
  149.            
  150.             this.responseString = b.toString();
  151.        
  152.             if (responseString.equals("success")) {
  153.                
  154.                 success = true;
  155.                
  156.             }else if (responseString.equals("error")) {
  157.                 success = false;
  158.             }
  159.            
  160.             Log.i(TAG, "Response String from server:"+responseString);
  161.            
  162.             dos.close();
  163.  
  164.         }
  165.         catch (MalformedURLException ex)
  166.         {
  167.             Log.e(TAG , "error: " + ex.getMessage());
  168.            
  169.         }
  170.  
  171.         catch (IOException ioe)
  172.         {
  173.             Log.e(TAG , "error: " + ioe.getMessage());
  174.            
  175.         }
  176.        
  177.         return success;
  178.     }
  179.  
  180.    
  181.     public String getResponse() {
  182.        
  183.         return this.responseString;
  184.    
  185.     }
  186.  
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement