0

引导我完成这个例外。我一直在尝试将文件从客户端发送到服务器 - 在客户端手动输入文件的名称。但是我在客户端收到 NullPointerException - 据我所知,可能的错误是“在打开文件之前,我传递了一个空参数,因此是 NPE”

服务器.java

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

public class Server extends Thread {
    public static final int PORT = 3333;
    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(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);

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

客户端.java

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.*;

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 :");
        }
        File file = new File(fileName);
        Socket socket = new Socket("localhost", 3333);
        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();
    }
}  
4

3 回答 3

2
String fileName = null;

       try {
            fileName = args[0];
        } catch (Exception e) {
            System.out.println("Enter the name of the file :");
        }
        File file = new File(fileName);

上面的代码只是检查用户是否提供了文件名。即使用户没有输入任何文件名,它也会继续进行,结果null将作为参数传递给File构造函数。因此你的NPE.

也将File实例化放在一个try catch块中。其次,File构造函数将文件的整个路径作为参数,因此请确保您正在使用的文件在当前工作目录中。

于 2012-08-20T10:00:13.093 回答
1

你的代码

String fileName = null


try {
    fileName = args[0];
 } catch (Exception e) {
    System.out.println("Enter the name of the file :");
 }

有问题。假设如果用户没有输入文件名,它会忽略“输入文件名:”的异常。因此,您的文件名将始终为 NULL。

您要么尝试将其修改为

try {
    fileName = args[0];
 } catch (Exception e) {
    System.out.println("Usage: java Client <file_name>");
    System.exit(0);
 }

或者

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

无论您要使用哪种方法,请检查 NULL 之类的

if (fileName == null) {
  System.out.println("File name can not be NULL");
  System.exit(0);
}

这应该做。干杯。

于 2012-08-20T10:07:31.887 回答
1

1.首先最好使用Scanner随路走的trim()方法,这样可以防止路径中的任何意外增加空间。

Scanner scan = new Scanner(System.in);
String tmppath = scan.nextLine();
String path = tmppath.trim();

2.但如果你想走自己的路......那就试试这个..

File f = null;   // If its in class scope, then 
                 // no need to initialize it to null, bydefault it will be null.

 try{

     f = args[0];


 }catch(Exception ex){

     System.out.println("file object is null");
}
于 2012-08-20T10:08:32.393 回答