java 中sun.net.ftp_开发FTP不要使用sun.net.ftp.ftpClient
轉(zhuǎn)自:http://cai21cn.iteye.com/blog/700188
在開(kāi)發(fā)一個(gè)web應(yīng)用過(guò)程中,需要開(kāi)發(fā)一個(gè)服務(wù)使用ftp功能將數(shù)據(jù)傳輸一個(gè)網(wǎng)外的ftp服務(wù)器。最初使用sun.net.ftp.ftpClient類(lèi),但是遇到問(wèn)題,在網(wǎng)內(nèi)測(cè)試沒(méi)有問(wèn)題,向網(wǎng)外傳時(shí)報(bào)告失敗。開(kāi)發(fā)環(huán)境如下:
web服務(wù):tomcat 5.5.28
OS平臺(tái):Linux 5
java: 1.5
失敗報(bào)告:port命令失敗,試試用pasv代替。代碼如下:
Java代碼??
TelnetOutputStream?os?=?null;
FileInputStream?in?=null;
try?{
logger.debug("開(kāi)始上傳文件"+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("發(fā)送TYPE?I?和 PASC命令");
//?命名文件,將文件名編碼轉(zhuǎn)為utf-8,否則中文文件名上載后為亂碼文件名
//os?=?ftpClient.put(new?String(targetFile.getBytes("UTF-8")));
os?=?ftpClient.put(targetFile);
logger.debug("創(chuàng)建"+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)這句出錯(cuò),這樣的代碼在網(wǎng)上都很常見(jiàn),但大約可以肯定的是許多的人只是拷貝復(fù)制下來(lái)構(gòu)造一篇博文,并沒(méi)有真正實(shí)踐過(guò)。
試著給服務(wù)器發(fā)送PASV命令也不行。查看ftpClient的源代碼,發(fā)現(xiàn)ftpClient在上載數(shù)據(jù)前首先嘗試PASV模式,如PASV模式失敗再使用PORT模式。通過(guò)TelnetOutputStream os = null此句代碼,推測(cè)是否使用了telent功能,調(diào)整兩邊的路由器防火墻功能,折騰半死,程序失敗依舊。
鑒于源代碼使用telnetoutputStream,又沒(méi)有控制傳輸模式的方法和命令,最后只好棄用sun.net.ftp.ftpClient,使用org.apache.commons.net.ftp.FTPClient類(lèi)開(kāi)發(fā)調(diào)試成功。部分代碼如下:
Java代碼??
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;
}
最后,找到j(luò)ava的老巢去,發(fā)現(xiàn)在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上浪費(fèi)掉的時(shí)間懊惱不已。
留文于此,希望廣大朋友們?cè)陂_(kāi)發(fā)過(guò)程中少走彎路。
PS到處拷貝粘帖賺文章的“專(zhuān)業(yè)技術(shù)作家”!
---------------------------------
目前官方文章已經(jīng)改寫(xiě)為:
Why Developers Should Not Write Programs??That Call 'sun' Packages
The java.*, javax.* and org.* packages documented in the Java 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.
Each company that implements the Java platform will do so in their own private way. The classes in sun.* are present in the JDK to support Oracle's implementation of the Java platform: the sun.* classes are what make the Java platform classes work "under the covers" for Oracle's JDK. These classes will not in general be present on another vendor's Java platform. If your Java program asks for a class "sun.package.Foo" by name, it may fail with ClassNotFoundError, and you will have lost a major advantage of developing in Java.
Technically, nothing prevents your program from calling into sun.* by name. From one release to another, these classes may be removed, or they may be moved from one package to another, and it's fairly likely that their interface (method names and signatures) will change. (From Oracle's point of view, since we are committed to maintaining the Java platform, we need to be able to change sun.* to refine and enhance the platform.) In this case, even if you are willing to run only on Oracle's implementation, you run the risk of a new version of the implementation breaking your program.
In general, writing java programs that rely on sun.* is risky: those classes are not portable, and are not supported.
總結(jié)
以上是生活随笔為你收集整理的java 中sun.net.ftp_开发FTP不要使用sun.net.ftp.ftpClient的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 港航和民航哪个值钱?
- 下一篇: java+cache使用方法_java相