package liu.net.udpupload; import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class UploadServer { public static void main(String[] args) throws IOException { //上传文本到服务端,服务端接收数据并存储到文件中,服务端接收完毕后,服务端回馈“接收成功” System.out.println("服务端启动。。。"); //1.服务端对象 ServerSocket ss= new ServerSocket(8890); //2.获取客户端 Socket s = ss.accept(); //3.获取读取流 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); //4.确定目的文件 PrintWriter pw = new PrintWriter(new FileWriter("server.txt"),true); //5.频繁读写 String line = null; while((line=bufIn.readLine())!=null){ pw.println(line); } //6.给客户端返回信息 PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.println("上传成功"); //7.关闭资源 pw.close(); s.close(); ss.close(); System.out.println("服务端成功关闭"); } }
原文地址:http://blog.51cto.com/11359581/2047979