0

Is it possible to change the current sourcecode so I can use 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(); 

Is it possible without any additional libraries or if not, which library suits best?

1 Answers1

2

You can use: org.apache.commons.net.ftp.FTPSClient

Example to use it:

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);
    }
}
Maxim Markov
  • 101
  • 1
  • 8
  • 1
    thanks, will try it, so ther eis no direct solution using the normal Java Classes shipped by Oracle? –  Mar 12 '13 at 15:52
  • but need to add percentage = n; String n_perct = n+"% "; System.out.print("\rTransferring "+filename_real_string+" "+n_perct+""); ... to in.read also the inputstream is a remote url and the remote file should be transferred directly to the ftp printing information about the current status (percentage) –  Mar 12 '13 at 16:03
  • 1
    No, there is no native implementation of ftps client in Java. – Maxim Markov Mar 12 '13 at 16:15
  • oh ok =( and my part about the updating percentage value? –  Mar 12 '13 at 16:19
  • If the input stream is a remote url, it doesnt mean that file can be transfered from remote dest to other remote dest. It have to be downloaded and then uploaded. To get a download/upload progress you can use CountingInputStream/CountingOutputStream. Here is an example: http://stackoverflow.com/questions/5875837/monitoring-progress-using-apache-commons-ftpclient – Maxim Markov Mar 12 '13 at 16:19
  • really? => https://github.com/DanielRuf/Plain-of-JARs/blob/master/sources/gog2ftp/gog2ftp.java#L69 –  Mar 12 '13 at 16:24
  • Really. When you open an input stream bytes are first downloaded to your server. And it is not a big difference, is it an url on the web, or file in the local file system. – Maxim Markov Mar 12 '13 at 16:36
  • to my server? doesnt the java file from me transfer the bytes directly ? well I need the part from my other file with the percentage and the direct transfer there. –  Mar 12 '13 at 16:41