/** * java file upload demo * @author:luowen * @time:2013-11-03 */ import java.net.*; import java.io.*; class UploadClient { public static void main(String[] args)throws Exception { //建立客户端socket对象 Socket s = new Socket("127.0.0.1",10000); BufferedReader bur = new BufferedReader(new FileReader("demo.txt")); PrintStream ps = new PrintStream(s.getOutputStream(),true); String line; while((line = bur.readLine()) != null) { ps.println(line); } s.shutdownOutput(); BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream())); while((line = bufr.readLine()) != null) { System.out.println(line); } bur.close(); bufr.close(); ps.close(); s.close(); } } class UploadServer { public static void main(String[] args)throws Exception { ServerSocket ss = new ServerSocket(10000); Socket s = ss.accept(); String ip = s.getInetAddress().getHostName(); System.out.println(ip+"........connected!"); BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream())); //BufferedWriter bufw = new BufferedWriter(new FileWriter("ServerDemo.txt")); PrintWriter bufw = new PrintWriter(new FileWriter("ServerDemo.txt")); String line; while((line = bufr.readLine()) != null) { //bufw.write(line,0,line.length()); //bufw.flush(); bufw.println(line); } OutputStream os = s.getOutputStream(); os.write("上传成功!".getBytes()); os.close(); bufr.close(); bufw.close(); ss.close(); } }