/** * By default this code raises a security exception as an * applet and writes a file successfully as an application * * With JDK 1.2 appletviewer, * if you configure your system to grant applets signed by "Duke" * and downloaded from the Java Software website to write a file * to your /tmp directory (or to the file named "C:\tmpfoo" on a * Windows system), then this applet can run. * * @version JDK 1.2 * @author Marianne Mueller */ import java.awt.*; import java.io.*; import java.lang.*; import java.applet.*; import java.net.*; import javax.net.ssl.*; public class writeFileSSL extends Applet { String myFile = "/tmp/foo"; File f = new File(myFile); DataOutputStream dos; public void init() { String osname = System.getProperty("os.name"); if (osname.indexOf("Windows") != -1) { myFile="C:" + File.separator + "tmpfoo"; } } public void paint(Graphics g) { try { String host = System.getProperty("writeFileSSL.hostname"); System.out.println("connecting to " + host); SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault(); SSLSocket s = (SSLSocket)sslFact.createSocket(host, 8181); OutputStream out = s.getOutputStream(); BufferedReader in = new BufferedReader ( new InputStreamReader(s.getInputStream())); String str = in.readLine(); in.close(); dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128)); dos.writeBytes("Cats can hypnotize you when you least expect it\n"); dos.writeBytes(str); dos.flush(); g.drawString("Successfully wrote to the file named " + myFile + " -- go take a look at it!", 10, 10); dos.close(); } catch (SecurityException e) { g.drawString("writeFileSSL: caught security exception", 10, 10); } catch (IOException ioe) { g.drawString("writeFileSSL: caught i/o exception", 10, 10); } } public static void main(String args[]) { Frame f = new Frame("writeFile"); writeFileSSL writefile = new writeFileSSL(); writefile.init(); writefile.start(); f.add("Center", writefile); f.setSize(300, 100); f.show(); } }