Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer a file from url to ftp using java?

Given a url, I want to transfer the file in that url to a specific to ftp. I have two options: 1) Using external library (like apache common-io) I can fetch the file from the url and then upload it to the ftp. 2) Open two sockets (to the url an to the ftp) and simply transfer the file from the url to the ftp.

I'm I right about this? What's the best way to do that?

I'm using spring.

Here are some links that I found about it:

How to download and save a file from Internet using Java?.

Here is the apache common-io library

Edit:

I'm dealing with 100-200M files.

Update: I choose to first download the file and then upload it. I choose to do this since I don't expect many problems downloading the file, but I expect problems uploading the file to the FTP server. So, I think the the error handling will better that way. The lack of this solution is the fact that I'm storing the temporary file in the disk. I'll update later if I'll encounter some problems.

like image 332
Noam Avatar asked Oct 18 '25 02:10

Noam


1 Answers

1. Frankly Speaking, If you are using Open-Source, then take the advantage of External Libraries.

2. Do Not re-invent the wheel, you have the trusted libraries at your disposal, so use it.

I am also attaching my program which i used to upload and download song to ftp server using the apache's common lib

Uploading :

public void goforIt(){


        FTPClient con = null;

        try
        {
            con = new FTPClient();
            con.connect("192.168.2.57");

            if (con.login("Administrator", "KUjWbk"))
            {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);
                String data = "/sdcard/vivekm4a.m4a";

                FileInputStream in = new FileInputStream(new File(data));
                boolean result = con.storeFile("/vivekm4a.m4a", in);
                in.close();
                if (result) Log.v("upload result", "succeeded");
                con.logout();
                con.disconnect();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }






    }

Dowloading:

public void goforIt(){
    FTPClient con = null;

    try
    {
        con = new FTPClient();
        con.connect("192.168.2.57");

        if (con.login("Administrator", "KUjWbk"))
        {
            con.enterLocalPassiveMode(); // important!
            con.setFileType(FTP.BINARY_FILE_TYPE);
            String data = "/sdcard/vivekm4a.m4a";

            OutputStream out = new FileOutputStream(new File(data));
            boolean result = con.retrieveFile("vivekm4a.m4a", out);
            out.close();
            if (result) Log.v("download result", "succeeded");
            con.logout();
            con.disconnect();
        }
    }
    catch (Exception e)
    {
        Log.v("download result","failed");
        e.printStackTrace();
    }



}
like image 189
Kumar Vivek Mitra Avatar answered Oct 20 '25 14:10

Kumar Vivek Mitra