import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Date; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class WebConnection { /**Turn a bufferedImage into a jpeg. */ public void makeJPegFile(BufferedImage bi, String pathname, String filename, float quality) { try { // convert it to an image without an alpha channel for the Jpeg BufferedImage biOut = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics biOutG = biOut.getGraphics(); biOutG.drawImage(bi, 0, 0, null); File outputFile = new File(pathname + filename); FileOutputStream fos = new FileOutputStream(outputFile); BufferedOutputStream bufOut = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bufOut); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(biOut); param.setQuality(quality, false); encoder.setJPEGEncodeParam(param); encoder.encode(biOut); System.out.println(" jpeg " + pathname + filename); bufOut.close(); } catch (FileNotFoundException e) { System.out.println("file not found" + pathname + filename); } catch (IOException e) { System.out.println("io execption" + pathname + filename); } } /**Combines writing a a bufferedImage to a jpeg file with with uploading * @ _pathname this has to be a world writable directory on the itp server. If you use null it will default to like/home/dbo3/public_html/VideoSensing/test/ * @ _filename If you give null it will use the time to for the filename /*/ public void putBufferedImageToServer(BufferedImage _bi,String _pathname, String _filename){ if (_pathname == null) _pathname = "/home/dbo3/public_html/VideoSensing/test/"; if (_filename == null) _filename = "WC" + getTimeString() + ".jpg" ; makeJPegFile(_bi, "./", "tempoloni.jpg", 0.5f); postFileToITP("./", "tempoloni.jpg", _pathname, _filename); } /**This does not work with secure ftp but if you have a regular ftp server it will*/ public void ftp_utility(String localPath, String localFilename, String serverLogin, String serverPassword, String serverAddress, String serverPath, String serverFilename) { FtpClient fcMyFtp = new FtpClient(); try { int ch ; System.out.println("FTP localpath: " + localPath + " localfile: " + localFilename ); fcMyFtp.openServer(serverAddress); System.out.println(" after open " + serverAddress); fcMyFtp.login(serverLogin,serverPassword); System.out.println(" after login "+ serverLogin ); fcMyFtp.cd(serverPath); System.out.println(" change dir"); fcMyFtp.binary(); System.out.println(" binary "); FileInputStream fis = new FileInputStream( localPath + localFilename); TelnetOutputStream tos =fcMyFtp.put(serverFilename); byte buffer[] = new byte[1000]; int len; System.out.println(" after streams "); while( (len = fis.read(buffer)) != -1 ) { tos.write(buffer,0,len); } fis.close(); tos.close(); } catch( IOException e ) { e.printStackTrace(); } } /**For giving your picture and arbitraty title*/ public String getTimeString() { Date now = new Date(); String date = String.valueOf(now.getYear() + "-" + String.valueOf(now.getMonth() + 1) + "-" + String.valueOf(now.getDate())); long millis = now.getTime(); String time = String.valueOf(millis); return date + time; } /**This connects with a php script (see below) that takes in a picture and * puts it somewhere on itp.nyu.edu. You can supply your directory * for example /home/netid/public_html/bla but you have to change the permissions of your bla folder to be world writable. * This is dubious so I might shut it down after this week. */ public void postFileToITP(String _localPath, String _localFilename, String _pathname, String _filename) { DataOutputStream dataOut; String urlString = "http://itp.nyu.edu/~dbo3/VideoSensing/files/WebCam/UpLoad.php"; URLConnection urlConn; URL url; try { url = new URL(urlString); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestProperty("Content-Type", "multipart/form-data, boundary=AaB03x"); dataOut = new DataOutputStream(urlConn.getOutputStream()); dataOut.writeBytes("--AaB03x\r\n"); dataOut.writeBytes("Content-Disposition: form-data; name=\"image_file\"; filename=\"" + URLEncoder.encode(_pathname + _filename) + "\"\r\n"); dataOut.writeBytes("Content-Type: image/jpeg\r\n"); dataOut.writeBytes("\r\n"); FileInputStream fis = new FileInputStream(_localPath + _localFilename); System.out.println("output from " + _localPath + _localFilename + " to " + _pathname + _filename); byte buffer[] = new byte[1000]; int len; while ((len = fis.read(buffer)) != -1) { dataOut.write(buffer, 0, len); } fis.close(); dataOut.close(); dataOut.writeBytes("\r\n--AaB03x--\r\n"); dataOut.flush(); dataOut.close(); System.out.println("waiting for server"); InputStream in = urlConn.getInputStream(); int input = 0; String inputSt = ""; while (input != -1) { input = in.read(); inputSt = inputSt + ((char) input); } System.out.println("from Server" + inputSt); } catch (MalformedURLException me) { System.err.println("MalformedURLException: " + me); } catch (IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); } } /*This method is talkiing to the following php on the server * \n"; } } ?> */ }