Results 1 to 13 of 13
  1. #1
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default Downloading FILES from a server

    Hi all, Im currently working on a FTP program that downloads all files in a directory. Currently, I am able to list all the files in that directory, but I am stuck as on how to download all of them automatically. Would need some help in this. Thank you.

    Java Code:
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.*;
    
    public class DownLoad {
    	
    	public static void main(String args[]) {
    		try {
    			java.net.URL url = new java.net.URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//all//pdb//");
    			java.io.InputStream in = url.openStream();
    			
    			int count = in.available();
    			byte content[] = new byte[count];
    			in.read(content, 0, count);
    				String contentAsString = new String(content, 0, count);
    				System.out.println(contentAsString);
    				
    									} catch (Exception e) {
    							e.printStackTrace();
    											}
    										   }
    						}

  2. #2
    cnovice is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi, You need a FTP library

    I recommand to Apache Commons Net library

    Supported protocols are:
    FTP/FTPS
    NNTP
    SMTP
    POP3
    Telnet
    TFTP
    Finger
    Whois
    rexec/rcmd/rlogin
    Time (rdate) and Daytime
    Echo
    Discard
    NTP/SNTP

    You will implement your program to using this library
    library URL Commons Net - Jakarta Commons Net

  3. #3
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    actually I also have the program to download one file..but how do i make it download all the files in that directory??

    Java Code:
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.FileOutputStream;
    import java.io.*;
    
    public class fileTransferProtocol {
    
        public static void main(String[] args) {
        	
        	try{
        	
        	URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//all//pdb//pdb100d.ent.gz;type=i");
    		URLConnection con = url.openConnection();
     
        	BufferedInputStream in= new BufferedInputStream(con.getInputStream());
    		FileOutputStream out = new FileOutputStream("C:\\Users\\Royston\\Documents\\FTP downloads\\pdb100d.ent.gz");
    
    			int i = 0;
    			byte[] bytesIn = new byte[1024];
    				while ((i = in.read(bytesIn)) >= 0) {
    				out.write(bytesIn, 0, i);
    													}
    				System.out.println("Download complete!!");
    					out.close();
    					in.close();  
        	}catch (Exception e){
        		e.printStackTrace();
        	}
        }  
    }
    Moderator edit: Quote tags changed to code tags
    Last edited by Fubarable; 07-26-2010 at 04:02 AM. Reason: Quote tags changed to code tags

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    First of all, is that your FTP connection made successfully in your code? I cannot see any authentication for that.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    To download multiple files, you need the list of all files within that directory. But how can you do that from the server, basically from the downloader. You had to have done it through a web service.

  6. #6
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    the connection was made successfully. the program is supposed to download all files automatically without any authentication needed. But I am only able to download a file through the file link. If i were to input the main directory, it will create a file that contains all that was seen on that main directory. How do i write the program to make it download all the files in that directory?

  7. #7
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    Hi eranga, im able to fulfill most of the things already however Im experiencing a problem that I cannot solve. I cannot download all the files even after I have managed to list all the files and inserting it into the link. The problem I suppose lies in the RED texts.

    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.FileOutputStream;
    import java.io.*;
    import java.util.*;

    public class CopyLines {

    public static StringBuffer buffer;
    public static BufferedReader input;

    public static void main(String[] args) throws IOException {

    try{

    URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//derived_data//pdb_entry_type.txt;type=i");
    URLConnection con = url.openConnection();

    BufferedInputStream in= new BufferedInputStream(con.getInputStream());
    FileOutputStream out = new FileOutputStream("C:\\Users\\Royston\\Documents\\F TP downloads\\pdb_entry_type.txt");

    int i = 0;
    byte[] bytesIn = new byte[1024];
    while ((i = in.read(bytesIn)) >= 0) {
    out.write(bytesIn, 0, i);
    }
    System.out.println("Download complete!!");
    out.close();
    in.close();
    }catch (Exception e){
    e.printStackTrace();
    }


    BufferedReader inputStream = null; // scan input line by line
    PrintWriter outputStream = null;// output aligned the same way

    try {
    inputStream =
    new BufferedReader(new FileReader("C:\\Users\\Royston\\Documents\\FTP downloads\\pdb_entry_type.txt"));
    outputStream =
    new PrintWriter(new FileWriter("C:\\Users\\Royston\\Documents\\FTP downloads\\pdb.txt"));

    String l;
    while ((l = inputStream.readLine()) != null) {
    outputStream.println(l);

    }
    } finally {
    if (inputStream != null) {
    inputStream.close();
    }
    if (outputStream != null) {
    outputStream.close();
    }
    }

    try{

    input = new BufferedReader(
    new FileReader( new File("C:\\Users\\Royston\\Documents\\FTP downloads\\pdb.txt") ) );
    String text;
    while ( ( text = input.readLine() ) != null ) {
    StringTokenizer s = new StringTokenizer(text," ");
    int counter=0;
    while(s.hasMoreTokens()) {
    String ss = s.nextToken();
    counter++;
    if (counter == 1 ) // extracts only 1st tokens per line.
    System.out.print(ss + "\n");



    URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//all//pdb//pdb"+ss+".ent.gz;type=i");
    URLConnection con = url.openConnection();

    BufferedInputStream in= new BufferedInputStream(con.getInputStream());
    FileOutputStream out = new FileOutputStream("C:\\Users\\Royston\\Documents\\F TP downloads\\"+ss+".ent.gz");

    int i = 0;
    byte[] bytesIn = new byte[1024];
    while ((i = in.read(bytesIn)) >= 0) {
    out.write(bytesIn, 0, i);
    }
    System.out.println("****************************** *******************");
    System.out.println(" File Transfer Protocol Download complete!!");
    System.out.println(" File Transfer Protocol Download complete!!");
    System.out.println(" File Transfer Protocol Download complete!!");
    System.out.println(" File Transfer Protocol Download complete!!");
    System.out.println("****************************** *******************");
    out.close();
    in.close();

    }
    }
    System.out.println();

    }catch( IOException ioException ) {}
    }
    }

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    cross-post

    Nice to know that Norm, and anyone posting here, will be wasting their time rehashing concepts that the people from other sites have already discussed because you didn't bother to reference the other post so that they could see what has already been done and decide whether or not the input they had is needed.

  9. #9
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    hi masijade if you're not to help I would recommend that you keep your comments to yourself, i've been writing my own codes and this is the last error im facing. Thanks

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So, you're going to let everyone here waste their time rehashing everything you've already discussed with Norm? Because it's too hard to include a link to that thread? Or is there some other reason everyone here should waste their time?

    P.S. Try to control what I post here.

  11. #11
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    Stop showing off here alright. there's a reason why there're forums. FOR PEOPLE TO SEEK HELP. Not for you to show off how great you are. Please, if you really need to comment, comment on what's being ask. The program I wrote is workable but not to what I need, understand?

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, and feel free to use them, but when you post on multiple sites then provide links to the other threads in all of the threads. Anything else is you simply enjoying watching others waste their time.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by yap_1991 View Post
    actually I also have the program to download one file..but how do i make it download all the files in that directory??
    If you can download one file you can download them all; simply make the file name a parameter of the method and call it in a loop for all the files you want to download.

    kind regards,

    Jos

Similar Threads

  1. Downloading a file from web server
    By rlaknar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2009, 09:15 AM
  2. How to use FTP to read files from server?
    By user12345 in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 12:46 PM
  3. Fetch files over web server
    By Juuno in forum Networking
    Replies: 3
    Last Post: 02-19-2009, 02:02 PM
  4. OutofMemory while downloading large files through FTP
    By deb_santanu in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2008, 01:04 PM
  5. Problems with downloading Files from Applet
    By Albert in forum Java Applets
    Replies: 1
    Last Post: 07-05-2007, 05:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •