A simple Java program which implement remote file transmit (Multi-thread)

本文介绍了一个基于Java的远程文件传输系统,包括客户端和服务端的设计与实现。客户端能够连接到指定的服务器并请求下载特定文件,而服务端则能处理来自多个客户端的文件下载请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.Toxic.iFile;


import java.io.*;

import java.net.*;


public class RemoteFileClient {

    protected BufferedInputStream socketReader;

    protected PrintWriter socketWriter;

    protected String hostIp;

    protected int hostPort;

    public RemoteFileClient(String hostIp, int hostPort) {

        this.hostIp = hostIp;

        this.hostPort=hostPort; 

    }

    public String getFile(String fileNameToGet) {

        StringBuffer fileLines = new StringBuffer();

        try {

            socketWriter.println(fileNameToGet);            

            socketWriter.flush();

            

            File file = new File("/Users/Toxic/Desktop/new.mp4"); /* receive path and file name*/

            BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(file));

            int read;

            while((read = socketReader.read())!= -1) {

                fout.write(read);

            }

            fout.close();

        }

        catch(IOException e) {

            System.out.println("Error reading from file: "+fileNameToGet);

        }

        return fileLines.toString();

    }


    public void setUpConnection() {

        try {

            Socket client = new Socket(hostIp,hostPort);

            socketReader = new BufferedInputStream(client.getInputStream());

            socketWriter = new PrintWriter(client.getOutputStream());

        }

        catch(UnknownHostException e) {

            System.out.println("Error1 setting up socket connection: unknown host at "+hostIp+":"+hostPort);

        }

        catch(IOException e) {

            System.out.println("Error2 setting up socket connection: "+e);

        }

    }

    public void tearDownConnection() {

        try {

            socketWriter.close(); 

            socketReader.close();

        }catch(IOException e) {            

            System.out.println("Error tearing down socket connection: "+e);

        }

    }

    public static void main(String args[]) throws IOException {

        RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",8001); /* Ip address & port to get connection to server */

        remoteFileClient.setUpConnection();

        StringBuffer fileContents = new StringBuffer();

        fileContents.append(remoteFileClient.getFile("/Users/Toxic/Desktop/-.mp4")); /* path and file name to get file from server*/      

    }

}


/**

 *  MultithreadRemoteFileServer.java

 *  Toxic 2011.05.26

 * 

 */


package com.Toxic.iFile;

import java.io.*;

import java.net.*;


public class MultithreadRemoteFileServer {

int listenPort;

public MultithreadRemoteFileServer(int listenPort) {

this.listenPort = listenPort;

}

public void acceptConnections() {

try {

ServerSocket server = new ServerSocket(listenPort, 5);

Socket incomingConnection = null;

while(true) {

incomingConnection = server.accept();

handleConnection(incomingConnection);

}

} catch(BindException e) {

System.out.println("Unable to bind to port " + listenPort);

} catch(IOException e) {

System.out.println("Unable to instatiate a ServerSocket on port: " + listenPort);

}

}

public void handleConnection(Socket connectionToHandle) {

new Thread(new ConnectionHandler(connectionToHandle)).start();

}

public static void main(String args[]) {

MultithreadRemoteFileServer server = new MultithreadRemoteFileServer(8001); /* server port to provide service*/

server.acceptConnections();

}

}


/**

 *  ConnectionHandler.java

 *  Toxic 2011.05.26

 *  

 */

package com.Toxic.iFile;


import java.io.*;

import java.net.*;



public class ConnectionHandler implements Runnable {

protected Socket socketToHandle;

public ConnectionHandler(Socket socketToHandle) {

this.socketToHandle = socketToHandle;

}


public void run() {

try {

BufferedOutputStream streamWriter = new BufferedOutputStream(socketToHandle.getOutputStream());

BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));

String fileToRead = streamReader.readLine();

BufferedInputStream fileReader = new BufferedInputStream(new FileInputStream(fileToRead));

int read;

while((read = fileReader.read())!= -1) {

streamWriter.write(read);

}

fileReader.close();

streamWriter.close();

streamReader.close();

} catch(Exception e) {

System.out.println("Error handling a client: " + e);

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值