关于java 下载文件出现的问题:ParserInitializationException: Unknown parser type:xxx

在使用ftpClient 作为下载工具类的时候可能出现的问题:

 FTPFile[]  files = ftpClient.listFiles(new String(remote.getBytes("GBK"), "iso-8859-1"));

在这个地方可能会抛出 Unknown parser type:xxx异常;

问题:在FTPClient 类中:listFiles的执行过程如下:

listFiels()--->initiateListParsing(String parserKey, String pathname)---->__parserFactory.createFileEntryParser(parserKey);

其中__parserFactory:FTPFileEntryParserFactory   __parserFactory;

重点就是FTPFileEntryParserFactory 这个类:

这个类的createFileEntryParser方法里面规定解析的key(也就是系统的类别);

默认下是有UNIX,window等;

但是可能情况下你访问的ftpserver放回的key都不在这里面;

现在你需要做的就是另写一个类;继承DefaultFTPFileEntryParserFactory;

这这个方法里面添加你需要解析的类

@Override
	public FTPFileEntryParser createFileEntryParser(String key) {
		if (key == null) {
		      throw new ParserInitializationException("Parser key cannot be null");
		    }
		    Class parserClass = null;
		    FTPFileEntryParser parser = null;
		    try
		    {
		      parserClass = Class.forName(key);
		      parser = (FTPFileEntryParser)parserClass.newInstance();
		    }
		    catch (ClassNotFoundException e)
		    {
		      try
		      {
		        String ukey = null;
		        if (null != key)
		        {
		          ukey = key.toUpperCase(Locale.ENGLISH);
		        }
		        System.out.println("--------------------the ukey is :"+ukey);
		        if ((ukey.indexOf("UNIX") >= 0) || (ukey.indexOf("TYPE: L8") >= 0))
		        {
		          System.out.println("the ukey is :"+ukey);
		          parser = createUnixFTPEntryParser();
		        }
		        else if (ukey.indexOf("VMS") >= 0)
		        {
		          parser = createVMSVersioningFTPEntryParser();
		        }
		        else if (ukey.indexOf("WINDOWS") >= 0 || ukey.indexOf("MSDOS") >= 0||(ukey.indexOf("FTP SERVER, HUAWEI CO")>=0))
		        {
		          //妈的,非得逼我这么干
		          parser = createNTFTPEntryParser();
		        }
		        else if (ukey.indexOf("OS/2") >= 0)
		        {
		          parser = createOS2FTPEntryParser();
		        }
		        else if ((ukey.indexOf("OS/400") >= 0) || (ukey.indexOf("AS/400") >= 0))
		        {
		          parser = createOS400FTPEntryParser();
		        }
		        else if (ukey.indexOf("MVS") >= 0)
		        {
		          parser = createMVSEntryParser();
		        }
		        else if (ukey.indexOf("NETWARE") >= 0)
		        {
		          parser = createNetwareFTPEntryParser();
		        }
		        else
		        {
		          throw new ParserInitializationException("Unknown parser type: " + key);
		        }
		      }
		      catch (NoClassDefFoundError nf) {
		        throw new ParserInitializationException("Error initializing parser", nf);
		      }

		    }
		    catch (NoClassDefFoundError e)
		    {
		      throw new ParserInitializationException("Error initializing parser", e);
		    }
		    catch (ClassCastException e)
		    {
		      throw new ParserInitializationException(parserClass.getName() + " does not implement the interface " + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
		    }
		    catch (Throwable e)
		    {
		      throw new ParserInitializationException("Error initializing parser", e);
		    }

		    return parser;
	}

在你的设置你的ftpclient的解析实现类:ftpClient.setParserFactory(new OSSFTPFileEntryParserFactory());

这样问题就over 

你可能感兴趣的:(关于java 下载文件出现的问题:ParserInitializationException: Unknown parser type:xxx)