如何使用套接字将文件从服务器端传输到客户端?

时间:2011-02-23 00:18:35

标签: java sockets client-server client-side server-side

我在哪里开始执行此任务时遇到问题,我想要一些示例或输入我应该如何设置我的服务器/客户端组件来接收和发送数据,包括让客户端下载图像。

这是我的客户端代码:

package V3;

import java.io.*;
import java.net.*;

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

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("localhost", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
        if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
        }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}

这是我的服务器端代码:

package V3;

import java.net.*;
import java.io.*;

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

        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }

        while (listening)
        new Version3ServerThread(serverSocket.accept()).start();

        serverSocket.close();

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        Version3Protocol kkp = new Version3Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

为进一步说明添加了线程类:

package V3;
    import java.net.*;
    import java.io.*;
    import java.io.FileWriter;

    public class Version3ServerThread extends Thread {
        private Socket socket = null;

        public Version3ServerThread(Socket socket) {
        super("Version3ServerThread");
        this.socket = socket;
        }

        public void run() {

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

            String inputLine, outputLine;
            Version3Protocol kkp = new Version3Protocol();
            outputLine = kkp.processInput(null);
            out.println(outputLine);

            while ((inputLine = in.readLine()) != null) {
            outputLine = kkp.processInput(inputLine);
            out.println(outputLine);
            if (outputLine.equals("Bye"))
                break;
                    if(kkp.getInteraction()){
                      Logging.writeToFile(socket.getInetAddress());
                    }
            }
            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }

1 个答案:

答案 0 :(得分:0)

到目前为止,没有文件传输功能的客户端 - 服务器通信代码对我来说很好。我想编译以找到任何错误,但你没有包括Version3Protocol和Logging。如果它确实编译了,那么下一步就是使用FileInputStream打开要在服务器上传输的文件,并使用FileOutputStream在客户端上写入新文件:

  

http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileInputStream.html   http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileOutputStream.html

然后,您可以将文件读入缓冲区,使用套接字传输,在客户端接收,然后将缓冲区内容复制到FileOutputStream中。