// 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.net.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;

import com.sun.image.codec.jpeg.*;

public class RawPost {
   public static void main(String[] args) {
      try {
         URL url = new URL("http://localhost/rawpost.php");
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con.setRequestMethod("POST");
         con.setRequestProperty("Content-Type", "image/jpeg");
         con.setDoOutput(true);
         BufferedImage bi = new BufferedImage(100, 100,
               BufferedImage.TYPE_INT_RGB);
         Graphics g = bi.getGraphics();
         g.setColor(Color.WHITE);
         g.fillRect(0, 0, 100, 100);
         g.setColor(Color.BLUE);
         g.fillOval(20, 20, 60, 60);
         JPEGImageEncoder ecd = JPEGCodec.createJPEGEncoder(con
               .getOutputStream());
         ecd.encode(bi);
         con.connect();
         if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = con.getInputStream();
            byte[] b = new byte[1000];
            int n;
            while ((n = is.read(b)) >= 0) {
               System.out.println(new String(b, 0, n));
            }
            is.close();
         } else {
            System.out.println(con.getResponseCode() + " "
                  + con.getResponseMessage());
         }
         con.disconnect();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (ImageFormatException e) {
         e.printStackTrace();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
