Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to Download a file via FTP

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to Download a file via FTP

    With this simple code you can download a file to your computer via FTP.

    You will need to amend the connection String if you wish to connect to a directory other than /public_html/

    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class JFTP {
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static String server = "ftp.server.com";
        public static String userName = "username";
        public static String password = "password";
        public static String fileName = "index.html";
     
        public static void main(String[] args) throws Exception{
     
        System.out.println("Connecting to FTP server...");    
     
        //Connection String
        URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/public_html/"+fileName+";type=i");
        //URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/"+fileName+";type=i");
        URLConnection con = url.openConnection();
     
        BufferedInputStream in = new BufferedInputStream(con.getInputStream());
     
        System.out.println("Downloading file.");
     
        //Downloads the selected file to the C drive
        FileOutputStream out = new FileOutputStream("C:\\" + fileName);
     
        int i = 0;
        byte[] bytesIn = new byte[1024];
        while ((i = in.read(bytesIn)) >= 0) {
            out.write(bytesIn, 0, i);
        }
        out.close();
        in.close();
     
        System.out.println("File downloaded.");
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Download a file via FTP

    Good example. Suggest to use Apache's Common FTP library.

Similar Threads

  1. Detecting File Download Completion
    By fedfan in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 5th, 2009, 04:27 AM

Tags for this Thread