如题,服务器端拒绝被动模式上传。求高手给解决方案,顺便说下注意事项,谢谢。

解决方案 »

  1.   

    拒绝被动模式,就只能主动模式了。不过主动模式,只能在同一个局域网中才能使用。给段代码参考下,
     public boolean download(String remoteFile, String localFile) {
      HttpServletRequest request = ServletActionContext.getRequest();
      HttpSession session = request.getSession();
      boolean flag = false;
      try {
       boolean flag2 = connectServer();
       if (flag2) {
                    ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
                    ftpClient.enterLocalActiveMode();   //这句重要,不行换enterRemoteActiveMode 看看
                    ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); 
        
        ftpClient.setBufferSize(1024);
        ftpClient.setControlEncoding("UTF-8");
        FTPClientConfig conf = new FTPClientConfig(
          FTPClientConfig.SYST_NT);
        conf.setServerLanguageCode("zh");    File file_in = new File(localFile);
        FileOutputStream fos = new FileOutputStream(file_in);
        flag = ftpClient.retrieveFile(directory + remoteFile, fos);
        ftpClient.disconnect();
       }
      } catch (IOException ex) {
       // flag = false;
       System.out.println("not download");
       System.out.println(ex);
      } finally {
       try {
        ftpClient.disconnect();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
      return flag;
     }
      

  2.   

    ftp.enterLocalActiveMode()这个可以设置被动模式上传。
    看看这个例子:public DownloadStatus download(String remote,String local) throws IOException{       
            //设置被动模式       
            ftpClient.enterLocalPassiveMode();       
            //设置以二进制方式传输       
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);       
            DownloadStatus result;       
                   
            //检查远程文件是否存在       
            FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));       
            if(files.length != 1){       
                System.out.println("远程文件不存在");       
                return DownloadStatus.Remote_File_Noexist;       
            }       
                   
            long lRemoteSize = files[0].getSize();       
            File f = new File(local);       
            //本地存在文件,进行断点下载       
            if(f.exists()){       
                long localSize = f.length();       
                //判断本地文件大小是否大于远程文件大小       
                if(localSize >= lRemoteSize){       
                    System.out.println("本地文件大于远程文件,下载中止");       
                    return DownloadStatus.Local_Bigger_Remote;       
                }       
                       
                //进行断点续传,并记录状态       
                FileOutputStream out = new FileOutputStream(f,true);       
                ftpClient.setRestartOffset(localSize);       
                InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));       
                byte[] bytes = new byte[1024];       
                long step = lRemoteSize /100;       
                long process=localSize /step;       
                int c;       
                while((c = in.read(bytes))!= -1){       
                    out.write(bytes,0,c);       
                    localSize+=c;       
                    long nowProcess = localSize /step;       
                    if(nowProcess > process){       
                        process = nowProcess;       
                        if(process % 10 == 0)       
                            System.out.println("下载进度:"+process);       
                        //TODO 更新文件下载进度,值存放在process变量中       
                    }       
                }       
                in.close();       
                out.close();       
                boolean isDo = ftpClient.completePendingCommand();       
                if(isDo){       
                    result = DownloadStatus.Download_From_Break_Success;       
                }else {       
                    result = DownloadStatus.Download_From_Break_Failed;       
                }       
            }else {       
                OutputStream out = new FileOutputStream(f);       
                InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));       
                byte[] bytes = new byte[1024];       
                long step = lRemoteSize /100;       
                long process=0;       
                long localSize = 0L;       
                int c;       
                while((c = in.read(bytes))!= -1){       
                    out.write(bytes, 0, c);       
                    localSize+=c;       
                    long nowProcess = localSize /step;       
                    if(nowProcess > process){       
                        process = nowProcess;       
                        if(process % 10 == 0)       
                            System.out.println("下载进度:"+process);       
                        //TODO 更新文件下载进度,值存放在process变量中       
                    }       
                }       
                in.close();       
                out.close();       
                boolean upNewStatus = ftpClient.completePendingCommand();       
                if(upNewStatus){       
                    result = DownloadStatus.Download_New_Success;       
                }else {       
                    result = DownloadStatus.Download_New_Failed;       
                }       
            }       
            return result;       
        }       
      

  3.   

    //设置被动模式       
    ftpClient.enterLocalPassiveMode();