两人之间的文件传送(本地和网络)和信息交流

(先运行)服务端:

import myChatRoom.ChatServer;
import superSendFile.sendFile;

import java.util.Scanner;

public class serve {
    public static Scanner input = new Scanner(System.in);
    public static boolean Flag = true;

    public static void main(String[] args) {
        try {
            menu();
        } catch (Exception e) {
//            throw new RuntimeException(e);
        }
    }

    public static void menu() throws Exception {
        System.out.println("请您输入你想做的事情");
        System.out.println("1.传递文件   2.聊天   3.退出");
        switch (input.nextInt()) {
            case 1:
                sendFile.menu();
                break;
            case 2:
                ChatServer.menu();
                break;
            case 3:
                while (Flag) {
                    System.out.println("您确定要退出吗,退出请按Y,不退出请按N");
                    char userChoose = input.next().toUpperCase().charAt(0);
                    switch (userChoose) {
                        case 'Y':
                            Flag = false;
                            break;
                        case 'N':
                            menu();
                        default:
                            break;
                    }
                }
                break;
            default:
                System.out.println("您输入错误!!!,请重新输入");
                menu();
        }
    }
}

(后运行)客户端:

import myChatRoom.ChatClient;
import superSendFile.acceptFile;

import java.util.Scanner;

public class client {
    public static Scanner input=new Scanner(System.in);
    public static void main(String[] args) {
        try {
            menu();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void menu() throws Exception {
        System.out.println("请您输入你想做的事情");
        System.out.println("选择1 或 2 的时候请确保服务端打开!!");
        System.out.println("1.传递文件   2.聊天   3.退出");

        switch (input.nextInt()){
            case 1:
                acceptFile.menu();
                break;
            case 2:
                ChatClient.show();
                break;
            case 3:
                break;
            default:
                System.out.println("您输入错误!!!");
        }
    }
}
以下是服务与客户端和服务端实现功能类
有两个功能:没有Main方法添加Main方法调用menu/show方法:单独运行!

功能一:网站上爬取资源发送给另一个人:(TCP客户端)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class sendFile {
    public static String IP="127.0.0.1";
    public static int PORT=12345;
    public static Scanner input=new Scanner(System.in);
    public static Socket socket;
    //public static Socket socket=new Socket(IP,PORT);
    static {
        try {
            socket = new Socket(IP,PORT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        try {
            menu();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String URLGetUserFileName(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入网址名称:");
        String URLName = input.next();
        return URLName;
    }

    public static void menu() throws Exception{
        System.out.println("请输入您的选择:");
        System.out.println("1.获取网络文件   2.获取这台电脑的本机文件   3.退出系统");

        Integer userChoose = input.nextInt();
        switch (userChoose){
            case 1:
                String urlName = URLGetUserFileName();
                sendURLFile(new URL(urlName));
                cyclicOperation();
            case 2:
                String localFileName = getLocalFileName();
                sendLocalFile(localFileName);
                cyclicOperation();
                break;
            case 3:
                break;
            default:
                System.out.println("您输入错误!!!");
                break;
        }
    }
    public static void sendURLFile(URL url) throws Exception{
        URLConnection urlConnection = url.openConnection();
        HttpURLConnection connection = (HttpURLConnection) urlConnection;
        InputStream is = connection.getInputStream();
        int len=0;
        byte[] b=new byte[1024];
        System.out.println("开始在网站下载资源");
        while ((len=is.read(b))!=-1){
            OutputStream os = socket.getOutputStream();
            os.write(b,0,len);
        }
        socket.shutdownOutput();
        System.out.println("下载结束");
    }
    public static void sendLocalFile(String LocalFile)throws Exception{
        FileInputStream fis=new FileInputStream(LocalFile);
        int len=0;
        byte[] b=new byte[1024];
        System.out.println("开始发送本机资源");
        while ((len=fis.read(b))!=-1){
            OutputStream os = socket.getOutputStream();
            os.write(b,0,len);
        }
        socket.shutdownOutput();
        System.out.println("本机资源发送完毕");
    }
    public static String getLocalFileName(){
        System.out.println("请输入文件地址:");
        return input.next();
    }
    public static void cyclicOperation() throws Exception {
        System.out.println("您是否要退出 确定退出输入Y,不退出输入N");
        char c = input.next().toUpperCase().charAt(0);
        if(c=='N'){
           socket=new Socket(IP,PORT);
            menu();
        } else if (c=='Y') {

        }
    }
}

:接收客户端爬取网上资源:(TCP客户端):

package superSendFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class acceptFile {
    public static ServerSocket serverSocket;

    static {
        try {
            serverSocket = new ServerSocket(12345);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Scanner input=new Scanner(System.in);
    public static String myLocalFil;
    public static String suffixName;
    public static void main(String[] args) {
        try {
            menu();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void menu() throws Exception {
        acceptFile(serverSocket);
    }
    public static void acceptFile(ServerSocket serverSocket)throws Exception{
        System.out.println("等待连接");
        Socket socket = serverSocket.accept();
        System.out.println("连接成功!");
        InputStream is = socket.getInputStream();
        System.out.println("文件保持地址:");
        myLocalFil=input.next();
        System.out.println("请输入后缀名   比如.mp4  .mp3  .exe");
        suffixName=input.next();
        FileOutputStream fos=new FileOutputStream(myLocalFil+suffixName);
        System.out.println("开始接收:");
        int len=0;
        byte[] b=new byte[1024];
        while ((len=is.read(b))!=-1){
            fos.write(b,0,len);
        }
        System.out.println("接收完成");
        cyclicOperation();
    }
    public static void cyclicOperation() throws Exception {
        System.out.println("您是否要退出 确定退出输入Y,不退出输入N");
        char c = input.next().toUpperCase().charAt(0);
        if(c=='N'){
            menu();
        } else if (c=='Y') {

        }
    }
}
功能二:两人互相聊天:(TCP客户端)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ChatClient {
    public static void main(String[] args) {
        show();
    }
    public static void show(){
        try {
            Socket socket = new Socket("127.0.0.1", 12345);
            System.out.println("已连接到服务器");

            BufferedReader in = new BufferedReader
                        (new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            BufferedReader consoleReader = new BufferedReader
                        (new InputStreamReader(System.in));

            String serverMessage, clientMessage;

            while (true) {
                serverMessage = in.readLine();
                System.out.println("服务器: " + serverMessage);

                System.out.print("我: ");
                clientMessage = consoleReader.readLine();
                out.println(clientMessage);

                if (serverMessage.equalsIgnoreCase("exit") || clientMessage.equalsIgnoreCase("exit")) {
                    break;
                }
            }

            in.close();
            out.close();
            consoleReader.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

:两人互相聊天:(TCP服务端):


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
    public static void main(String[] args) {
        menu();
    }
    public static void menu(){
        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            System.out.println("等待客户端连接...");

            Socket clientSocket = serverSocket.accept();
            System.out.println("客户端已连接:" + clientSocket.getInetAddress().getHostAddress());

            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

            String clientMessage, serverMessage;

            while (true) {
                System.out.print("我: ");
                serverMessage = consoleReader.readLine();
                out.println(serverMessage);

                clientMessage = in.readLine();
                System.out.println("客户端: " + clientMessage);

                if (serverMessage.equalsIgnoreCase("exit") || clientMessage.equalsIgnoreCase("exit")) {
                    break;
                }
            }

            in.close();
            out.close();
            consoleReader.close();
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值