問題描述
在使用 URLConnection 類時(shí)是否可以設(shè)置 FTP 被動或主動模式?我找不到任何關(guān)于此的文檔,但這是一個(gè)非常基本的功能,很難相信它沒有實(shí)現(xiàn).
Is it possible to set FTP passive or active mode when working with URLConnection class? I cannot find any documentation on this, yet this is such a basic functionality that it's hard to believe it is not implemented.
如果不是,我在標(biāo)準(zhǔn) Java 中還有哪些其他選擇?
If it's not, what other options do I have in standard Java?
推薦答案
要讓 FTP 完全正常工作,你真的需要使用 套接字.URLConnection
和 consorts 根本不適合您的需要(不支持進(jìn)入被動模式).幸運(yùn)的是,Apache Commons 已經(jīng)為您完成了許多類似 Apache Commons Net FTPClient 的工作.要實(shí)現(xiàn)您的需要,只需使用 FTPClient#enterLocalPassiveMode()
或 FTPClient#enterLocalActiveMode()
To get FTP full fledged to work, you'll really need to fall back to low-level programming with Sockets. The URLConnection
and consorts simply doesn't suit your needs (no support to enter into passive mode). Fortunately Apache Commons has already done a lot of work for you in flavor of Apache Commons Net FTPClient. To achieve what you need, just use FTPClient#enterLocalPassiveMode()
or FTPClient#enterLocalActiveMode()
這篇關(guān)于使用 Java URLConnection 設(shè)置 FTP 主動/被動模式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!