客户端:
package WebProgramingDemo; import java.io.BufferedReader; public class UploadTextClient { /**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException { Socket s = new Socket("192.168.2.103", 10005);
BufferedReader bufr = new BufferedReader(new FileReader("client.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = bufr.readLine()) != null) {
out.println(line); }
s.shutdownOutput();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufr.close();
s.close(); } }
服务端:
package WebProgramingDemo; import java.io.BufferedReader;
import java.io.BufferedWriter;
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 UploadTextServer { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()
+ ".....connected"); BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
BufferedWriter bufw = new BufferedWriter(new FileWriter("server.txt"));
String line = null;
while ((line = bufIn.readLine()) != null) {
bufw.write(line);
bufw.newLine();
bufw.flush();
}
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("上传成功");
bufw.close();
s.close();
ss.close();
} }