0

是否可以更改当前源代码以便我可以使用 FTPS?:

InputStream in = new URL(url).openStream();
OutputStream out = new     URL("ftp://"+user+":"+password+"@"+server+""+dir+""+filename_real_string).openConnection().get    OutputStream();
byte[] buffer = new byte[16384];
while ((r=in.read(buffer))>=0) {
  out.write(buffer, 0, r);
}
in.close();
out.close(); 

是否有可能没有任何额外的库,或者如果没有,哪个库最适合?

4

1 回答 1

3

您可以使用:org.apache.commons.net.ftp.FTPSClient

使用它的示例:

public static void main(String[] args) {
    FTPSClient ftp = new FTPSClient();
    String host = "server.com";
    int port = 2121;
    String folderName = "dir";
    String username = "user";
    String password = "password";
    try {
        ftp.connect(host, port);
        ftp.login(username, password);
        InputStream fis = new FileInputStream("../filename_src.txt");

        ftp.storeFile("/" + folderName + "/filename_dest.xml", fis);
        fis.close();

        ftp.logout();
        ftp.disconnect();
    } catch (SocketException ex) {
        Logger.getLogger(FTPSendMessage.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FTPSendMessage.class.getName()).log(Level.SEVERE, null, ex);
    }
}
于 2013-03-12T15:44:35.113 回答