---------------------- <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();
}
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>、期待与您交流! ----------------------