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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 江南 黑马帝   /  2012-6-10 02:50  /  1473 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

头晕晕的了,不知道客户端为什么得不到数据 猜测就是在流的写入写出被阻塞,出了问题了!太累了怎么都看不出来
import java.io.*;
import java.net.*;

class PicClien
{   
          public static void main(String []  args)throws Exception
          {
                  Socket s=new Socket("192.168.1.254",1007);
                  FileInputStream fis=new FileInputStream("c:\\1.bmp");
                  OutputStream  out=s.getOutputStream();
                  byte[] buf=new byte[1024];
                  int len=0;
                  while((len=fis.read(buf))!=-1)
                  {
                          out.write(buf,0,len);
                  }
                  InputStream in=s.getInputStream();
                  byte[] bufIn= new byte[1024];
                  int num=in.read(bufIn);
                  System.out.println(new String(bufIn,0,num));
                  fis.close();
                  s.close();
          }
}


class  PicServer
{
        public static void main(String[] args)throws Exception
        {
                ServerSocket ss= new ServerSocket(1007);
                Socket s=ss.accept();
                InputStream in=s.getInputStream();
                FileOutputStream fos= new FileOutputStream("server.bmp");
                byte[] buf= new byte[1024];
                 
                int len=0;
                while((len=in.read(buf))!=-1)
                {
       fos.write(buf,0,len);               
                }
                OutputStream out= s.getOutputStream();
                out.write("上传成功".getBytes());
                fos.close();
                s.close();
                ss.close();
        }

}

3 个回复

倒序浏览

回帖奖励 +1


客户端传完图片后,但是并没有发送结束标记,所以服务端并不知道已经传完了,一直在等着接收文件,只有给服务端发送结束标记才行,可以约定一个结束标记,比如在最后发送个“over”之类的,这样服务端接到之后就知道接收完成了,才会给客户端回馈信息,但是这样不专业也容易出问题,比如如果文件内有“over”内容就会出错,所以Socket有自定一个shundownOutput方法,传完文件后调用该方法关闭其输出流,实际就是在向服务端发送文件上传结束标记,看图:

还有,最好把刷新流,因为用到了字节数组缓冲,凡是有缓冲的都要刷新,免得数据丢失。
回复 使用道具 举报
本帖最后由 赵兵锋 于 2012-6-10 03:49 编辑
  1. public class PicServer {
  2.         public static void main(String[] args) throws Exception {
  3.                 ServerSocket ss = new ServerSocket(1007);
  4.                 Socket s = ss.accept();
  5.                 InputStream in = s.getInputStream();
  6.                 FileOutputStream fos = new FileOutputStream("e:/server.jpg");
  7.                 byte[] buf = new byte[1024];
  8.                 OutputStream out = s.getOutputStream();
  9.                 int len = 0;
  10.                 while ((len = in.read(buf)) ==1024) {//只有当客户端的输出流close后,此处的len才会为-1,但客户端若将输出流关掉,连接也会关掉。所以这里以读出的是否是1024个字节判断,不是1024就表示这是最后一组数据了
  11.                         fos.write(buf, 0, len);
  12.                 }
  13.                 fos.write(buf, 0, len);//将最后一组数据写入到文件
  14.                 fos.flush();
  15.                 fos.close();//文件流可以关掉了
  16.                 out.write("上传成功".getBytes());
  17.                 out.flush();//写后一定要flush一下,强制将输出缓冲区中数据清空,写入流中
  18.                 in.close();//此时才可以关输入流,提早关会导致连接关闭
  19.                 s.close();
  20.                 ss.close();
  21.         }
  22. }
复制代码
  1. public class PicClient {
  2.         public static void main(String []  args)throws Exception
  3.     {
  4.             Socket s=new Socket("127.0.0.1",1007);
  5.             FileInputStream fis=new FileInputStream("e:/1.jpg");
  6.             OutputStream  out=s.getOutputStream();
  7.             byte[] buf=new byte[1024];
  8.             int len=0;
  9.             while((len=fis.read(buf))!=-1)
  10.             {               
  11.                     out.write(buf,0,len);
  12.             }
  13.             out.flush();//一定要有这步,保证数据都传过去了
  14.             fis.close();//文件流这时可以关了
  15.             InputStream in=s.getInputStream();//在此之前一定不能关闭out,不然连接也会关闭,这里就会报错
  16.             byte[] bufIn= new byte[1024];
  17.             int num=in.read(bufIn);
  18.             System.out.println(new String(bufIn,0,num));
  19.             out.close();//此时才可以关闭流
  20.             s.close();
  21.     }
  22. }
复制代码
回复 使用道具 举报
上面那位兄弟已经说的很明白了,没有结束标记,服务端会一直等客户端传数据。
给你一个我写的加入缓冲区,及时刷新的,可以有效的防止数据丢失。
  1. import java.io.*;
  2. import java.net.*;


  3. class TcpServer {

  4.         /**
  5.          * @param args
  6.          */
  7.         private static BufferedOutputStream bw;
  8.         private static BufferedInputStream brIn;

  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 try {
  12.                         ServerSocket ss = new ServerSocket(11000);

  13.                         Socket sk = ss.accept();

  14.                         brIn = new BufferedInputStream(sk.getInputStream());

  15.                         bw = new BufferedOutputStream(new FileOutputStream("d:\\1.jpg"));
  16.                        
  17.                         byte[] buf = new byte[1024];
  18.                         int len = 0;
  19.                         while((len = brIn.read(buf))!=-1){
  20.                                 bw.write(buf,0,len);
  21.                                 bw.flush();
  22.                         }
  23.                         OutputStream out = sk.getOutputStream();
  24.                         out.write("上传成功".getBytes());

  25.                 } catch (IOException e) {
  26.                         // TODO Auto-generated catch block
  27.                         e.printStackTrace();
  28.                 }
  29.         }

  30. }




  31. class TcpClient {

  32.         /**
  33.          * @param args
  34.          */
  35.         private static BufferedInputStream br;
  36.         private static BufferedOutputStream bfout;
  37.         private static Socket sk;

  38.         public static void main(String[] args) {
  39.                 // TODO Auto-generated method stub
  40.                

  41.                 try {
  42.                         sk = new Socket("192.168.1.254",11000);
  43.                        
  44.                         br = new BufferedInputStream(new FileInputStream("c:\\1.jpg"));
  45.                         bfout = new BufferedOutputStream(sk.getOutputStream());

  46.                         byte[] buf = new byte[1024];
  47.                        
  48.                         int len = 0;
  49.                        
  50.                         while((len=br.read(buf))!= -1){
  51.                                 bfout.write(buf,0,len);
  52.                                 bfout.flush();
  53.                         }
  54.                        
  55.                         sk.shutdownOutput();
  56.                         InputStream in = sk.getInputStream();
  57.                         byte[] bufIn = new byte[1024];

  58.                         int num = in.read(bufIn);
  59.                         System.out.println(new String(bufIn,0,num));



  60.                 } catch (IOException e) {
  61.                         // TODO Auto-generated catch block
  62.                         e.printStackTrace();
  63.                 } finally {
  64.                         try {
  65.                                 if (br != null)
  66.                                         br.close();
  67.                         } catch (IOException e) {
  68.                                 e.printStackTrace();
  69.                         }
  70.                         try {
  71.                                 if (bfout != null)
  72.                                         bfout.close();
  73.                         } catch (IOException e) {
  74.                                 e.printStackTrace();
  75.                         }
  76.                         try {
  77.                                 if (sk != null)
  78.                                         sk.close();
  79.                         } catch (IOException e) {
  80.                                 e.printStackTrace();
  81.                         }
  82.                 }
  83.         }
  84. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马