View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.vfs2.provider.ftp;
19  
20  import java.nio.charset.StandardCharsets;
21  import java.time.Instant;
22  
23  import org.apache.commons.net.PrintCommandListener;
24  import org.apache.commons.net.io.Util;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystemManager;
27  import org.apache.commons.vfs2.FileSystemOptions;
28  import org.apache.commons.vfs2.VFS;
29  
30  public class Main {
31  
32      public static void main(final String[] args) {
33          final String host = "localhost";
34          final String remoteDir = "/unicode";
35          try {
36              // System.setProperty("file.encoding", "UTF-8");
37              final FileSystemManager manager = VFS.getManager();
38              final FileSystemOptions opts = new FileSystemOptions();
39              final FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
40              builder.setAutodetectUtf8(opts, true);
41              builder.setControlEncoding(opts, StandardCharsets.UTF_8);
42              builder.setUserDirIsRoot(opts, false);
43              builder.setPassiveMode(opts, true);
44              builder.setFileType(opts, FtpFileType.BINARY);
45              final String ftpUrl = "ftp://" + host + remoteDir;
46              try (FileObject remoteFolder = manager.resolveFile(ftpUrl, opts)) {
47                  final FtpFileObject ftpFileObject = (FtpFileObject) remoteFolder;
48                  final FtpFileSystem ftpFileSystem = (FtpFileSystem) ftpFileObject.getFileSystem();
49                  final FTPClientWrapper clientW = (FTPClientWrapper) ftpFileSystem.getClient();
50                  clientW.getFtpClient().addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
51                  clientW.getFtpClient().syst();
52                  clientW.getFtpClient().setFileType(0);
53                  clientW.sendOptions("UTF8", "ON");
54                  System.out.printf("%s - Files in %s:%n", Instant.now(), remoteDir);
55                  final FileObject[] files = remoteFolder.getChildren();
56                  for (final FileObject file : files) {
57                      final String fileName = file.getName().getBaseName();
58                      System.out.println(fileName);
59                  }
60              }
61          } catch (final Exception e) {
62              e.printStackTrace();
63          }
64      }
65  }