import java.io.*;

public class ftp {

    public ftp() {
    	
    URL url =     new URL("ftp://username:password@ftp.whatever.com/file.zip;type=i");
    URLConnection con = url.openConnection();
	
	BufferedInputStream in =     new BufferedInputStream(con.getInputStream());
	FileOutputStream out =     new FileOutputStream("C:\\file.zip");
	
	int i = 0;
	byte[] bytesIn = new byte[1024];
	
	while ((i = in.read(bytesIn)) >= 0)
	 {
	out.write(bytesIn, 0, i);
	 }
	out.close();
	in.close();
    	
    }   
}

above are the codes I've found but I cant seem to compile the codes. It says afew errors: "cannot find symbol class URL, cannot find symbol class URLConnection". What did I left out??

Recommended Answers

All 44 Replies

Read the API doc for those classes and see what package they are in.
Add import statements to your code for those packages.

I've added in the import statements, however, I still have errors which I cant find the faults in it. Listed below are my codes and errors.

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ftp {

    public ftp() {
    	
    URL url = new URL("ftp://username:password@ftp.whatever.com/file.zip;type=i");
    URLConnection con = url.openConnection();
	
	BufferedInputStream in = new BufferedInputStream(con.getInputStream());
	FileOutputStream out = new FileOutputStream("C:\\file.zip");
	
	int i = 0;
	byte[] bytesIn = new byte[1024];
	
	while ((i = in.read(bytesIn)) >= 0)
	 {
	out.write(bytesIn, 0, i);
	 }
	out.close();
	in.close();
    	
    }   
}

D:\project1\MP Project\Java Files\ftp.java:9: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
URL url = new URL("ftp://username:password@ftp.whatever.com/file.zip;type=i");
^
D:\project1\MP Project\Java Files\ftp.java:10: unreported exception java.io.IOException; must be caught or declared to be thrown
URLConnection con = url.openConnection();
^
D:\project1\MP Project\Java Files\ftp.java:12: unreported exception java.io.IOException; must be caught or declared to be thrown
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
^
D:\project1\MP Project\Java Files\ftp.java:13: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileOutputStream out = new FileOutputStream("C:\\file.zip");
^
D:\project1\MP Project\Java Files\ftp.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
while ((i = in.read(bytesIn)) >= 0)
^
D:\project1\MP Project\Java Files\ftp.java:20: unreported exception java.io.IOException; must be caught or declared to be thrown
out.write(bytesIn, 0, i);
^
D:\project1\MP Project\Java Files\ftp.java:22: unreported exception java.io.IOException; must be caught or declared to be thrown
out.close();
^
D:\project1\MP Project\Java Files\ftp.java:23: unreported exception java.io.IOException; must be caught or declared to be thrown
in.close();
^
8 errors

All the error messages say this:

must be caught

Put the code with the errors in try{}catch(){} blocks as the error message requires.

You can probably put all of the code in one block. Be sure to have a printStackTrace() call in the catch block.

Hi Norm, thanks for you reply. I've added in the try and catch statements and it can now be compiled however Im still encountering problems, it cannot seem to be able to download files from the ftp site. below are my codes and the error:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ftp {

    public ftp() {
    	
    	try{
    	
    URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//;type=i");
    URLConnection con = url.openConnection();
	
	BufferedInputStream in = new BufferedInputStream(con.getInputStream());
	FileOutputStream out = new FileOutputStream("D:\\project1\\MP Project\\FTP Downloads\\file.pdb");
	
	int i = 0;
	byte[] bytesIn = new byte[1024];
	
	while ((i = in.read(bytesIn)) >= 0)
	 {
	out.write(bytesIn, 0, i);
	 }
	out.close();
	in.close(); 
    	} catch(Exception e) 
    		{
    			e.printStackTrace();
    		}
    }

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

Can you explain what you do to get that error?
Can you show the command line you entered to get that error message.
What folder are you in and what files are in that folder?

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ftp {

    public ftp() {
    	
    	try{
    	
    URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//;type=i");
    URLConnection con = url.openConnection();
	
	BufferedInputStream in = new BufferedInputStream(con.getInputStream());
	FileOutputStream out = new FileOutputStream("D:\\project1\\MP Project\\FTP Downloads\\file.pdb");
	
	int i = 0;
	byte[] bytesIn = new byte[1024];
	
	while ((i = in.read(bytesIn)) >= 0)
	 {
	out.write(bytesIn, 0, i);
	 }
	out.close();
	in.close(); 
    	} catch(Exception e) 
    		{
    			e.printStackTrace();
    		}
    }

basically what i did whas adding in the TRY, CATCH and e.printStackTrace() statements.

the file output stream folders is actually where i want the file to be saved in, the folder contains no file for the time being as I have yet to start the downloading using this program.

What about the execution error you posted:

java.lang.NoSuchMethodError: main
Exception in thread "main"

Is that solved now?

Not solved yet, it happened after I've added the try and catch statements.

Can you explain what you do to get that error?
Can you show the command line you entered to get that error message.
What folder are you in and what files are in that folder?

URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//;type=i");

this command is to get the files that i want from this link.

FileOutputStream out = new FileOutputStream("D:\\project1\\MP Project\\FTP Downloads\\file.pdb");

This is the line i that i want my downloaded files to be. In the FTP Downloads folder, there are currently no files.

try{
} catch(Exception e) 
   {
    e.printStackTrace();
    }

This is what I've added into the program, leading to it showing the error that I've posted earlier

Hi Norm I've changed my codes and now when i execute my program, a firewall popup shows and it says Java 2TM has been blocked by it. Does it mean I have to disable my firewall?

below are my codes:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ftp {

    public static void main(String[] args) {
    	
    	try{
    	
    URL url = new URL("ftp://ftp.wwpdb.org//pub//pdb//data//structures//all//pdb//;type=i");
    URLConnection con = url.openConnection();
	
	BufferedInputStream in = new BufferedInputStream(con.getInputStream());
	FileOutputStream out = new FileOutputStream("D://project1//MP Project//FTP Downloads//");
	
	int i = 0;
	byte[] bytesIn = new byte[1024];
	
	while ((i = in.read(bytesIn)) >= 0)
	 {
	out.write(bytesIn, 0, i);
	 }
	out.close();
	in.close(); 
    	} catch(Exception e) 
    		{
    			e.printStackTrace();
    		}
    }   
}

this is the error i get:

java.io.FileNotFoundException: D:\project1\MP Project\FTP Downloads (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at ftp.main(ftp.java:15)

Process completed.

FileNotFoundException: D:\project1\MP Project\FTP Downloads

That looks like a folder name, not a filename. Add a filename to the end of it.

The FTP protocol has two modes, with one the server tries to connect to your PC using a second connection. I don't remember how to tell the server to reuse the one connection you've established. Something about passive mode perhaps.

I've added in the file name and all but it still cannot work.

import java.io.*;

import java.net.URL;
import java.net.URLConnection;
import java.io.FileOutputStream;


public class ftp {

    public static void main(String[] args) {
    	
    	try{
    	
    URL url =     new URL("ftp://ftp.ww.pdb.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);
	 }
	out.close();
	in.close();
    	} catch(Exception e)
    	{
    		e.printStackTrace();
    	}	
    }   
}

java.net.UnknownHostException: ftp.ww.pdb.org
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
at ftp.main(ftp.java:25)

Process completed.

.UnknownHostException: ftp.ww.pdb.org

Is the URL you are trying to use correct?

It should be correct. What i did was just copying n pasting of the link of the ftp site. were you able to access it as well?

No, I can't. I get this:

This webpage is not available.

The webpage at ftp://ftp.ww.pdb.org/ might be temporarily down or it may have moved permanently to a new web address.

oh no so sorry, there's an error in my link its wwpdb not ww.pdb. I've made the changes already and now I can only download one file in that directory..what do i do if i need to download all the files in the directory? is for loop usable?

It should be correct

Best to double check your data before posting. It will save us all a lot of time.

download all the files in the directory

Put the names in a list and do a loop over the list.

putting the names in a list is going to be time consuming as i have tens of thousands of files to download..is it possible to use the following code?

File dir = new File("1AIV.txt"); 					//set file
     String path = dir.getAbsolutePath();				//get file path
     int lala = path.lastIndexOf("\\");					//get index of '\'
     String howPath = path.substring(0,lala+1);			//extract directory name
     File usePath = new File(howPath);					//assign directory for reading file
     String[] fileDir = usePath.list();					//store file in dit to array
     if(fileDir == null) 								//check if dir exists
     {
     	System.out.println("Directory Does Not Exist");
     }
      else
      {
     	for(int j=0; j<fileDir.length-2;j++)

Im not sure if this only work for offline directories.

I can now download one file using my codes, however I need to download all the files in that directory, how do i go about doing that??

import java.io.*;

import java.net.URL;
import java.net.URLConnection;
import java.io.FileOutputStream;


public class ftp {

    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=d");
    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);
	 }
	out.close();
	in.close();
    	} catch(Exception e)
    	{
    		e.printStackTrace();
    	}	
    }   
}

I need to download all the files in that directory, how do i go about doing that??

As I said before get a list of the filenames and have a loop that gets the files one by one.

are there any sample codes? because i really dont know how to start.so sorry

I've changed it accordingly to the one below, it has no errors however It says Directory does not exist. Did i input the link wrongly?

import java.io.*;
import java.io.FileOutputStream;


public class ftp {

    public static void main(String[] args) {
    	
    	File dir = new File("ftp://ftp.wwpdb.org//pub//pdb//data//structures//all//pdb//;type=d");

    	String path = dir.getAbsolutePath();
    	int haha = path.lastIndexOf("\\");
    	String howPath = path.substring(0, haha+1);
    	File usePath = new File(howPath);
    	String[] fileDir = usePath.list();
    	
    	if(fileDir == null)
    	{
    		System.out.println("Directory Does Not Exist");
    	}
    	
    	else
    	{
    	for(int j=0; j<fileDir.length-2; j++)
    	{  
    		  String fileName = fileDir[j];	
    	try{
	
	BufferedReader in =     new BufferedReader(new FileReader (fileDir[j]));
	FileWriter out =     new FileWriter("C:\\Users\\Royston\\Documents\\FTP downloads\\"+ fileName);
    	out.close();
    	in.close();    	
    	} catch(Exception e)
    	{
    		e.printStackTrace();
    	}
	
    	}
    	}
    }   
}

Hi Norm, 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\\FTP 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\\FTP 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 ) {} 
      }  
    }

cross-post

And, according to a post in that thread, he's very happy to let people waste their time.

@yap_1991 Which forum do you want to use?

either is fine.

One or the other please. You chose and post on both which is to be used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.