ftpclientconfig.java

来自「apache推出的net包」· Java 代码 · 共 518 行 · 第 1/2 页

JAVA
518
字号
/* * Copyright 2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.net.ftp;import java.text.DateFormatSymbols;import java.util.Collection;import java.util.Locale;import java.util.Map;import java.util.StringTokenizer;import java.util.TreeMap;/** * <p> * This class implements an alternate means of configuring the * {@link  org.apache.commons.net.ftp.FTPClient  FTPClient} object and * also subordinate objects which it uses.  Any class implementing the  * {@link  org.apache.commons.net.ftp.Configurable  Configurable }  * interface can be configured by this object.  * </p><p> * In particular this class was designed primarily to support configuration * of FTP servers which express file timestamps in formats and languages  * other than those for the US locale, which although it is the most common * is not universal.  Unfortunately, nothing in the FTP spec allows this to  * be determined in an automated way, so manual configuration such as this * is necessary. * </p><p> * This functionality was designed to allow existing clients to work exactly * as before without requiring use of this component.  This component should * only need to be explicitly invoked by the user of this package for problem * cases that previous implementations could not solve. * </p> * <h3>Examples of use of FTPClientConfig</h3> * Use cases: * You are trying to access a server that  * <ul>  * <li>lists files with timestamps that use month names in languages other  * than English</li> * <li>lists files with timestamps that use date formats other  * than the American English "standard" <code>MM dd yyyy</code></li> * <li>is in different timezone and you need accurate timestamps for  * dependency checking as in Ant</li> * </ul> * <p> * Unpaged (whole list) access on a UNIX server that uses French month names * but uses the "standard" <code>MMM d yyyy</code> date formatting * <pre> *    FTPClient f=FTPClient(); *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); *    conf.setServerLanguageCode("fr"); *    f.configure(conf); *    f.connect(server); *    f.login(username, password); *    FTPFile[] files = listFiles(directory); * </pre> * </p> * <p> * Paged access on a UNIX server that uses Danish month names * and "European" date formatting in Denmark's time zone, when you * are in some other time zone. * <pre> *    FTPClient f=FTPClient(); *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); *    conf.setServerLanguageCode("da"); *    conf.setDefaultDateFormat("d MMM yyyy"); *    conf.setRecentDateFormat("d MMM HH:mm"); *    conf.setTimeZoneId("Europe/Copenhagen"); *    f.configure(conf); *    f.connect(server); *    f.login(username, password); *    FTPListParseEngine engine = *       f.initiateListParsing("com.whatever.YourOwnParser", directory); * *    while (engine.hasNext()) { *       FTPFile[] files = engine.getNext(25);  // "page size" you want *       //do whatever you want with these files, display them, etc. *       //expensive FTPFile objects not created until needed. *    } * </pre> * </p>  * <p> * Unpaged (whole list) access on a VMS server that uses month names * in a language not {@link #getSupportedLanguageCodes() supported} by the system. * but uses the "standard" <code>MMM d yyyy</code> date formatting * <pre> *    FTPClient f=FTPClient(); *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_VMS); *    conf.setShortMonthNames( *        "jan|feb|mar|apr|ma\u00ED|j\u00FAn|j\u00FAl|\u00e1g\u00FA|sep|okt|n\u00F3v|des"); *    f.configure(conf); *    f.connect(server); *    f.login(username, password); *    FTPFile[] files = listFiles(directory); * </pre> * </p> * <p> * Unpaged (whole list) access on a Windows-NT server in a different time zone. * (Note, since the NT Format uses numeric date formatting, language issues * are irrelevant here). * <pre> *    FTPClient f=FTPClient(); *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT); *    conf.setTimeZoneId("America/Denver"); *    f.configure(conf); *    f.connect(server); *    f.login(username, password); *    FTPFile[] files = listFiles(directory); * </pre> * </p> * Unpaged (whole list) access on a Windows-NT server in a different time zone * but which has been configured to use a unix-style listing format. * <pre> *    FTPClient f=FTPClient(); *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); *    conf.setTimeZoneId("America/Denver"); *    f.configure(conf); *    f.connect(server); *    f.login(username, password); *    FTPFile[] files = listFiles(directory); * </pre> * </p> * @since 1.4 * @see org.apache.commons.net.ftp.Configurable * @see org.apache.commons.net.ftp.FTPClient * @see org.apache.commons.net.ftp.parser.FTPTimestampParserImpl#configure(FTPClientConfig) * @see org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl */public class FTPClientConfig{	    /**     * Identifier by which a unix-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_UNIX  = "UNIX";    /**     * Identifier by which a vms-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_VMS   = "VMS";        /**     * Identifier by which a WindowsNT-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_NT    = "WINDOWS";    /**     * Identifier by which an OS/2-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_OS2   = "OS/2";    /**     * Identifier by which an OS/400-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_OS400 = "OS/400";        /**     * Identifier by which an MVS-based ftp server is known throughout     * the commons-net ftp system.     */    public static final String SYST_MVS = "MVS";        private final String serverSystemKey;	private String defaultDateFormatStr = null;	private String recentDateFormatStr = null;	private String serverLanguageCode = null;	private String shortMonthNames = null;	private String serverTimeZoneId = null;			/**	 * The main constructor for an FTPClientConfig object	 * @param systemKey key representing system type of the  server being 	 * connected to. See {@link #getServerSystemKey() serverSystemKey}	 */	public FTPClientConfig(String systemKey) {		this.serverSystemKey = systemKey;	}	/**	 * Convenience constructor mainly for use in testing.	 * Constructs a UNIX configuration. 	 */	public FTPClientConfig() {	    this(SYST_UNIX);	}	/**	 * Constructor which allows setting of all member fields	 * @param systemKey key representing system type of the  server being 	 * connected to. See 	 *  {@link #getServerSystemKey() serverSystemKey}	 * @param defaultDateFormatStr See 	 * 	{@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}	 * @param recentDateFormatStr See	 * 	{@link  #setRecentDateFormatStr(String)  recentDateFormatStr}	 * @param serverLanguageCode See	 * 	{@link  #setServerLanguageCode(String)  serverLanguageCode}	 * @param shortMonthNames See	 * 	{@link  #setShortMonthNames(String)  shortMonthNames}	 * @param serverTimeZoneId See	 * 	{@link  #setServerTimeZoneId(String)  serverTimeZoneId}	 */	public FTPClientConfig(String systemKey,	        			   String defaultDateFormatStr,	        			   String recentDateFormatStr,	        			   String serverLanguageCode,	        			   String shortMonthNames,	        			   String serverTimeZoneId)	{	    this(systemKey);		this.defaultDateFormatStr = defaultDateFormatStr;		this.recentDateFormatStr = recentDateFormatStr;		this.serverLanguageCode = serverLanguageCode;		this.shortMonthNames = shortMonthNames;		this.serverTimeZoneId = serverTimeZoneId;	}		private static Map LANGUAGE_CODE_MAP = new TreeMap();	static {				// if there are other commonly used month name encodings which		// correspond to particular locales, please add them here.								// many locales code short names for months as all three letters		// these we handle simply.		LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);		LANGUAGE_CODE_MAP.put("de",Locale.GERMAN);		LANGUAGE_CODE_MAP.put("it",Locale.ITALIAN);		LANGUAGE_CODE_MAP.put("es", new Locale("es", "", "")); // spanish		LANGUAGE_CODE_MAP.put("pt", new Locale("pt", "", "")); // portuguese		LANGUAGE_CODE_MAP.put("da", new Locale("da", "", "")); // danish		LANGUAGE_CODE_MAP.put("sv", new Locale("sv", "", "")); // swedish		LANGUAGE_CODE_MAP.put("no", new Locale("no", "", "")); // norwegian		LANGUAGE_CODE_MAP.put("nl", new Locale("nl", "", "")); // dutch		LANGUAGE_CODE_MAP.put("ro", new Locale("ro", "", "")); // romanian		LANGUAGE_CODE_MAP.put("sq", new Locale("sq", "", "")); // albanian		LANGUAGE_CODE_MAP.put("sh", new Locale("sh", "", "")); // serbo-croatian		LANGUAGE_CODE_MAP.put("sk", new Locale("sk", "", "")); // slovak				LANGUAGE_CODE_MAP.put("sl", new Locale("sl", "", "")); // slovenian

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?