001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.libcheck; 018 019import java.io.OutputStream; 020import java.nio.charset.Charset; 021 022import org.apache.commons.net.ftp.FTPClient; 023import org.apache.commons.net.ftp.FTPFile; 024import org.apache.commons.net.ftp.FTPReply; 025 026/** 027 * Basic check for FTP. 028 */ 029public final class FtpCheck { 030 private FtpCheck() { 031 /* main class not instantiated. */ 032 } 033 034 public static void main(final String[] args) throws Exception { 035 if (args.length < 3) { 036 throw new IllegalArgumentException("Usage: FtpCheck user pass host dir"); 037 } 038 final String user = args[0]; 039 final String pass = args[1]; 040 final String host = args[2]; 041 String dir = null; 042 if (args.length == 4) { 043 dir = args[3]; 044 } 045 046 final FTPClient client = new FTPClient(); 047 client.connect(host); 048 final int reply = client.getReplyCode(); 049 if (!FTPReply.isPositiveCompletion(reply)) { 050 throw new IllegalArgumentException("cant connect: " + reply); 051 } 052 if (!client.login(user, pass)) { 053 throw new IllegalArgumentException("login failed"); 054 } 055 client.enterLocalPassiveMode(); 056 057 final OutputStream os = client.storeFileStream(dir + "/test.txt"); 058 if (os == null) { 059 throw new IllegalStateException(client.getReplyString()); 060 } 061 os.write("test".getBytes(Charset.defaultCharset())); 062 os.close(); 063 client.completePendingCommand(); 064 065 if (dir != null && !client.changeWorkingDirectory(dir)) { 066 throw new IllegalArgumentException("change dir to '" + dir + "' failed"); 067 } 068 069 System.err.println("System: " + client.getSystemType()); 070 071 final FTPFile[] files = client.listFiles(); 072 for (int i = 0; i < files.length; i++) { 073 final FTPFile file = files[i]; 074 if (file == null) { 075 System.err.println("#" + i + ": " + null); 076 } else { 077 System.err.println("#" + i + ": " + file.getRawListing()); 078 System.err.println("#" + i + ": " + file.toString()); 079 System.err.println("\t name:" + file.getName() + " type:" + file.getType()); 080 } 081 } 082 client.disconnect(); 083 } 084}