String result = "上传处不成功";
     if(list != null){
            FTPClient ftpClient = new FTPClient(); 
            FileInputStream fis = null; 
            int reply = 0;
            try {          
                ftpClient.connect(loginInfo.getIP()); 
                ftpClient.login(loginInfo.getAccount(), loginInfo.getPassword()); 
                reply = ftpClient.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.out.println("FTP服务器 拒绝连接");
log.info("FTP服务器 拒绝连接");
return "FTP服务器 拒绝连接";
}                 
                String srcFilePath = CircuitExcel(loginInfo.getDestPath(), list, FileName, loginInfo.getExcelPath());
                String fileName = FileName.replace('/', '_')+".xls";
                File srcFile = new File(srcFilePath + fileName); 
                fis = new FileInputStream(srcFile); 
                //设置上传目录 
                ftpClient.changeWorkingDirectory(loginInfo.getDestPath()); 
                ftpClient.setBufferSize(1024); 
                ftpClient.setControlEncoding("GBK"); 
                //设置文件类型(二进制) 
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
                ftpClient.enterLocalPassiveMode();
                ftpClient.storeFile(FileName+".xls", fis);  //--生成EXCEL文件
                result = "上传处成功";
            } catch (IOException e) {
             result = "FTP客户端出错!";
                e.printStackTrace(); 
                throw new RuntimeException("FTP客户端出错!", e); 
            } finally { 
                IOUtils.closeQuietly(fis); 
                try { 
                    ftpClient.disconnect(); 
                } catch (IOException e) { 
                 result = "关闭FTP连接发生异常!";
                    e.printStackTrace(); 
                    throw new RuntimeException("关闭FTP连接发生异常!", e); 
                } 
            }     
     }
     return result;ftpClient.connect(loginInfo.getIP()); 这里报错。已经肯定ip格式正确127.0.0.1本机测试的是XP系统, 有安装IIS, 读写等权限都有, 有设置开机密码, 本机电脑没杀毒软件防火墙等【裸奔】。 情景是这样的, 做一个WEBSERVICE接口,通过单号, 查询数据库【这里没错】, 然后生成EXCEL【这里也没错】, 上传到指定的FTP服务器上【这里错了】。异常如下:
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:267)
at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:335)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:550)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:163)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:250)
at com.catt.webservice.inBsBillDispatchSysInterface.service.InBsBillDsptSysService.Upload(InBsBillDsptSysService.java:100)
at com.catt.webservice.inBsBillDispatchSysInterface.service.InBsBillDsptSysService.getCircuit(InBsBillDsptSysService.java:50)
at com.catt.webservice.inBsBillDispatchSysInterface.inBsBillDsptSys.getCircuit(inBsBillDsptSys.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:100)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:176)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:524)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

解决方案 »

  1.   

    不知道你用客户端工具或者不通过程序能连接上FTP服务器吗?
      

  2.   

    FTPClient client = new FTPClient();
    client.connect(InetAddress.getLocalHost());
    client.login("Test", "1234");
    System.out.println(client.getReplyCode());我这样连我本机的没有问题。你把连接的那个变量换成我那种形式,然后测试下通不通。
      

  3.   

    我现在用的  没有问题的代码public boolean login(String host, int port, String user, String pwd)
    throws Exception {
    boolean flag = false;
    try {
    client.setControlEncoding("GBK");// 设置登陆编码格式
    client.setConnectTimeout(60000);// 超时60秒
    client.connect(host.trim(), port);// 连接FTP
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
    this.close();
    throw new Exception("FTP  refuse connect");
    }
    } catch (SocketException e) {
    log.error(e);
    throw new SocketException("FTP connect timeout");
    } catch (IOException e) {
    log.error(e);
    throw new IOException("hostname/IP error");
    }
    ...... }
      

  4.   

    FTPClient ftpClient = new FTPClient("127.0.0.1",port); 
      

  5.   

    client.setConnectTimeout(60000);// 超时60秒我只是没有设置超时, 所以本机没错, 别的FTP就异常了。^_^
      

  6.   

    不对吧,不设置超时是一直尝试连接不会超时的,你最好在连接的时候指定port。否则使用默认port可能会不成功。