/////// FileServer.java /////// import java.io.*; import java.net.*; import java.util.StringTokenizer; public class FileSaver { public static void main (String[] args) throws IOException { String filename, rw, dir = "/tmp/"; if ( args.length != 2 ) return; filename = args[0]; rw = args[1]; if ( rw.equals("r") ) { readBack(dir + filename); return; } else { writeTo(dir + filename); return; } } static void readBack(String file) throws IOException { int c; FileInputStream infile = new FileInputStream(file); // Sending a stream of bytes System.out.println("Content-Type: application/octet-stream"); System.out.println(); // empty line while ( (c = infile.read() ) > -1 ) System.out.write(c); } static void writeTo(String file) throws IOException { int c; FileOutputStream ofile = new FileOutputStream(file); while ( (c = System.in.read() ) > -1 ) ofile.write(c); // confirmation System.out.println("Content-Type: text/html"); // 1st line System.out.println(); // empty line System.out.println("
Your" +file+ " has been saved
"); } }