import java.io.*;
import java.net.*;

public class httpput 
{
    static public void main(String[] args)
    {
		try
		{
			if(args.length != 2)
			{
				System.out.println("usage: java http-put file target_url");
				System.exit(0);
			}
			
	        URL url = new URL(args[1]);
        
   			HttpURLConnection conn = (HttpURLConnection)url.openConnection();            
			conn.setRequestProperty("Content-Type", "application/octet-stream");
		
			// conn.setRequestProperty("Authorization", "Basic anJzY2xpZW50Ompyc2NsaWVudA==");
	
			conn.setRequestMethod("PUT");

        	conn.setDoOutput(true);
			OutputStream os = conn.getOutputStream();

			FileInputStream fis = new FileInputStream(args[0]);

			byte[] buf = new byte[8192];
			int read = -1;
			int total = 0;

			long start = System.currentTimeMillis();

			while((read = fis.read(buf)) != -1)
        	{
            	os.write(buf, 0, read);
				total += read;
        	}

			fis.close();
			os.close();
        
        	// Evaluate the response from the server
        	int code = conn.getResponseCode();
        	String response = conn.getResponseMessage();
			
			long end = System.currentTimeMillis();
	
			System.out.println("HTTP response code: " + code + " " + response);

			System.out.println("Sent: " + total + " in " + (end-start) + " millis");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
    }
}
