设为首页 收藏本站
查看: 865|回复: 0

[经验分享] 开发FTP不要使用sun.net.ftp.ftpClient

[复制链接]

尚未签到

发表于 2016-6-8 11:54:55 | 显示全部楼层 |阅读模式
在开发一个web应用过程中,需要开发一个服务使用ftp功能将数据传输一个网外的ftp服务器。最初使用sun.net.ftp.ftpClient类,但是遇到问题,在网内测试没有问题,向网外传时报告失败。开发环境如下:
web服务:tomcat 5.5.28
OS平台:Linux 5
java: 1.5
失败报告:port命令失败,试试用pasv代替。代码如下:
TelnetOutputStream os = null;
FileInputStream in =null;
try {
logger.debug("开始上传文件"+sourceFile);
java.io.File file_in = new java.io.File(sourceFile);
in = new FileInputStream(file_in);
//ftpClient.sendServer("TYPE I \r\n");
//ftpClient.sendServer("PASV \r\n" );
//logger.debug("发送TYPE I 和 PASC命令");
// 命名文件,将文件名编码转为utf-8,否则中文文件名上载后为乱码文件名
//os = ftpClient.put(new String(targetFile.getBytes("UTF-8")));
os = ftpClient.put(targetFile);
logger.debug("创建"+targetFile+" 成功");
byte[] bytes = new byte[4096];
int c;
while ((c = in.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} catch (IOException e) {
logger.error(e.getMessage());
return 0;
} finally {
if (in != null) {
in.close();
}
if (os != null) {
os.close();
}
}
代码在os = ftpClient.put(targetFile)这句出错,这样的代码在网上都很常见,但大约可以肯定的是许多的人只是拷贝复制下来构造一篇博文,并没有真正实践过。
  试着给服务器发送PASV命令也不行。查看ftpClient的源代码,发现ftpClient在上载数据前首先尝试PASV模式,如PASV模式失败再使用PORT模式。通过TelnetOutputStream os = null此句代码,推测是否使用了telent功能,调整两边的路由器防火墙功能,折腾半死,程序失败依旧。
  鉴于源代码使用telnetoutputStream,又没有控制传输模式的方法和命令,最后只好弃用sun.net.ftp.ftpClient,使用org.apache.commons.net.ftp.FTPClient类开发调试成功。部分代码如下:
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
ftpClient.enterLocalPassiveMode();
logger.debug("Set pasv return code:"+ftpClient.getReplyCode());
flag = ftpClient.storeFile(newName, iStream);
logger.debug("upload file return code:"+ftpClient.getReplyCode());
} catch (IOException e) {
logger.debug("upload error:"+ftpClient.getReplyCode());
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}

  最后,找到java的老巢去,发现在java 1.5的文档里,有这样一段话:
Why Developers Should Not Write Programs
That Call 'sun' Packages
The classes that Sun includes with the Java 2 SDK, Standard Edition, fall into package groups java.*, javax.*, org.* and sun.*. All but the sun.* packages are a standard part of the Java platform and will be supported into the future. In general, packages such as sun.*, that are outside of the Java platform, can be different across OS platforms (Solaris, Windows, Linux, Macintosh, etc.) and can change at any time without notice with SDK versions (1.2, 1.2.1, 1.2.3, etc). Programs that contain direct calls to the sun.* packages are not 100% Pure Java. In other words:
    The java.*, javax.* and org.* packages documented in the Java 2 Platform Standard Edition API Specification make up the official, supported, public interface.
    If a Java program directly calls only API in these packages, it will operate on all Java-compatible platforms, regardless of the underlying OS platform.
    The sun.* packages are not part of the supported, public interface.
    A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.
For these reasons, there is no documentation available for the sun.* classes. Platform-independence is one of the great advantages of developing in the Java programming language. Furthermore, Sun and our licensees of Java technology are committed to maintaining backward compatibility of the APIs for future versions of the Java platform. (Except for code that relies on serious bugs that we later fix.) This means that once your program is written, the class files will work in future releases.
  具体URL:http://java.sun.com/products/jdk/faq/faq-sun-packages.html
  真是为在sun.net.ftp上浪费掉的时间懊恼不已。
  留文于此,希望广大朋友们在开发过程中少走弯路。
  PS到处拷贝粘帖赚文章的“专业技术作家”!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227846-1-1.html 上篇帖子: cPanel文件管理之创建FTP账户 下篇帖子: 用common-net-ftp上传文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表