Esempio n. 1
11
  /**
   * Description: 从FTP服务器下载指定文件夹下的所有文件
   *
   * @param url FTP服务器hostname
   * @param port FTP服务器端口
   * @param username FTP登录账号
   * @param password FTP登录密码
   * @param remotePath FTP服务器上的相对路径
   * @param localPath 下载后保存到本地的路径
   * @param fileName 保存为该文件名的文件
   * @return
   */
  public static boolean downFile(
      String url,
      int port,
      String username,
      String password,
      String remotePath,
      String localPath,
      String fileName) {
    boolean success = false;
    FTPClient ftp = null;
    try {
      int reply;
      ftp = getConnection(url, port, username, password);
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return success;
      }
      boolean changeWorkDirState = false;
      changeWorkDirState = ftp.changeWorkingDirectory(remotePath); // 转移到FTP服务器目录
      System.out.println(changeWorkDirState == true ? "切换FTP目录 成功" : "切换FTP目录 失败");
      if (changeWorkDirState == false) return false;
      FTPFile[] fs = ftp.listFiles();
      for (FTPFile ff : fs) {
        if (ff.getName().equals(fileName)) {
          File localFile = new File(localPath + "/" + ff.getName());

          OutputStream is = new FileOutputStream(localFile);
          ftp.retrieveFile(ff.getName(), is);
          is.close();
        }
      }

      ftp.logout();
      success = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException ioe) {
        }
      }
    }
    return success;
  }
Esempio n. 2
0
 public Object makeObject() throws Exception {
   FTPClient client = new FTPClient();
   try {
     if (uri.getPort() > 0) {
       client.connect(uri.getHost(), uri.getPort());
     } else {
       client.connect(uri.getHost());
     }
     if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
       throw new IOException("Ftp error: " + client.getReplyCode());
     }
     if (!client.login(uri.getUsername(), uri.getPassword())) {
       throw new IOException("Ftp error: " + client.getReplyCode());
     }
     if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
       throw new IOException("Ftp error. Couldn't set BINARY transfer type.");
     }
   } catch (Exception e) {
     if (client.isConnected()) {
       client.disconnect();
     }
     throw e;
   }
   return client;
 }
Esempio n. 3
0
  /**
   * Opens a new connection and performs login with user name and password if set.
   *
   * @throws IOException
   */
  protected void connectAndLogin() throws IOException {
    if (!ftpClient.isConnected()) {
      ftpClient.connect(getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort());

      log.info("Connected to FTP server: " + ftpClient.getReplyString());

      int reply = ftpClient.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {
        throw new CitrusRuntimeException("FTP server refused connection.");
      }

      log.info("Successfully opened connection to FTP server");

      if (getEndpointConfiguration().getUser() != null) {
        log.info(String.format("Login as user: '******'", getEndpointConfiguration().getUser()));
        boolean login =
            ftpClient.login(
                getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword());

        if (!login) {
          throw new CitrusRuntimeException(
              String.format(
                  "Failed to login to FTP server using credentials: %s:%s",
                  getEndpointConfiguration().getUser(), getEndpointConfiguration().getPassword()));
        }
      }
    }
  }
  public static FTPClient getFTPClient() {
    FTPClient client = ftpClientThreadLocal.get();
    if (client != null && client.isConnected()) {
      return client;
    }
    ftpClientThreadLocal.remove();
    FTPClient ftpClient = new FTPClient(); // 创建ftpClient
    ftpClient.setControlEncoding("UTF-8"); // 设置字符编码
    Boolean isConnect = connectFtp(ftpClient);

    ftpClient.enterLocalPassiveMode();
    try {
      ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
      ftpClient.setSoTimeout(1000 * 30);
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 得到返回答复码
    int reply = ftpClient.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
      try {
        ftpClient.disconnect();
      } catch (IOException e) {
        e.printStackTrace();
      }

    } else {
      ftpClientThreadLocal.set(ftpClient);
    }
    return ftpClient;
  }
  /**
   * @param path ex:/upload/2023
   * @param filename xxx.jpg
   * @param input 输入流
   * @return
   */
  public static boolean uploadFile(String path, String filename, InputStream input) {
    boolean success = false;
    FTPClient ftpClient = null;
    try {
      ftpClient = getFTPClient();
      int reply = ftpClient.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {

        return success;
      }
      // 使用二进制上传
      ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
      ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
      ftpClient.enterLocalPassiveMode();
      ftpCreateDirectoryTree(ftpClient, path);
      success = ftpClient.storeFile(filename, input);
      input.close();
      ftpClient.logout();

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (null != ftpClient && ftpClient.isConnected()) {
        try {
          ftpClient.disconnect();
        } catch (IOException ioe) {
        }
      }
    }
    return success;
  }
  /**
   * Function retrieved file on the remote server NOTE: This function closes output stream after
   * retrieving file content
   *
   * @param fileName
   * @param fileOutput
   * @return
   * @throws SocketException
   * @throws IOException
   */
  public boolean retrieveFile(String fileName, OutputStream fileOutput)
      throws SocketException, IOException {
    boolean fileRetrieved = false;

    connect2Server();

    if (oFtp.isConnected()) {

      try {

        fileRetrieved = oFtp.retrieveFile(fileName, fileOutput);

        if (fileRetrieved) {
          logMessage("File '" + fileName + "' retrieved");
        } else {
          logError(
              "Can't retrieve file "
                  + fileName
                  + " FTP reply code: "
                  + oFtp.getReplyCode()
                  + " Ftp message: "
                  + oFtp.getReplyString());
        }
        fileOutput.close();
      } catch (IOException ioe) {
        log.warning("FTPUtil - retrieveFile(): " + ioe.toString());
        return false;
      }
    }
    this.closeFtpConnection();
    return fileRetrieved;
  }
Esempio n. 7
0
 /**
  * Description: 向FTP服务器上传文件
  *
  * @param host FTP服务器hostname
  * @param port FTP服务器端口
  * @param username FTP登录账号
  * @param password FTP登录密码
  * @param basePath FTP服务器基础目录
  * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
  * @param filename 上传到FTP服务器上的文件名
  * @param input 输入流
  * @return 成功返回true,否则返回false
  */
 public static boolean uploadFile(
     String host,
     int port,
     String username,
     String password,
     String basePath,
     String filePath,
     String filename,
     InputStream input) {
   boolean result = false;
   FTPClient ftp = new FTPClient();
   try {
     int reply;
     ftp.connect(host, port); // 连接FTP服务器
     // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
     ftp.login(username, password); // 登录
     reply = ftp.getReplyCode();
     if (!FTPReply.isPositiveCompletion(reply)) {
       ftp.disconnect();
       return result;
     }
     // 切换到上传目录
     if (!ftp.changeWorkingDirectory(basePath + filePath)) {
       // 如果目录不存在创建目录
       String[] dirs = filePath.split("/");
       String tempPath = basePath;
       for (String dir : dirs) {
         if (null == dir || "".equals(dir)) continue;
         tempPath += "/" + dir;
         if (!ftp.changeWorkingDirectory(tempPath)) {
           if (!ftp.makeDirectory(tempPath)) {
             return result;
           } else {
             ftp.changeWorkingDirectory(tempPath);
           }
         }
       }
     }
     // 设置上传文件的类型为二进制类型
     ftp.setFileType(FTP.BINARY_FILE_TYPE);
     // 上传文件
     if (!ftp.storeFile(filename, input)) {
       return result;
     }
     input.close();
     ftp.logout();
     result = true;
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (ftp.isConnected()) {
       try {
         ftp.disconnect();
       } catch (IOException ioe) {
       }
     }
   }
   return result;
 }
 private synchronized void closeFtpConnection() throws IOException {
   if (oFtp.isConnected()) {
     try {
       oFtp.logout();
     } catch (org.apache.commons.net.ftp.FTPConnectionClosedException e) {
     }
   }
   oFtp.disconnect();
 }
Esempio n. 9
0
 public void releaseFtp(UMOEndpointURI uri, FTPClient client) throws Exception {
   if (isCreateDispatcherPerRequest()) {
     destroyFtp(uri, client);
   } else {
     if (client != null && client.isConnected()) {
       ObjectPool pool = getFtpPool(uri);
       pool.returnObject(client);
     }
   }
 }
Esempio n. 10
0
 /** 서버와의 연결을 끊는다. */
 public void disconnection() {
   try {
     client.logout();
     if (client.isConnected()) {
       client.disconnect();
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Esempio n. 11
0
  @Override
  public void upload(UploadRequest uploadRequest) {
    ServerInfo serverInfo = uploadRequest.getServerInfo();
    String host = serverInfo.getHost();

    if (uploadRequest.isUseProxy()) {
      initializeProxy(host);
    }
    int attemptCount = 0;
    while (true) {
      try {

        login(serverInfo.getHost(), serverInfo.getLogin(), serverInfo.getPassword());

        try {
          ftpClient.setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
        } catch (SocketException e) {
          LOGGER.error("socket exception: {} {}", e.getMessage(), e.getStackTrace());

          throw new RuntimeException("socket exception: " + e.getMessage());
        }

        String prefixDirectory = serverInfo.getPrefix();
        if (prefixDirectory != null) {
          ftpClient.changeWorkingDirectory(prefixDirectory);
        }

        File directory = new File(uploadRequest.getUploadDir());
        uploadDir(directory);

        ftpClient.logout();
        break;

      } catch (IOException e) {
        LOGGER.error("i/o error: {}, retrying", e.getMessage());

        attemptCount++;

        if (attemptCount > 5) {
          LOGGER.debug("choosing another proxy after 5 attempts");
          initializeProxy(host);

          attemptCount = 0;
        }
      } finally {
        if (ftpClient.isConnected()) {
          try {
            ftpClient.disconnect();
          } catch (IOException ioe) {
            // do nothing
          }
        }
      }
    }
  }
Esempio n. 12
0
 /**
  * Logout and disconnect the given FTPClient. *
  *
  * @param client
  * @throws IOException
  */
 private void disconnect(FTPClient client) throws IOException {
   if (client != null) {
     if (!client.isConnected()) {
       throw new FTPException("Client not connected");
     }
     boolean logoutSuccess = client.logout();
     client.disconnect();
     if (!logoutSuccess) {
       LOG.warn("Logout failed while disconnecting, error code - " + client.getReplyCode());
     }
   }
 }
Esempio n. 13
0
  public boolean test() {

    int base = 0;
    boolean error = false;
    FTPFile[] ftpFiles;

    testMsg = null;

    try {
      int reply;
      ftpClient.connect(ftpVO.getServer(), ftpVO.getPortasInteger());

      // Check connection
      reply = ftpClient.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        testMsg = Resource.getString("ftpchooser.msg.noconnect");
        return false;
      }

      // Login
      if (!ftpClient.login(ftpVO.getUser(), ftpVO.getPassword())) {
        ftpClient.logout();
        testMsg = Resource.getString("ftpchooser.msg.nologin");
        return false;
      }

      ftpClient.syst();

      // Change directory
      if (!ftpClient.changeWorkingDirectory(ftpVO.getDirectory())) {
        testMsg = Resource.getString("ftpchooser.msg.nodirectory");
        return false;
      }

      testMsg = Resource.getString("ftpchooser.msg.success");
      ftpClient.logout();

    } catch (IOException ex) {
      testMsg = ex.getLocalizedMessage();
      error = true;
    } finally {
      if (ftpClient.isConnected()) {
        try {
          ftpClient.disconnect();
        } catch (IOException f) {
        }
      }
    }
    return !error;
  }
Esempio n. 14
0
  @Override
  public void destroy() throws Exception {
    if (ftpClient.isConnected()) {
      ftpClient.logout();

      try {
        ftpClient.disconnect();
      } catch (IOException e) {
        log.warn("Failed to disconnect from FTP server", e);
      }

      log.info("Successfully closed connection to FTP server");
    }
  }
Esempio n. 15
0
  /**
   * @param remotePath 远程文件路径ַ ex:/upload/2012/xxxx.jpg
   * @param out 文件输出流
   * @return
   */
  public static boolean downFile(String remotePath, OutputStream out) {
    Boolean flag = Boolean.FALSE;
    // 得到文件名 ex: xxxx.jpg
    String fileName = getLastName(remotePath);
    // 得到文件存储路径 ex:/upload/2012
    String remoteStorePath = getFilePath(remotePath);
    FTPClient ftpClient = null;
    try {
      ftpClient = getFTPClient();
      // 得到返回答复码
      int reply = ftpClient.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {
        try {
          ftpClient.disconnect();
          return flag;
        } catch (IOException e) {
          e.printStackTrace();
          return flag;
        }
      }

      ftpClient.changeWorkingDirectory(remoteStorePath);

      // ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//

      FTPFile[] fs = ftpClient.listFiles();
      for (FTPFile file : fs) {
        if (fileName.equalsIgnoreCase(file.getName())) {
          flag = ftpClient.retrieveFile(fileName, out);

          break;
        }
      }
      ftpClient.logout();
    } catch (IOException e) {
      e.printStackTrace();

    } finally {
      if (null != ftpClient && ftpClient.isConnected()) {
        try {
          ftpClient.disconnect();
        } catch (IOException ioe) {
        }
      }
    }

    return flag;
  }
Esempio n. 16
0
 /** Disconnects. */
 private void disconnectOnFailure() {
   try {
     if (ftp.isConnected()) {
       ftp.disconnect();
     }
   } catch (IOException e) {
     /*
      * This exception is not re thrown, because
      * this method is used only by exception handling
      * blocks.
      */
     logger.error(e.toString());
   } finally {
     ftp = null;
   }
 }
Esempio n. 17
0
  public boolean open() {
    boolean isSuccessful = false;

    if (isOpen) {
      throw new IllegalStateException("Is already open, must be closed before!");
    }

    try {
      int reply;
      ftpClient.connect(ftpVO.getServer(), ftpVO.getPortasInteger());

      reply = ftpClient.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        throw new IOException("Can't connect!");
      }

      if (!ftpClient.login(ftpVO.getUser(), ftpVO.getPassword())) {
        ftpClient.logout();
        throw new IOException("Can't login!");
      }

      if (!ftpClient.changeWorkingDirectory(ftpVO.getDirectory())) {
        ftpClient.logout();
        throw new IOException("Can't change directory!");
      }

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
      ftpClient.enterLocalPassiveMode();

      isSuccessful = true;

    } catch (Exception e) {
      if (ftpClient.isConnected()) {
        try {
          ftpClient.disconnect();
        } catch (IOException f) {
          // do nothing
        }
      }
      isSuccessful = false;
    }
    isOpen = isSuccessful;
    return isSuccessful;
  }
Esempio n. 18
0
  /**
   * Description: 从FTP服务器下载文件
   *
   * @param host FTP服务器hostname
   * @param port FTP服务器端口
   * @param username FTP登录账号
   * @param password FTP登录密码
   * @param remotePath FTP服务器上的相对路径
   * @param fileName 要下载的文件名
   * @param localPath 下载后保存到本地的路径
   * @return
   */
  public static boolean downloadFile(
      String host,
      int port,
      String username,
      String password,
      String remotePath,
      String fileName,
      String localPath) {
    boolean result = false;
    FTPClient ftp = new FTPClient();
    try {
      int reply;
      ftp.connect(host, port);
      // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
      ftp.login(username, password); // 登录
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return result;
      }
      ftp.changeWorkingDirectory(remotePath); // 转移到FTP服务器目录
      FTPFile[] fs = ftp.listFiles();
      for (FTPFile ff : fs) {
        if (ff.getName().equals(fileName)) {
          File localFile = new File(localPath + "/" + ff.getName());

          OutputStream is = new FileOutputStream(localFile);
          ftp.retrieveFile(ff.getName(), is);
          is.close();
        }
      }

      ftp.logout();
      result = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException ioe) {
        }
      }
    }
    return result;
  }
Esempio n. 19
0
  private static void sendMockFTPScannedImg() throws IOException {
    log.info(String.format("ftp is connected at %s: %s", ftpClient.isConnected(), "IP_FTP_CLIENT"));

    String filename = dateFormat.format(new Date()).replace(' ', '_').replace(':', '_');
    String remote = String.format("%s/%s.jpg", CLIENT_IMG_DIR, filename);

    BufferedInputStream bis = null;
    try {
      bis = new BufferedInputStream(new FileInputStream("test.jpg"));
      ftpClient.storeFile(remote, bis);
      log.info(String.format("Sending file as %s", remote));
      bis.close();

    } catch (FileNotFoundException e) {
      log.info("couldn't find file");
    }
  }
Esempio n. 20
0
  /**
   * Description: 从FTP服务器下载单个文件
   *
   * @param url FTP服务器hostname
   * @param port FTP服务器端口
   * @param username FTP登录账号
   * @param password FTP登录密码
   * @param remotePath FTP服务器上的相对路径+文件名 example:/imagename.jpg
   * @param localPath 下载后保存到本地的路径
   * @param fileName 保存为该文件名的文件
   * @return
   */
  public static boolean downSingleFile(
      String url,
      int port,
      String username,
      String password,
      String remotePath,
      String localPath,
      String fileName) {
    boolean success = false;
    FTPClient ftp = null;
    FileOutputStream fos = null;
    try {
      int reply;
      ftp = getConnection(url, port, username, password);
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return success;
      }
      boolean changeWorkDirState = false;
      fos = new FileOutputStream(localPath + "/" + fileName);

      ftp.setBufferSize(1024);
      // 设置文件类型(二进制)
      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      ftp.retrieveFile(remotePath, fos);
      changeWorkDirState = ftp.changeWorkingDirectory(remotePath); // 转移到FTP服务器目录

      ftp.logout();
      success = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException ioe) {
        }
      }
    }
    return success;
  }
Esempio n. 21
0
  public static void main(String[] args) {
    String server = "192.168.1.72";
    int port = 21;
    String user = "******";
    String pass = "******";

    FTPClient ftpClient = new FTPClient();
    try {

      ftpClient.connect(server, port);
      ftpClient.login(user, pass);
      ftpClient.enterLocalPassiveMode();

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

      // APPROACH #1: uploads first file using an InputStream
      File firstLocalFile = new File("D:/Shridhar/FolderTest/ItmtuResponsestatus_2012_1_18_15");

      String firstRemoteFile = "/home/ftp/";
      InputStream inputStream = new FileInputStream(firstLocalFile);

      System.out.println("Start uploading first file");
      boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
      inputStream.close();
      if (done) {
        System.out.println("The first file is uploaded successfully.");
      }

    } catch (IOException ex) {
      System.out.println("Error: " + ex.getMessage());
      ex.printStackTrace();
    } finally {
      try {
        if (ftpClient.isConnected()) {
          // ftpClient.logout();
          ftpClient.disconnect();
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
  /**
   * @param mask
   * @param remoteFolder
   * @throws SocketException
   * @throws IOException
   */
  public void removeFilesByMask(String mask, String remoteFolder)
      throws SocketException, IOException {
    connect2Server();

    if (oFtp.isConnected()) {
      FTPFile[] fileslist = oFtp.listFiles(remoteFolder);

      Pattern pat = Pattern.compile("^" + mask);

      for (FTPFile file : fileslist) {
        java.util.regex.Matcher m = pat.matcher(file.getName());
        boolean is_pat_file = m.find();

        if (is_pat_file) {
          oFtp.deleteFile(remoteFolder + file.getName());
        }
      }
    }
    this.closeFtpConnection();
  }
Esempio n. 23
0
 public static void append(
     String server, String username, String password, String remotePath, InputStream is)
     throws IOException {
   FTPClient mFtp = new FTPClient();
   try {
     mFtp.connect(server, FTP.DEFAULT_PORT);
     mFtp.login(username, password);
     mFtp.setFileType(FTP.BINARY_FILE_TYPE);
     mFtp.enterLocalPassiveMode();
     mFtp.appendFile(remotePath, is);
   } finally {
     try {
       if (mFtp.isConnected()) {
         mFtp.logout();
         mFtp.disconnect();
       }
     } catch (IOException ex) {
       ex.printStackTrace();
     }
   }
 }
  /**
   * @param mask
   * @param path
   * @return
   * @throws SocketException
   * @throws IOException
   */
  public List<FTPFile> getFilesByMask(String mask, String path)
      throws SocketException, IOException {
    List<FTPFile> maskFiles = new ArrayList<FTPFile>();
    connect2Server();

    if (oFtp.isConnected()) {
      FTPFile[] fileslist = oFtp.listFiles(path);

      Pattern pat = Pattern.compile("^" + mask);

      for (FTPFile file : fileslist) {
        java.util.regex.Matcher m = pat.matcher(file.getName());
        boolean is_pat_file = m.find();

        if (is_pat_file) {
          maskFiles.add(file);
        }
      }
    }
    this.closeFtpConnection();
    return maskFiles;
  }
  /**
   * Delete file from server
   *
   * @param filename
   * @param ftpFolder
   * @throws SocketException
   * @throws IOException
   */
  public void removeFile(String filename, String ftpFolder) throws SocketException, IOException {
    connect2Server();

    if (oFtp.isConnected()) {
      String file = ftpFolder + filename;

      if (this.isExists(filename, ftpFolder)) {
        if (!oFtp.deleteFile(file)) {
          logError(
              "Can't delete file '"
                  + filename
                  + "' from "
                  + ftpFolder
                  + " FTP reply code: "
                  + oFtp.getReplyCode()
                  + " Ftp message: "
                  + Arrays.asList(oFtp.getReplyStrings()));
        } else {
          logMessage("File '" + file + "' removed from server");
        }
      }
    }
    this.closeFtpConnection();
  }
Esempio n. 26
0
  /**
   * Description: 向FTP服务器上传文件
   *
   * @param url FTP服务器
   * @param port FTP服务器端口
   * @param username FTP登录账号
   * @param password FTP登录密码
   * @param path FTP服务器保存目录
   * @param filename 上传到FTP服务器上的文件名
   * @param input 输入流
   * @return 成功返回true,否则返回false
   */
  public static boolean uploadFile(
      String url,
      int port,
      String username,
      String password,
      String path,
      String filename,
      InputStream input) {
    boolean success = false;
    FTPClient ftp = null;
    try {
      int reply;
      ftp = getConnection(url, port, username, password);
      reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return success;
      }
      ftp.changeWorkingDirectory(path);
      ftp.storeFile(filename, input);

      input.close();
      ftp.logout();
      success = true;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException ioe) {
        }
      }
    }
    return success;
  }
  /**
   * The method that handles file uploading, this method takes the absolute file path of a local
   * file to be uploaded to the remote FTP server, and the remote file will then be transfered to
   * the FTP server and saved as the relative path name specified in method setRemoteFile
   *
   * @param localfilename – the local absolute file name of the file in local hard drive that needs
   *     to FTP over
   */
  public synchronized boolean uploadFile(String localfilename, String remoteFile) {

    final BufferedInputStream bis;
    try {
      // URL url = new URL(this.getRemoteFileUrl(remoteFile));
      // URLConnection urlFtpConnection = url.openConnection();

      InputStream is = new FileInputStream(localfilename);
      bis = new BufferedInputStream(is);

      connect2Server();

      if (oFtp.isConnected()) {
        String fileExtension = localfilename.substring(localfilename.lastIndexOf(".") + 1);

        String[] fileExtensions = {"png", "gif", "bmp", "jpg", "jpeg", "tiff"};

        // If exporting to .csv or .tsv, add the gid parameter to specify which sheet to export
        if (Arrays.asList(fileExtensions).contains(fileExtension)) {
          oFtp.setFileType(FTP.BINARY_FILE_TYPE);
        }

        // OutputStream os = oFtp.storeFileStream(remoteFile);
        // if (os == null)
        // {
        // logError(oFtp.getReplyString());
        // }
        // BufferedOutputStream bos = new BufferedOutputStream(os);
        // byte[] buffer = new byte[1024];
        // int readCount;
        //
        // while ((readCount = bis.read(buffer)) > 0)
        // {
        // bos.write(buffer);
        // // bos.flush();
        // }
        // bos.close();

        if (!oFtp.storeFile(remoteFile, bis)) {
          logError(
              "Can't upload file "
                  + localfilename
                  + " FTP reply code: "
                  + oFtp.getReplyCode()
                  + " Ftp message: "
                  + oFtp.getReplyString());

          bis.close();
          is.close();

          this.closeFtpConnection();
          return false;
        } else {
          logMessage("File `" + localfilename + "` is uploaded!");
        }
      }

      /*
       * urlFtpConnection.getOutputStream();
       * //
       * log.message("File `" + localfilename + "` is uploaded!");
       */

      bis.close();
      is.close();
      // this.closeFtpConnection();
      return true;
    } catch (Exception ex) {
      StringWriter sw0 = new StringWriter();
      PrintWriter p0 = new PrintWriter(sw0, true);
      ex.printStackTrace(p0);
      log.error(sw0.getBuffer().toString());
      log.exception(ex);

      return false;
    }
  }
Esempio n. 28
0
 public void destroyFtp(UMOEndpointURI uri, FTPClient client) throws Exception {
   if (client != null && client.isConnected()) {
     ObjectPool pool = getFtpPool(uri);
     pool.invalidateObject(client);
   }
 }
Esempio n. 29
0
 public static Map<String, Object> putFile(DispatchContext dctx, Map<String, ?> context) {
   Locale locale = (Locale) context.get("locale");
   Debug.logInfo("[putFile] starting...", module);
   InputStream localFile = null;
   try {
     localFile = new FileInputStream((String) context.get("localFilename"));
   } catch (IOException ioe) {
     Debug.logError(ioe, "[putFile] Problem opening local file", module);
     return ServiceUtil.returnError(
         UtilProperties.getMessage(resource, "CommonFtpFileCannotBeOpen", locale));
   }
   List<String> errorList = new LinkedList<String>();
   FTPClient ftp = new FTPClient();
   try {
     Integer defaultTimeout = (Integer) context.get("defaultTimeout");
     if (UtilValidate.isNotEmpty(defaultTimeout)) {
       Debug.logInfo(
           "[putFile] set default timeout to: " + defaultTimeout.intValue() + " milliseconds",
           module);
       ftp.setDefaultTimeout(defaultTimeout.intValue());
     }
     Debug.logInfo("[putFile] connecting to: " + (String) context.get("hostname"), module);
     ftp.connect((String) context.get("hostname"));
     if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
       Debug.logInfo("[putFile] Server refused connection", module);
       errorList.add(UtilProperties.getMessage(resource, "CommonFtpConnectionRefused", locale));
     } else {
       String username = (String) context.get("username");
       String password = (String) context.get("password");
       Debug.logInfo(
           "[putFile] logging in: username="******", password="******"[putFile] login failed", module);
         errorList.add(
             UtilProperties.getMessage(
                 resource,
                 "CommonFtpLoginFailure",
                 UtilMisc.toMap("username", username, "password", password),
                 locale));
       } else {
         Boolean binaryTransfer = (Boolean) context.get("binaryTransfer");
         boolean binary = (binaryTransfer == null) ? false : binaryTransfer.booleanValue();
         if (binary) {
           ftp.setFileType(FTP.BINARY_FILE_TYPE);
         }
         Boolean passiveMode = (Boolean) context.get("passiveMode");
         boolean passive = (passiveMode == null) ? true : passiveMode.booleanValue();
         if (passive) {
           ftp.enterLocalPassiveMode();
         }
         Debug.logInfo(
             "[putFile] storing local file remotely as: " + context.get("remoteFilename"), module);
         if (!ftp.storeFile((String) context.get("remoteFilename"), localFile)) {
           Debug.logInfo("[putFile] store was unsuccessful", module);
           errorList.add(
               UtilProperties.getMessage(
                   resource,
                   "CommonFtpFileNotSentSuccesfully",
                   UtilMisc.toMap("replyString", ftp.getReplyString()),
                   locale));
         } else {
           Debug.logInfo("[putFile] store was successful", module);
           List<String> siteCommands = checkList(context.get("siteCommands"), String.class);
           if (siteCommands != null) {
             for (String command : siteCommands) {
               Debug.logInfo("[putFile] sending SITE command: " + command, module);
               if (!ftp.sendSiteCommand(command)) {
                 errorList.add(
                     UtilProperties.getMessage(
                         resource,
                         "CommonFtpSiteCommandFailed",
                         UtilMisc.toMap("command", command, "replyString", ftp.getReplyString()),
                         locale));
               }
             }
           }
         }
       }
       ftp.logout();
     }
   } catch (IOException ioe) {
     Debug.logWarning(ioe, "[putFile] caught exception: " + ioe.getMessage(), module);
     errorList.add(
         UtilProperties.getMessage(
             resource,
             "CommonFtpProblemWithTransfer",
             UtilMisc.toMap("errorString", ioe.getMessage()),
             locale));
   } finally {
     try {
       if (ftp.isConnected()) {
         ftp.disconnect();
       }
     } catch (Exception e) {
       Debug.logWarning(e, "[putFile] Problem with FTP disconnect: ", module);
     }
     try {
       localFile.close();
     } catch (Exception e) {
       Debug.logWarning(e, "[putFile] Problem closing local file: ", module);
     }
   }
   if (errorList.size() > 0) {
     Debug.logError(
         "[putFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList,
         module);
     return ServiceUtil.returnError(errorList);
   }
   Debug.logInfo("[putFile] finished successfully", module);
   return ServiceUtil.returnSuccess();
 }
  @Override
  public Future<Set<File>> download(LayerRequest currentLayer) throws Exception {
    this.currentLayer = currentLayer;
    currentLayer.setMetadata(this.includesMetadata());

    File directory = getDirectory();
    Set<File> fileSet = new HashSet<File>();
    List<String> urls = this.getUrls(currentLayer);
    for (String url : urls) {
      InputStream inputStream = null;
      URL currentURL = new URL(url);
      String ftpServerAddress = currentURL.getHost();
      String currentPath = currentURL.getPath();

      FTPClient ftp = new FTPClient();
      int delimiterIndex = currentPath.lastIndexOf("/");
      String remoteDirectory = currentPath.substring(0, delimiterIndex);
      String fileName = currentPath.substring(delimiterIndex + 1);

      try {
        int reply;
        ftp.connect(ftpServerAddress);
        // Although they are open FTP servers, in order to access the files, a anonymous login is
        // necessary.
        ftp.login("anonymous", "anonymous");
        System.out.println("Connected to " + ftpServerAddress + ".");
        System.out.print(ftp.getReplyString());

        // After connection attempt, you should check the reply code to verify success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
          ftp.disconnect();
          System.err.println("FTP server refused connection.");
          System.exit(1);
        }
        reply = ftp.getReplyCode();

        // enter passive mode
        ftp.enterLocalPassiveMode();

        // change current directory
        ftp.changeWorkingDirectory(remoteDirectory);

        // check if the file with given name exists in ftp server
        try {
          FTPFile[] ftpFiles = ftp.listFiles(fileName);
          if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
              if (!file.isFile()) continue;
              System.out.println("Found file:" + file.getName());

              // transfer the file
              inputStream = ftp.retrieveFileStream(file.getName());

              // save the file and add to fileset
              // TODO: ftp file does not contain MIME type.
              File outputFile = OgpFileUtils.createNewFileFromDownload(fileName, "ZIP", directory);
              // FileUtils with a BufferedInputStream seems to be the fastest method with a small
              // sample size.  requires more testing
              BufferedInputStream bufferedIn = null;
              try {
                bufferedIn = new BufferedInputStream(inputStream);
                FileUtils.copyInputStreamToFile(bufferedIn, outputFile);
                fileSet.add(outputFile);
              } finally {
                IOUtils.closeQuietly(bufferedIn);
              }
            }
          }
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          ftp.logout();
        }
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (ftp.isConnected()) {
          try {
            ftp.disconnect();
          } catch (IOException ioe) {
            // do nothing
          }
        }
        IOUtils.closeQuietly(inputStream);
      }
    }
    return new AsyncResult<Set<File>>(fileSet);
  };