程序实现 传输图片

第一

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
public class Testuser {
 public static void main(String[] args) {
  try {
   ServerSocket ss = new ServerSocket(4848);
   while(true) {
    Socket server = ss.accept();
    FileOutputStream fos = new FileOutputStream("d:/"+UUID.randomUUID().toString()+".jpg");
    InputStream is = server.getInputStream();
    System.out.println(is.available());
    byte[] buf =new byte[8702];
    int len = -1;
    while((len = is.read(buf))!=-1) {
     fos.write(buf,0,len);
    }
    //is.read(buf);
    fos.write(buf);
    fos.flush();
    fos.close();
    is.close();
    
   }
  }catch(Exception e ) {
   
  }
 }
}
第2
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import org.omg.CORBA.portable.InputStream;
public class Fille {
 public static void main(String[] args) {
  String img ="D:\\Demo\\op\\1.jpg";
  try {
   Socket client = new Socket("192.168.0.116",4848);
   
    //FileOutputStream fos =new FileOutputStream("D:\\Demo\\we"+UUID.randomUUID().toString()+"jpg");
    
    FileInputStream fis =new FileInputStream(img);
    System.out.println(fis.available());
    byte[] buf = new byte[fis.available()];
    OutputStream os = client.getOutputStream();
    fis.read(buf);
    os.write(buf);
    os.flush();
    os.close();
    
    fis.close();
    //InputStream is =  server.getInputStream();
    
    
  }catch(Exception e) {
   
  }
 }
}
运行结果

 

 

posted @ 2020-06-17 19:26  信王勤宝  阅读(149)  评论(0编辑  收藏  举报