TCP upload picture

From , 3 Years ago, written in Java, viewed 80 times.
URL https://pastebin.vip/view/b5a1fc20
  1. package cn.itcast_09;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11.  
  12. /*
  13.  * TCP上传图片的客户端:
  14.  *
  15.  */
  16.  
  17. public class UploadImageDemo {
  18.         public static void main(String[] args) throws IOException {
  19.                 //创建套接字
  20.                 Socket s = new Socket("192.168.1.103",4444);
  21.                 //封装图片文件对象和通道流对象
  22.                 BufferedInputStream is = new BufferedInputStream(new FileInputStream("C:\\a.png"));
  23.                 BufferedOutputStream os = new BufferedOutputStream(s.getOutputStream());
  24.                
  25.                 //读写数据
  26.                 byte [] bys = new byte[1024];
  27.                 int len;
  28.                 while((len=is.read(bys))!=-1){
  29.                         os.write(bys, 0, len);
  30.                         os.flush();
  31.                 }
  32.                
  33.                 s.shutdownOutput();
  34.                
  35.                 //读取反馈
  36.                 InputStream bos = s.getInputStream();
  37.                 byte [] by = new byte[1024];
  38.                 int lenth = 0;
  39.                 lenth=bos.read(by);
  40.                 String ss = new String(by,0,lenth);
  41.                 System.out.println(ss);
  42.                
  43.                 //释放资源
  44.                 is.close();
  45.                 s.close();
  46.                
  47.                
  48.                
  49.                
  50.                
  51.                
  52.                
  53.         }
  54.  
  55. }
  56.  

Reply to "TCP upload picture"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website