A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© b_boywindy 中级黑马   /  2012-3-1 09:31  /  1638 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求写一个java TCP 协议上传下载的程序,没有javaweb代码??

1 个回复

倒序浏览
图片的上传下载
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();
                }
               
               

        }


}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马