某人的作业,有用的拿走

今天下午五分钟给某人赶制的作业一份,有需要的拿走。
关键字:java socket tcp 文件传输

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;

/*
 * Copyrights (C) 2008 Bearice ([email protected])
 * Release under GNU/GPL Version 2.
 */
/**
 *
 * @author Bearice
 */
public class FileTransport {

    public static void send(String args[]) throws Exception {
        String host = args[1];
        int port = Integer.parseInt(args[2]);
        System.out.printf("Connecting to %s:%d\n", host, port);
        Socket socket = SocketFactory.getDefault().createSocket(host, port);
        FileInputStream fis = new FileInputStream(args[3]);
        int b;
        System.out.println("Sending...");
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        while ((b = fis.read()) != -1) {
            bos.write(b);
        }
        bos.close();
    }

    public static void recv(String args[]) throws Exception {
        int port = Integer.parseInt(args[1]);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[2]));
        System.out.printf("Listening on port %d\n", port);
        Socket socket = ServerSocketFactory.getDefault().createServerSocket(port).accept();
        int b;
        InputStream is = socket.getInputStream();
        System.out.println("Recving...");
        while ((b = is.read()) != -1) {
            bos.write(b);
        }
        bos.close();
    }

    public static void main(String[] args) throws Exception {
        if (args.length == 4 && args[0].equalsIgnoreCase("send")) {
            send(args);
        } else if (args.length == 3 && args[0].equalsIgnoreCase("recv")) {
            recv(args);
        } else {
            System.out.println("Usage:");
            System.out.println("FileTransport [recv|send <host>] <port> <file>");
        }
    }
}
>_<|| 我手贱!!这个有问题!绝对有问题!!一般般啦,真的很一般般。还不错哦~小表扬一下!GJ!乃就是新世界的神様了,快去拯救世界吧! (No Ratings Yet)
Loading...

一人吐槽

  1. 阻塞干扰说道:
    骑着 Unknown Unknown 和 Unknown Unknown

    熊,你的博客访问速度好慢的说,我是说访问的时候好卡……

春菜 对话 相声
双击调戏
双击调戏