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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 湛添友 中级黑马   /  2014-4-27 11:58  /  831 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//为什么 随便打开 下面一个  flush()  程序就停下了??

package net;
/*
* 使用 Tcp传输协议 上传图片
*/
import java.io.*;
import java.net.*;
public class PicturesDemo
{
public static void main(String args[]) throws IOException, IOException
{
  Socket s=new Socket("192.168.1.8",4456);
  
  
  
   InputStream in=s.getInputStream();
  OutputStream out=s.getOutputStream();
  
  
  BufferedInputStream bis=new BufferedInputStream(in);
  BufferedOutputStream bos=new BufferedOutputStream(out);
  BufferedInputStream bisFile=new BufferedInputStream(new FileInputStream("D:\\abc\\1.JPG"));
  
  
  int l=0;
  while((l=bisFile.read())!=-1)
  {
   bos.write(l);
//  bos.flush();
  }
  
  
  
  
  s.shutdownOutput();
  
  byte[]buf2=new byte[1024];
  int len2=in.read(buf2);
  
  System.out.println(new String(buf2,0,len2));
  bisFile.close();
  s.close();
  
}
}
class Server4
{
public static void main(String args[]) throws Exception
{
  
  
  ServerSocket ss=new ServerSocket(4456);
  Socket s=ss.accept();
  String ip=s.getInetAddress().getHostAddress();
  System.out.println(ip+"connecet");
  InputStream in=s.getInputStream();
  OutputStream out=s.getOutputStream();

  
  
  BufferedInputStream bis=new BufferedInputStream(in);
  BufferedOutputStream bos=new BufferedOutputStream(out);
  BufferedOutputStream bosFile=new BufferedOutputStream(new FileOutputStream("D:\\a.jpg"));
  
  
  int l=0;
  while((l=bis.read())!=-1)
  {
   bosFile.write(l);
  // bosFile.flush();
  }
  
  

  
  
  out.write("图片已经上传".getBytes());
  

  bosFile.close();
  s.close();
  ss.close();
  
}
}















2 个回复

倒序浏览
大哥,加点注释嘛 。我们这些新手也可以学学啊。
回复 使用道具 举报
我有一个tcp上传图片的代码给你参考:

  1. /*
  2. tcp上传图片。
  3. */
  4. import java.io.*;
  5. import java.net.*;
  6. /*
  7. 客户端:
  8. 1,建立端点。
  9. 2,读取客户端图片数据。
  10. 3,利用Socket方法getOutputStream将数据写入到输入流中,传给服务端。
  11. 3,接受读取服务端反馈信息。
  12. 4,关闭。
  13. */


  14. class PicTest
  15. {
  16. public static void main(String[] args)throws Exception
  17. {
  18.          Socket s=new Socket("113.16.82.67",10006);
  19.         PicClient cli=new PicClient(s);
  20.           ServerSocket ss=new ServerSocket(10006);
  21.           PicServer ser=new PicServer(ss);

  22.          
  23.           new Thread(ser).start();
  24.           Thread.sleep(5000);
  25.           new Thread(cli).start();
  26. }


  27. }
  28. class PicClient implements Runnable
  29. {
  30.         private Socket s;
  31.           PicClient(Socket s)
  32.                 {
  33.                         this.s=s;
  34.             }

  35.         public void run()
  36.         {       
  37.                 try
  38.                 {
  39.                                          FileInputStream fis=new FileInputStream("c:\\1.jpg");
  40.   OutputStream out=s.getOutputStream();
  41.   byte[] buf=new byte[1024];
  42.   int len=0;
  43.   while ((len=fis.read(buf))!=-1)
  44.   {
  45.    out.write(buf,0,len);
  46.   }
  47.   s.shutdownOutput();
  48.   InputStream in=s.getInputStream();
  49.   byte[] bufinfo=new byte[1024];
  50.   int leninfo=in.read(bufinfo);
  51.   System.out.println(new String(bufinfo,0,leninfo));
  52.   fis.close();
  53.   s.close();
  54.                 }
  55.                 catch (Exception ex)
  56.                 {

  57.                 }

  58.        
  59.         }



  60. }




  61. class PicServer implements Runnable
  62. {
  63.         private ServerSocket ss;
  64.         PicServer(ServerSocket ss)
  65.         {
  66.                 this.ss=ss;
  67.         }

  68.         public void run()
  69.         {
  70.                 try
  71.                 {
  72.                                         Socket s=ss.accept();
  73.   InputStream in=s.getInputStream();
  74.   FileOutputStream out=new FileOutputStream("c:\\jay.jpg");
  75.   byte[]buf=new byte[1024];
  76.   int len=0;
  77.   while ((len=in.read(buf))!=-1)
  78.   {
  79.    out.write(buf,0,len);
  80.   }
  81.   OutputStream outinfo=s.getOutputStream();
  82.   outinfo.write("上传成功!".getBytes());
  83.   out.close();
  84.   s.close();
  85.   ss.close();
  86.                 }
  87.                 catch (Exception ex)
  88.                 {
  89.                 }

  90.        
  91.         }
  92.   

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