// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.net.*;

public class Upload {
    private static final String UPLOAD_URL = "http://localhost/rawupload.php";
    private static void upload(String dirname, String filename) {
        try {
            URL url = new URL(UPLOAD_URL + "?filename=" + filename);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            InputStream is = new FileInputStream(dirname + File.separator + filename);
            OutputStream os = con.getOutputStream();
            byte[] b = new byte[100000];
            int n;
            while((n = is.read(b)) >= 0) {
                os.write(b, 0, n);  
            }
            con.connect();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("OK");
            } else {
                System.out.println("Error");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        upload("C:", "X.java");
    }
}
