本帖最后由 张然龙 于 2014-6-8 08:38 编辑
这样上传图片会丢数据,请问问题是出在哪里呢???
- class Fasong implements Runnable
- {
- public void run()
- {
- try
- {
- Socket s=new Socket ("192.168.1.106",19892);
- BufferedOutputStream bo=new BufferedOutputStream(s.getOutputStream());
- BufferedInputStream bi=new BufferedInputStream(new FileInputStream("D:\\截图.bmp"));
- byte b[]=new byte[1024];
- int len;
- while((len=bi.read(b))!=-1)
- {
- bo.write(b,0,len);
- }
- s.shutdownOutput();
- s.close();
- bi.close();
- System.out.println("图片发送完成!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class Jieshou implements Runnable
- {
- public void run()
- {
- try
- {
- ServerSocket ss=new ServerSocket(19892);
- Socket s=ss.accept();
- BufferedInputStream bi=new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D:\\截图保存.bmp"));
- byte b[]=new byte[1024];
- int len;
- while((len=bi.read(b))!=-1)
- {
- bo.write(b,0,len);
- }
- bo.close();
- ss.close();
- System.out.println("上传成功!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class Demo
- {
- public static void main(String[] args)throws Exception
- {
- new Thread(new Jieshou()).start();
- Thread.sleep(1000);
- new Thread(new Fasong()).start();
- }
- }
复制代码 |