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

© 张然龙 金牌黑马   /  2014-6-1 21:38  /  1138 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张然龙 于 2014-6-2 10:05 编辑

不管怎么试都不能跳出while循环,不管是上传端还是接收端都是如此 求各位解决一下,我用println打印了一下len的值 ,发现一直是1024.根本没出现过其他数字 这是为什么呢?而且我的图片完美的上传发送了过去....
  1. import java.io.*;
  2. import java.net.*;

  3. class Dxc implements Runnable
  4. {
  5.         public void run()
  6.         {
  7.                 try
  8.                 {
  9.                         Socket s=new Socket("192.168.1.106",12000);
  10.                         OutputStream os=s.getOutputStream();
  11.                         FileInputStream fis=new FileInputStream("D:\\截图.bmp");
  12.                         byte b[]=new byte[1024];
  13.                         int len;
  14.                         while ((len=fis.read(b))!=-1)
  15.                         {
  16.                                 os.write(b,0,len);
  17.                                 os.flush();
  18.                         }
  19.                         System.out.println("上传端跳出循环");
  20.                         s.shutdownOutput();
  21.                         s.close();
  22.                         fis.close();
  23.                 } catch (Exception e){}
  24.         }
  25. }
  26. class Dxc2 implements Runnable
  27. {
  28.         public void run()
  29.         {
  30.                 try
  31.                 {
  32.                         ServerSocket ss=new ServerSocket(12000);
  33.                         Socket s=ss.accept();
  34.                         InputStream is=s.getInputStream();
  35.                         byte b[]=new byte[1024*1024];
  36.                         int len;
  37.                         BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
  38.                         while((len=is.read(b))!=-1)
  39.                         {
  40.                                 bw.write(b,0,len);
  41.                                 bw.flush();
  42.                         }
  43.                         System.out.println("接收端跳出循环");
  44.                         ss.close();
  45.                         bw.close();
  46.                 }catch(Exception e){}
  47.         }
  48. }
  49. public class Demo3
  50. {
  51.         public static void main(String args[])throws Exception
  52.         {
  53.                 new Thread(new Dxc2()).start();
  54.                 Thread.sleep(200);
  55.                 new Thread(new Dxc()).start();
  56.         }
  57. }
复制代码




评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1

查看全部评分

6 个回复

倒序浏览
其实我就抢个沙发
回复 使用道具 举报

你不会是来刷金币的吧!......:Q
回复 使用道具 举报
你是在逗我们玩吗?看看
  1. package demo;
  2. import java.io.*;
  3. import java.net.*;

  4. class Dxc implements Runnable
  5. {
  6.         public void run()
  7.         {
  8.                 try
  9.                 {
  10.                         Socket s=new Socket("192.168.1.101",12000);
  11.                         OutputStream os=s.getOutputStream();
  12.                         FileInputStream fis=new FileInputStream("D:\\1.jpg");
  13.                         byte b[]=new byte[1024];
  14.                         int len;
  15.                         while ((len=fis.read(b))!=-1)
  16.                         {
  17.                                 os.write(b,0,len);
  18.                                 System.out.println(len);
  19.                                 os.flush();
  20.                         }
  21.                         System.out.println("上传端跳出循环");
  22.                         s.shutdownOutput();
  23.                         s.close();
  24.                         fis.close();
  25.                 } catch (Exception e){}
  26.         }
  27. }
  28. class Dxc2 implements Runnable
  29. {
  30.         public void run()
  31.         {
  32.                 try
  33.                 {
  34.                         ServerSocket ss=new ServerSocket(12000);
  35.                         Socket s=ss.accept();
  36.                         InputStream is=s.getInputStream();
  37.                         byte b[]=new byte[1024*1024];
  38.                         int len;
  39.                         BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
  40.                         while((len=is.read(b))!=-1)
  41.                         {
  42.                                 bw.write(b,0,len);
  43.                                 System.out.println(len);
  44.                                 bw.flush();
  45.                         }
  46.                         System.out.println("接收端跳出循环");
  47.                         ss.close();
  48.                         bw.close();
  49.                 }catch(Exception e){}
  50.         }
  51. }
  52. public class Demo3
  53. {
  54.         public static void main(String args[])throws Exception
  55.         {
  56.                 new Thread(new Dxc2()).start();
  57.                 Thread.sleep(200);
  58.                 new Thread(new Dxc()).start();
  59.         }
  60. }
复制代码

输出结果:
  1. 1024
  2. 1024
  3. 1024
  4. 1024
  5. 1024
  6. 829
  7. 829
  8. 上传端跳出循环
  9. 接收端跳出循环
复制代码

最后图片复制成功

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1

查看全部评分

回复 使用道具 举报
.....我的和你的不一样啊....什么情况
回复 使用道具 举报
无语了我都..我这个最后一个是1024,然后就没了,然后while循环就卡住了,上传端跳出循环这个控制台就没有...
回复 使用道具 举报
睡一觉..重启下电脑...好了..无语.. 不好意思了大家..
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马