首页 > 代码库 > 黑马程序员 【】java学习之路——TCP(三)客户端上传文件到服务器

黑马程序员 【】java学习之路——TCP(三)客户端上传文件到服务器

------- <a href=http://www.mamicode.com/"http://www.itheima.com" target="blank">android培训、java培训、期待与您交流! ----------

import java.io.*;

import java.net.*;
class  TextClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.104",10010);
BufferedReader bufr = 
new BufferedReader(new FileReader("TcpDemo.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
 
 
String line = null;
while ((line = bufr.readLine())!=null)
{
out.println(line);
}
s.shutdownOutput();//关闭客户端输出流,相当于给流中加入结束标记啊(方式二)
out.println("over");//自定义结束标记 (结束方式一)
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TextServer 
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10010);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".........connected");
DataInputStream dis = new DataInputStream(s.getInputStream());
long l = dis.readLong();


BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new FileWriter("Server.txt"),true);
String line = null;
while ((line = bufIn.readLine())!=null)
{
if ("over".equals(line))
{
break;
} //自定义结束标记 (结束方式一)
out.println(line);
}


PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功");
out.close();
s.close();
ss.close();

}
}