黑马程序员技术交流社区
标题:
网络编程TCP
[打印本页]
作者:
b_boywindy
时间:
2012-3-1 09:31
标题:
网络编程TCP
求写一个java TCP 协议上传下载的程序,没有javaweb代码??
作者:
李明伟
时间:
2012-3-11 12:15
图片的上传下载
public class PicServerTest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
ServerSocket ss=new ServerSocket(10002);
while(true)
{
Socket s=ss.accept();
new Thread(new PicServer(s)).start();
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class PicServer implements Runnable
{
Socket s;
public PicServer(Socket s)
{
this.s=s;
}
public void run()
{
try
{
InputStream is=s.getInputStream();
FileOutputStream fos=new FileOutputStream("d:\\as.jpg");
byte[] buf=new byte[1024];
int len;
while((len=is.read(buf))!=-1)
{
fos.write(buf);
}
//PrintWriter pw=new PrintWriter(s.getOutputStream());
OutputStream os=s.getOutputStream();
os.write("ok".getBytes());
s.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class PicClient
{
public PicClient()
{
}
public static void main(String[] args)
{
try
{
Socket s=new Socket("localhost",10001);
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
BufferedInputStream bufs=new BufferedInputStream(new FileInputStream("d:\\1.jpg"));
byte[] buf=new byte[1024];
while((bufs.read(buf))!=-1)
{
os.write(buf);
os.flush();
}
s.shutdownOutput();
byte[] by=new byte[1024];
is.read(by);
System.out.println(buf);
bufs.close();
s.close();
} catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2