【问题标题】:Error in uploading file to server java.lang.nullpointerexception connection is null exception将文件上传到服务器时出错 java.lang.nullpointerexception 连接为空异常
【发布时间】:2015-10-26 14:11:39
【问题描述】:

我正在开发一个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.");
            }
        }

然后另一个异常是socket write error。

如何解决?

【问题讨论】:

    标签: java database server ftp-server


    【解决方案1】:

    您不应该对 hostDir 字符串进行反转义。

    如果您省略该行,代码应该可以工作。

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

    【讨论】:

    • 如果我逃避该步骤,则该步骤不起作用,因为这是存储文件的服务器文件路径 软件导致连接中止:套接字写入错误 软件导致连接中止:recv 失败,我做了一些更改进入我的代码,所以现在他们将显示 java.net.SocketException: Connection reset by peer: socket write error
    猜你喜欢
    • 2018-04-27
    • 2014-10-10
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2017-06-17
    • 2014-01-07
    相关资源
    最近更新 更多