日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java 中sun.net.ftp_开发FTP不要使用sun.net.ftp.ftpClient

發布時間:2024/9/19 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 中sun.net.ftp_开发FTP不要使用sun.net.ftp.ftpClient 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:http://cai21cn.iteye.com/blog/700188

在開發一個web應用過程中,需要開發一個服務使用ftp功能將數據傳輸一個網外的ftp服務器。最初使用sun.net.ftp.ftpClient類,但是遇到問題,在網內測試沒有問題,向網外傳時報告失敗。開發環境如下:

web服務:tomcat 5.5.28

OS平臺:Linux 5

java: 1.5

失敗報告:port命令失敗,試試用pasv代替。代碼如下:

Java代碼??

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類開發調試成功。部分代碼如下:

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;

}

最后,找到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到處拷貝粘帖賺文章的“專業技術作家”!

---------------------------------

目前官方文章已經改寫為:

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.

總結

以上是生活随笔為你收集整理的java 中sun.net.ftp_开发FTP不要使用sun.net.ftp.ftpClient的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美一区欧美二区 | 国产精品免费入口 | 成人国产精品一区二区 | 怡春院视频 | 日韩不卡一区二区三区 | av在线色 | 精品人妻一区二区三区香蕉 | 朱竹清到爽高潮痉挛 | 亚洲激情视频在线观看 | 日本激情网站 | 亚洲成人福利 | 蜜臀av一区二区三区有限公司 | 伊人7 | 三上悠亚一区二区在线观看 | 久操av在线 | 少妇大叫太粗太大爽一区二区 | 熟女丰满老熟女熟妇 | 国产精品99| 免费看a级片 | 韩国一级一片高清免费观看 | 成人性生活毛片 | 欧美成人一级片 | 青草成人免费视频 | 色吧久久| 在线观看麻豆视频 | 丰满人妻一区二区三区精品高清 | 日本不卡在线视频 | 久久久久久久性 | 在线中文字幕亚洲 | 丰满少妇一区 | 中国大陆一级片 | 福利资源在线 | 精品日本一区二区三区在线观看 | 欧美激情综合五月色丁香 | 欧美精品手机在线 | 疯狂做受xxxx国产 | 欧美午夜三级 | 亚洲再线 | 蜜臀国产AV天堂久久无码蜜臀 | 性xxxx视频播放免费 | 国产综合精品视频 | 国产精品一区二区入口九绯色 | 激情网久久 | 欧美又粗又大xxxxbbbb疯狂 | 日老女人视频 | 在线免费观看 | 免费亚洲精品 | 污网在线观看 | 国产免费99 | 韩国中文字幕hd久久精品 | 99爱国产| 四虎免费视频 | 91人人爱| 一级黄网| 午夜影院在线观看视频 | 亚州激情| 无套内谢少妇露脸 | 一本一道久久综合 | 中文在线资源天堂 | 对白超刺激精彩粗话av | 夜夜爽夜夜操 | 久久伊人五月天 | 国产精品成人一区二区网站软件 | 91福利视频在线观看 | 黄色工厂在线观看 | 亚洲私人影院 | 亚洲影院在线观看 | 久久网址 | 成人福利视频网 | 国产精品午夜久久 | 国产高潮视频 | 国产精品国产三级国产aⅴ无密码 | 欧美日韩精选 | 成人激情视频在线播放 | 成年男女免费视频网站 | 国产麻豆精品一区二区 | 狂野少女电影在线观看国语版免费 | 黄色网在线播放 | 国产99久久 | 久久久久久www | 色伊人影院| xx视频在线观看 | 免费极品av一视觉盛宴 | 黄色录像片子 | 亚洲www啪成人一区二区麻豆 | 无码人妻丰满熟妇区毛片18 | 亚洲国产综合在线 | 国产精品精华液网站 | 亚洲成人h | 国产人成在线 | 欧美一区二区三区四区视频 | 成人免费看视频 | 国产精品4| 午夜日韩福利 | 日本美女裸体视频 | av久久久 | 高清国产mv在线观看 | 老熟妇毛片 | 色女综合 |