ftp 不要用 sun ftpclient

2024-05-13 14:32
文章标签 sun ftp 不要 ftpclient

本文主要是介绍ftp 不要用 sun ftpclient,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在开发一个web应用过程中,需要开发一个服务使用ftp功能将数据传输一个网外的ftp服务器。最初使用sun.net.ftp.ftpClient类,但是遇到问题,在网内测试没有问题,向网外传时报告失败。开发环境如下: 
web服务:tomcat 5.5.28 
OS平台:Linux 5 
java: 1.5 
失败报告:port命令失败,试试用pasv代替。代码如下: 
Java代码   收藏代码
  1. TelnetOutputStream os = null;  
  2.         FileInputStream in =null;             
  3.         try {  
  4.             logger.debug("开始上传文件"+sourceFile);  
  5.             java.io.File file_in = new java.io.File(sourceFile);  
  6.             in = new FileInputStream(file_in);  
  7.             //ftpClient.sendServer("TYPE I \r\n");  
  8.             //ftpClient.sendServer("PASV \r\n" );  
  9.             //logger.debug("发送TYPE I 和 PASC命令");  
  10.             // 命名文件,将文件名编码转为utf-8,否则中文文件名上载后为乱码文件名            
  11.             //os = ftpClient.put(new String(targetFile.getBytes("UTF-8")));  
  12.             os = ftpClient.put(targetFile);  
  13.             logger.debug("创建"+targetFile+" 成功");  
  14.             byte[] bytes = new byte[4096];  
  15.             int c;  
  16.             while ((c = in.read(bytes)) != -1) {  
  17.                 os.write(bytes, 0, c);  
  18.             }  
  19.         } catch (IOException e) {  
  20.             logger.error(e.getMessage());  
  21.             return 0;  
  22.         } finally {  
  23.             if (in != null) {  
  24.                 in.close();  
  25.             }  
  26.             if (os != null) {  
  27.                 os.close();  
  28.             }  
  29.             }  

代码在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类开发调试成功。部分代码如下: 
Java代码   收藏代码
  1. public boolean uploadFile(String fileName, String newName)  
  2.         throws IOException {  
  3.     boolean flag = false;  
  4.     InputStream iStream = null;  
  5.     try {  
  6.         iStream = new FileInputStream(fileName);  
  7.         ftpClient.enterLocalPassiveMode();  
  8.         logger.debug("Set pasv return code:"+ftpClient.getReplyCode());  
  9.         flag = ftpClient.storeFile(newName, iStream);  
  10.         logger.debug("upload file return code:"+ftpClient.getReplyCode());  
  11.     } catch (IOException e) {  
  12.         logger.debug("upload error:"+ftpClient.getReplyCode());  
  13.         flag = false;  
  14.         return flag;  
  15.     } finally {  
  16.         if (iStream != null) {  
  17.             iStream.close();  
  18.         }  
  19.     }  
  20.     return flag;  
  21. }  

  最后,找到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到处拷贝粘帖赚文章的“专业技术作家”!

这篇关于ftp 不要用 sun ftpclient的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/985967

相关文章

「Bioconductor」不要轻易相信AnnotationHub的物种注释包

Bioconductor开发的物种注释包系列集合了一个物种不同来源的注释信息,能够根据基因ID对其进行多种来源的注释,比如说基因的别名,基因的功能等。 我之前也写过一篇文章用Bioconductor对基因组注释介绍如何使用AnnotationHub下载注释数据库, 使用select(), mapIds等函数进行注释操作。我自己写一个流程也用到了它给基因ID, 如AT1G14185, 注释别名和功

删除CentOS 7自带的OpenJDK 和 安装Sun的JDK1.8

系统有时候会默认使用OpenJDK版本,需要卸载后,重新安装自己需要的JDK版本  查询OpenJDK,发现有两个openJDK版本          删除openJDK版本          再次查询openJDK版本,发现已被删除          官网上下载1.8版本的tar包,并解压值usr/java路径下(路径可自定义)          进入

Spring Boot启动报错Lombok supports: sun/apple javac 1.6, ECJ

版本 idea 2023.3.4 <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.32</version></dependency> 解决方式 File->Settings->Build, Execution, Deployment->C

当公司气的你要死的时候,请不要迁怒到公司的人

当公司SB的时候,不要迁怒于和你合作的同事。 他们本来是可爱的个体,但是为这样的公司服务,所以做出的事情可能让人讨厌。 当你离开公司的时候, 你才认清他们。

NetSuite最佳实践:要不要使用Bin,这是个问题

NetSuite最佳实践:要不要使用Bin,这是个问题 如果我适合使用Bin呢?如何去启用Bin? 有库存的NetSuite需要回答的一个问题是,“要使用Bin吗?”。对于这个问题并不那么容易回答,但是下面有一些因素,会影响到你最后的决策。 当我们开始的时候,要记住一件事,那就是Bin需要结合特殊的仓库流程来使用。如果遵守这些流程,你的库存将更加精确和快速挑库,而且有更精确的订

druid-spring-boot-stater-1.2.6+maven3.8.2 -> Cannot resolve com.sun:tools:1.8

druid-spring-boot-stater-1.2.6+maven3.8.2 -> Cannot resolve com.sun:tools:1.8 问题描述问题重现问题解决问题原因 问题描述 druid-spring-boot-stater-1.2.6+maven3.8.2 会导致编译时出现 Cannot resolve com.sun:tools:1.8报错 问题重现

ftp和ftpget 命令

ubuntu 本身不带ftpget命令要先建立软链接 ln -s  /bin/busybox  ftpget 我用Serv-U 软件在window上开了FTP服务器,电脑本机地址192.168.1.156  登录名zyx 密码123456  共享目录D:\     下载hello 1.用ftp命令下载程序 ftp 192.168.1.156 输入登录名zyx密码123456 ftp> get

linux 登录ftp报Received message too long 1416128883

在linux 登录ftp报Received message too long 1416128883 [root@localhost ~]# sftp oaftp@172.20.xx.xx 解决办法: [root@localhost ~]# usermod -s /bin/bash oaftp [root@localhost ~]# systemctl restart vsftpd.s

阿里云服务器提醒漏洞要不要打补丁?

我们自己用的电脑一旦发现漏洞,往往是第一时间进行打补丁重启等等,但是作为服务器而言,往往没有这个习惯,为什么?因为害怕服务器打补丁以后,重启后出现打不开的情况,毕竟稳定的运行似乎在这种情况下显得更为重要。 用过阿里云服务器小伙伴应该都知道,在服务器列表会提醒你服务器的安全状态,比如发现多少高危、中危、低危等的漏洞,比如下图的提醒。 那么什么样的漏洞我们该打补丁呢?根据阿里云的提示是,高危漏

Linux服务之FTP

使用linux搭建FTP服务器不仅功能较windows多,而且在安全性上也是值得放心的。那么如何用linux搭建FTP服务器呢?别着急,耐心看完本文,相信你掌握使用linux操作系统搭建FTP服务器的。用linux搭建FTP服务器的方法很多,本文以linux自带的vsftpd来搭建FTP。vsftpd是一个安全、高速、稳定的FTP服务器。   一:安装、启动vsftpd