FTP中MLST概要解读---解决获取ftpFile为null的另外一种方式
2017-05-03 22:51
441 查看
零、引言
之前写FTP工具库,用的是ftp4j,他使用其他非常简单方便,但是在细节上提供的可选项比较少(当然也可能是我了解不够深刻)最新的项目重写了FTP工具类,选择了apache net中的ftp库,选择apache的原因有如下几个:1是我相信apche 2是它的注释完善(apache的代码注释值得每一位程序猿学习) 3是提供的可选配置(FTPConfig)有跟多选择(比如主动被动模式,断点续传等)。
本人在使用ftp4j判定文件是否存在的时候,通过API(具体那个忘了)获取FTPFile对象时,在部分FTP服务时(filezilla)会遇到返回值为null的问题(备注:原因是时间格式化的问题),当时解决判定文件是否存在改用只通过获取文件名来解决。
本次改用apache net的时候,在使用API listFiles() 获取的也是null,经过详细查看源码,发现了一个API是 mlistFile() 这样获取结果OK。
一、Apache的mlistFile源码分析
源码如下:/** * Get file details using the MLST command * * @param pathname the file or directory to list, may be {@code null} * @return the file details, may be {@code null} * @throws IOException on error * @since 3.0 */ public FTPFile mlistFile(String pathname) throws IOException { boolean success = FTPReply.isPositiveCompletion(sendCommand(FTPCmd.MLST, pathname)); if (success){ String reply = getReplyStrings()[1]; //5:check /* check the response makes sense. * Must have space before fact(s) and between fact(s) and filename * Fact(s) can be absent, so at least 3 chars are needed. */ if (reply.length() < 3 || reply.charAt(0) != ' ') { throw new MalformedServerReplyException("Invalid server reply (MLST): '" + reply + "'"); } String entry = reply.substring(1); // skip leading space for parser return MLSxEntryParser.parseEntry(entry); } else { return null; } }
我看源码大概分析出了2点:
1. 此方法调用的是MLST,而listFile调用的是LIST。问题来了:什么是MLST??
2. 代码方法体第4行: String reply = getReplyStrings()[1]; 获取返回码其目的是为了判定FTP服务器是否支持MLST??有些FTPServer不支持?
二、什么是MLST(红色部分)
Older servers supports LIST command only for directory listing, this way the FTP client gets a non-userfriendly raw format for parsing, and only FTP clients knows what it meaning. Since the file timestamp based on the server timezone, it makes more different to doing FTP synchronize with folders and files because of there is no way to get current file timestamp in the server.旧的服务对目录列表仅支持LIST命令,这样FTP客户端会获得一个非用户友好的待解析的原始格式,仅仅只有FTP客户端知道它的意义。由于文件的时间戳基于服务器的时区,它使得在FTP对目录和文件同步时有许多的不同,因为没有方法获得服务器上当前文件的时间戳。
For example there is a normally LIST format of raw directory:
-rw-r--r-- 1 user user 7080 Mar 9 05:24 faq.html
例如:这是对原始目录的普通LIST命令格式
-rw-r--r-- 1 user user 7080 Mar 9 05:24 faq.html
Is it readable for you?
是否可读呢?
The MLSD command provided by newer servers to gives users a standarded, detailed, readable directory listing, by sending MLSD command through FTP clients,the server returns accurate file information such as file create time, modified time, size and file owner. Since MLSD directory listing includes file modified time in UTC, so it's very useful for FTP client to converts remote file's timestamp to your Local time when synchronize folders. Also the MLST command could be used to get timestamp of single remote file only.
新的服务器提供的MLSD命令通过FTP客户端发送MLSD命令,服务器收集文件信息,如文件创建时间,修改时间,文件大小及文件所有用,向用户返回一个标准,详细且可读格式的目录列表。由于MLSD目录列表包含UTC格式的文件修改时间,因此这对于FTP客户端非常有用,当需要同步目录时,它可转换远程文件的时间戳到你的本地时间。同时,MLST命令也被用于获得单个远程文件的时间戳。
三、结尾
最近打王者有些沉迷,该反省。学知识,做技术,不能不求甚解。
相关文章推荐
- java.lang.UnsatisfiedLinkError,findLibrary returned null的一种可能的解决方式
- WORD 的 Open 和Workbook 的 LoadFromFile 函数返回null的一种解决方法
- System Error Code 111 The file name is too long的一种可能的解决方式
- 在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取。这里主要讲这个注解
- uri获取file路径为空null的解决办法
- ASP.NET DEVELOPMENT SERVER 未能开始侦听端口xxxxx以一种访问权限不允许的方式做了一个访问套接字的尝试——解决方法
- DAX 错误另外一种解决办法
- Sql Server 2005 中解决死锁的一种方式
- .net 2.0 中对于非服务器端的input元素 type为file后台无法获取的问题的解决方法
- 如何解决内网无法用PORT方式访问FTP服务器的问题
- Reflection中对于out类型的获取的另外一种方法
- Java中获取汉字字符串编码格式的一种可行方式
- 使用临时表的另外一种方式
- Heritrix2在Settings Sheets时出现java.lang.NullPointerException错的原因和解决方式
- 寻求一种尽可能简单处理方式来解决复杂问题
- 郁闷N天了,IDFTP发生10054错误后无法继续上传文件,重新创建IDFTP后发生错误:550 cannot create file。请教解决方法
- “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法
- [★] TreeView 开发 资源管理器 另外一种绑定方式
- 另外一种WebService的用户验证方式---使用验证票
- Linux下mysql 5.x得到root密码后的另外一种利用方式