使用java套接字进行文件传输:客户端出错“线程中的异常”main“java.lang.NullPointerException”

时间:2012-08-20 09:52:50

标签: java sockets nullpointerexception

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

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

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

3 个答案:

答案 0 :(得分: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构造函数将文件的整个路径作为参数接收,因此请确保您使用的文件位于当前工作目录中。

答案 1 :(得分:1)

您的代码

String fileName = null


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

遇到了问题。假设用户没有输入文件名,它会忽略“输入文件名:”的异常。所以你的fileName总是为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);
}

这应该做。欢呼声。

答案 2 :(得分: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");
}