• Java学习笔记——Socket实现文件传输


    我越是逃离,却越是靠近你。

    我越是背过脸,却越是看见你。

    我从你开始,

    我在你结束。

    需求:实现局域网下socket传输文件.

    客户端步骤:

    1、建立与服务器的连接

    2、创建client输出流

    3、创建输入流,绑定本地文件

    4、读取文件后输出给服务器

    服务器端步骤:

    1、启动服务器

    2、创建accept输入流

    3、创建输出流绑定本地文件

    4、读取客户端信息写入本地文件

    1.字节流传输文件.

     1 public class Client {
     2 
     3     public static void main(String[] args) throws IOException {
     4         Socket client = new Socket("192.168.0.112", 50000);
     5         OutputStream os = client.getOutputStream();
     6         FileInputStream fis = new FileInputStream("/home/yanshaochen/public/2017-05-06 15-12-02屏幕截图.png");
     7         byte[] bytes = new byte[1024];
     8         int data;
     9         while((data = fis.read(bytes))!= -1){
    10             os.write(bytes, 0, data);
    11         }
    12         fis.close();
    13         client.close();
    14         System.out.println("发送成功!");
    15     }
    16 }
     1 public class Server {
     2 
     3     public static void main(String[] args) throws Exception {
     4         ServerSocket server = new ServerSocket(50000);
     5         System.out.println("listening..");
     6         Socket accept = server.accept();
     7         InputStream is = accept.getInputStream();
     8         FileOutputStream fos = new FileOutputStream("d:\download\屏幕截图.png");
     9         byte[] bytes = new byte[1024];
    10         int data;
    11         while((data = is.read(bytes))!=-1){
    12             fos.write(bytes, 0, data);
    13         }
    14         fos.close();
    15         server.close();
    16 }

    2.字符流传输文件txt.

     1 public class Client {
     2 
     3     public static void main(String[] args) throws IOException {
     4         
     5         Socket client = new Socket("192.168.0.112", 50000);
     6         OutputStream os = client.getOutputStream();
     7         Reader fr = new FileReader("/home/yanshaochen/public/zfsm.txt");
     8         BufferedReader br = new BufferedReader(fr);
     9         String buffer;
    10         while((buffer = br.readLine())!= null){
    11             buffer += "
    ";
    12             os.write(buffer.getBytes());
    13         }
    14         br.close();
    15         client.close();
    16         System.out.println("发送成功!");
    17     }
    18 }
     1 public class Server {
     2 
     3     public static void main(String[] args) throws Exception {
     4 
     5         ServerSocket server = new ServerSocket(50000);
     6         System.out.println("listening..");
     7         Socket accept = server.accept();
     8         InputStream is = accept.getInputStream();
     9         FileWriter fw = new FileWriter("d:\download\zfsm.txt");
    10         byte[] bytes = new byte[1024];
    11         int data;
    12         while((data = is.read(bytes)) != -1){
    13             fw.write(new String(bytes, 0, data));
    14         }
    15         fw.close();
    16         server.close();
    17     }
    18 
    19 }
  • 相关阅读:
    Android中的回调Callback
    vim编辑器配置及常用命令
    自定义View 和 ViewGroup
    BluetoothClass详解
    BluetoothServerSocket详解
    NSData转换成NSDictionary
    SDWebImage缓存图片的机制(转)
    非ARC和ARC下创建单利模式的宏定义,可以直接套用
    详细例子构建自定义cell
    使用FMDB框架来加载数据库
  • 原文地址:https://www.cnblogs.com/tomasman/p/6907538.html
Copyright © 2020-2023  润新知