zoukankan      html  css  js  c++  java
  • Java Tcp文件传输---转载

    /**
    客户端
    1、服务端点
    2、读取客户端已有的文件数据
    3、通过socket输出流发给服务端
    4、读取服务端反馈信息
    5、关闭
    **/
    import java.io.*;
    import java.net.*;
    class  UploadClient
    {
    public static void main(String[] args) throws Exception
    {
    Socket s = new Socket("127.0.0.1",4434);
    FileInputStream fis = new FileInputStream("D://2.png");
    OutputStream out = s.getOutputStream();
    byte[] buf = new byte[1024];
    int len = 0;
    while((len = fis.read(buf))!=-1)
    {
    out.write(buf,0,len);
    }
    //数据已经完成的时候执行shutdownOutput
    s.shutdownOutput();
    InputStream is = s.getInputStream();
    byte[] bufin = new byte[1024];
    int lenin = is.read(bufin);
    System.out.println(new String(bufin,0,lenin));
    fis.close();
    s.close();
    }
    }
    /**
    服务器端
    */
    class  UploadServer
    {
    public static void main(String[] args)throws Exception
    {
    ServerSocket ss = new ServerSocket(4434);
    Socket s = ss.accept();
    InputStream is = s.getInputStream();
    FileOutputStream fos = new FileOutputStream("C://test.jpg");
    byte[] buf = new byte[1024];
    int len = 0;
    while((len = is.read(buf))!=-1)
    {
    fos.write(buf,0,buf.length);
    }
    OutputStream out = s.getOutputStream();
    out.write("上传成功".getBytes());
    fos.close();
    s.close();
    ss.close();
    }
    }
  • 相关阅读:
    hdu1066之数学题
    hdu1065计算几何
    hdu1060
    hdu1056
    appium安装说明
    LR安装说明
    网络编程
    读写excel
    dom
    HTML
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/4276914.html
Copyright © 2011-2022 走看看