将文件上载到服务器java.lang.nullpointerexception连接时出错是null异常

时间:2015-08-04 08:52:31

标签: java database server ftp-server

我正在处理java申请。当我向服务器发送文件时,我收到异常。错误是:Error in connection =java.lang.NullPointerException

代码如下:

 File file = new File(localFileFullName);
        if (!(file.isDirectory())) {
            if (file.exists()) {
                FileInputStream input = null;
                FileInputStream input1 = null;
                try {
                    input = new FileInputStream(new File(localFileFullName));
                    if (input != null) {

                        hostDir = hostDir.replaceAll("//", "/");
                       logger.info("uploading host dir : " + hostDir);


                       //new 
                       FTPClient ftpClient= DBConnection.connect();  
                       String secondRemoteFile = hostDir;
                       logger.error("connect to the server"+ftpClient.isConnected());

                       System.out.println("Start uploading second file");
                       OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
                       byte[] bytesIn = new byte[4096];
                       int read = 0;

                       while ((read = input.read(bytesIn)) != -1) {
                           outputStream.write(bytesIn, 0, read);
                       }
                       input.close();
                       outputStream.close();

                       boolean completed = ftpClient.completePendingCommand();
                       if (completed) {
                           System.out.println("The second file is uploaded successfully.");
                       }
     ftpClient.disconnect();
 } else {
                         logger.error("uploading file input null.");
             }

                }catch(CopyStreamException cs)
                {
                    logger.error("Copy StreamExeption is come "+cs);

                } catch(Exception ex)
                {
                     logger.error("Error in connection ="+ex);



                }finally {
//                  boolean disconnect= DBConnection.disConnect();
                    input.close();

//                  logger.info("Connection close from DBConnection"+disconnect);
                }

            } else {
                  logger.info("uploading file is not exists.");
            }
        }

然后另一个例外是套接字写入错误。

如何解决?

1 个答案:

答案 0 :(得分:0)

您不应该解除hostDir字符串的转义。

如果省略该行代码应该有效。

另一个可能的错误是您的连接失败, 检查我们是否需要有关异常的更多信息