zoukankan      html  css  js  c++  java
  • Java简单文件传输 socket简单文件传输示例

    服务器端代码:

    import java.io.*;
    import java.net.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: HYY
     * Date: 13-10-30
     * Time: 下午2:15
     * To change this template use File | Settings | File Templates.
     */
    public class Server {
    
        public static void main(String[] args) {
            ServerSocket serverSocket;
            BufferedInputStream bufferedInputStream;//文件读取流
            DataOutputStream dataOutputStream;//向客户端发送的数据流
            try {
                serverSocket = new ServerSocket(10000);
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接。");
                dataOutputStream = new DataOutputStream(socket.getOutputStream());
                bufferedInputStream = new BufferedInputStream(new FileInputStream("c:/temp.sql"));
                byte[] buffer = new byte[8192];
                int read;
                while ((read = bufferedInputStream.read(buffer)) != -1) {
                    dataOutputStream.write(buffer, 0, read);
                    dataOutputStream.flush();
                }
                System.out.println("文件传输完毕!");
                bufferedInputStream.close();
                dataOutputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
            }
    
        }
    }

    客户端代码:

    import java.io.*;
    import java.net.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: HYY
     * Date: 13-10-30
     * Time: 下午2:29
     * To change this template use File | Settings | File Templates.
     */
    public class Client {
        public static void main(String[] args) {
            DataInputStream dataInputStream;//从服务器读取数据输入流
            FileOutputStream fileOutputStream;//写入本地文件流
            try {
                Socket socket = new Socket("127.0.0.1", 10000);
                System.out.println("连上服务器。");
                dataInputStream = new DataInputStream(socket.getInputStream());
                File file = new File("d:/temp_copy.sql");
                fileOutputStream = new FileOutputStream(file);
                byte[] buffer = new byte[8192];
                int read;
                while ((read=dataInputStream.read(buffer))>0) {
                    fileOutputStream.write(buffer, 0, read);
                }
                System.out.println("文件接收完毕!");
                fileOutputStream.close();
                dataInputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    数据库中group by和having语法使用方法
    loadrunner---HTML 和URL两种模式录制的区别
    loadrunner---设置检查点
    jmeter---接口测试
    H5前端页面性能测试
    Nginx服务器中的Nginx.conf配置文件主要内容解释
    测试用例的八大要素
    mysql在linux中的操作命令
    软件兼容性测试
    liunx中的gcc命令
  • 原文地址:https://www.cnblogs.com/wuyou/p/3396723.html
Copyright © 2011-2022 走看看