黑马程序员_TCP-客户端并发上传图片

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
import java.net.*;
import java.io.*;
class KingThread implements Runnable
{
private Socket s;


KingThread(Socket s)
{
this.s=s;
}
public void run()
{
int count=1;
try
{
BufferedInputStream bis= new BufferedInputStream(s.getInputStream());
String ip=s.getInetAddress().getHostAddress();
File f=new File(ip+"("+count+")"+".jpg");
while(f.exists()){
f=new File(ip+"("+(++count)+")"+".jpg");}
PrintStream ps=new PrintStream(f);
byte[] b=new byte[1024*1024];
int len;
while((len=bis.read(b))!=-1)
{
ps.write(b,0,len);
ps.flush();
}
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
pw.println("上传成功");
}
catch (Exception e)
{
throw new RuntimeException("接受失败");
}
}






}
class PeopleThread implements Runnable
{
private Socket s;
PeopleThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("1.jpg"));
BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
byte[] b=new byte[1024*1024];
int len;
while((len=bis.read(b))!=-1)
{
bos.write(b,0,len);
bos.flush();
}
s.shutdownOutput();
BufferedInputStream bisnew = new BufferedInputStream(s.getInputStream());
byte[] buf=new byte[1024*1024];
int num;
String news=new String(buf,0,num=bisnew.read(buf));
System.out.println(news);
}
catch (Exception e)
{
throw new RuntimeException("上传失败");
}

}
}
class King 
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(1999);
while(true)
{
Socket s=ss.accept();
new Thread(new KingThread(s)).start();
}
}
}
class People
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.190", 1999);
new Thread(new PeopleThread(s)).start();
}

}
---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值