本帖最后由 陈延真 于 2013-6-9 23:00 编辑
/*
* 读取客户端的数据,并写入服务器的一个地方。最后给客户端一个反馈,上次文件成功
*/
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 {
// 创建服务器Socket对象
ServerSocket ss = new ServerSocket(12321);
Socket s = ss.accept();
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 获取输出流
PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);
String line = null;
while ((line = br.readLine()) != null) {
pw.println(line);
}
// 给出反馈
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("上传文件成功");
// 释放资源
pw.close();
s.close();
ss.close();
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args) throws IOException {
// 创建客户端Socket对象
Socket s = new Socket("192.168.3.100", 12321);
// 封装文件的读取
BufferedReader br = new BufferedReader(new FileReader(
"a.java"));
// 获取输出流
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = br.readLine()) != null) {
pw.println(line);
}
// 接收反馈
BufferedReader brServer = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String server = brServer.readLine();
System.out.println (server);
// 释放资源
br.close();
s.close();
}
}
|