Copying a File from Client to Server

我的梦境 提交于 2019-12-25 02:05:35

问题


I'm trying to copy a file from Client to Server, in Java, like so:

Client:

public class Client {

    public static void main(String[] args) throws Exception {
        String fileName = "D:\\6282.mp3";

        try {

        } catch (Exception e) {
            Scanner scanner = new Scanner(System.in);
            String file_name = fileName;

            File file = new File(file_name);
            Socket socket = new Socket("localhost", 3332);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            oos.writeObject(file.getName());

            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[Server.BUFFER_SIZE];
            Integer bytesRead = 0;

            while ((bytesRead = fis.read(buffer)) > 0) {
                oos.writeObject(bytesRead);
                oos.writeObject(Arrays.copyOf(buffer, buffer.length));
            }

            oos.close();
            ois.close();
            System.exit(0);
        }

    }

}

Server:

public class Server extends Thread {

    public static final int PORT = 3332;
    public static final int BUFFER_SIZE = 626;

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);

            while (true) {
                Socket s = serverSocket.accept();
                saveFile(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveFile(Socket socket) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        FileOutputStream fos = null;
        byte[] buffer = new byte[BUFFER_SIZE];

        // 1. Read file name.  
        Object o = ois.readObject();

        if (o instanceof String) {
            fos = new FileOutputStream(o.toString());
        } else {
            throwException("Something is wrong");
        }

        // 2. Read file to the end.  
        Integer bytesRead = 0;

        do {
            o = ois.readObject();

            if (!(o instanceof Integer)) {
                throwException("Something is wrong");
            }

            bytesRead = (Integer) o;

            o = ois.readObject();

            if (!(o instanceof byte[])) {
                throwException("Something is wrong");
            }

            buffer = (byte[]) o;

            // 3. Write data to output file.  
            fos.write(buffer, 0, bytesRead);

        } while (bytesRead == BUFFER_SIZE);

        System.out.println("File transfer success");

        fos.close();

        ois.close();
        oos.close();
    }

    public static void throwException(String message) throws Exception {
        throw new Exception(message);
    }

    public static void main(String[] args) {
        new Server().start();
    }
}

When I run I get:

run:
BUILD SUCCESSFUL (total time: 0 seconds)

but nothing really happens. This is my first at Client-Server and I'm not sure what I'm getting wrong.

Please help. Thank you.


回答1:


Some issues in your code are:

For the client ,you have written the entire code in the catch block, which will not work unless an exception occurs.

You are trying to pass the name of the file here instead of the file.

oos.writeObject(file.getName());

You need to run the server, then the client. here is a sample working code:

Client:

public class Client {

    public static void main(String[] args) throws Exception {
        String fileName = "C:\\2048.jpg";

        try {
            File file = new File(fileName);
            Socket socket = new Socket("localhost", 3332);
            byte[] mybytearray = new byte[(int) file.length()];
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            bis.read(mybytearray, 0, mybytearray.length);
            OutputStream os = socket.getOutputStream();
            os.write(mybytearray, 0, mybytearray.length);
            os.flush();
            os.close();
        } catch (Exception e) {
        }

    }
}

Server:

public class Server extends Thread {

    public static final int PORT = 3332;
    public static final int BUFFER_SIZE = 626;

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                Socket s = serverSocket.accept();
                saveFile(s);
            }
        } catch (Exception e) {
        }
    }

    private void saveFile(Socket socket) throws Exception {
        InputStream ois = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("C:\\2049.jpg");;

        byte[] mybytearray = new byte[1024];
        System.out.println("Reading file from server...");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead;
        while ((bytesRead = ois.read(mybytearray)) != -1) {
            bos.write(mybytearray);
        }

        bos.close();
        System.out.println("Writing file complete...");

    }

    public static void main(String[] args) {
        new Server().start();
    }
}


来源:https://stackoverflow.com/questions/22245846/copying-a-file-from-client-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!