本帖最后由 张然龙 于 2014-6-2 10:05 编辑
不管怎么试都不能跳出while循环,不管是上传端还是接收端都是如此 求各位解决一下,我用println打印了一下len的值 ,发现一直是1024.根本没出现过其他数字 这是为什么呢?而且我的图片完美的上传发送了过去....- import java.io.*;
- import java.net.*;
- class Dxc implements Runnable
- {
- public void run()
- {
- try
- {
- Socket s=new Socket("192.168.1.106",12000);
- OutputStream os=s.getOutputStream();
- FileInputStream fis=new FileInputStream("D:\\截图.bmp");
- byte b[]=new byte[1024];
- int len;
- while ((len=fis.read(b))!=-1)
- {
- os.write(b,0,len);
- os.flush();
- }
- System.out.println("上传端跳出循环");
- s.shutdownOutput();
- s.close();
- fis.close();
- } catch (Exception e){}
- }
- }
- class Dxc2 implements Runnable
- {
- public void run()
- {
- try
- {
- ServerSocket ss=new ServerSocket(12000);
- Socket s=ss.accept();
- InputStream is=s.getInputStream();
- byte b[]=new byte[1024*1024];
- int len;
- BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("D:\\收到.bmp"));
- while((len=is.read(b))!=-1)
- {
- bw.write(b,0,len);
- bw.flush();
- }
- System.out.println("接收端跳出循环");
- ss.close();
- bw.close();
- }catch(Exception e){}
- }
- }
- public class Demo3
- {
- public static void main(String args[])throws Exception
- {
- new Thread(new Dxc2()).start();
- Thread.sleep(200);
- new Thread(new Dxc()).start();
- }
- }
复制代码
|