在客户端服务器程序中获取java.io.EOFException

时间:2016-05-11 06:08:33

标签: java netbeans client-server file-transfer

我正在编写一个代码,用于将文件从客户端发送到服务器并将该文件保存在特定位置。但是当我输入要在客户端发送的文件的名称时,我在服务器程序中收到异常。客户端程序成功运行并终止,但服务器端抛出异常。 服务器代码是:

import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;

public class server extends Thread {
    public static final int PORT = 3332;
    public static final int BUFFER_SIZE = 100;

    @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(new File("F:\\txt.txt"));
        } 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();
    }
} 

客户端程序是:

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.lang.*;
import java.util.Scanner;


public class client {
    public static void main(String[] args) throws Exception {
        String fileName = null;

       try {
            fileName = args[0];
        } catch (Exception e) {
        System.out.println("Enter the name of the file :");
        Scanner scanner = new Scanner(System.in);
        String file_name = scanner.nextLine();

        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);    
}
}  
}  

我得到的例外是:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2608)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at server.saveFile(server.java:43)
    at server.run(server.java:19)

0 个答案:

没有答案