Results 1 to 18 of 18

Thread: Help

  1. #1
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Help

    Hello All,

    I am VERYnew to java please kindly look at the code below what is wrong with it? I am trying to connect to the ftp server, and download files to a particular location however when run this program I get the following error , please note for obvious reasons I cannot display credentials. Please kindly get back to me


    Java Code:
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class FtpDownload {
    	private static final int BUFFER_SIZE = 4096;
    
    	public static void main(String[] args) { // this is a function
    		long startTime = System.currentTimeMillis();
    		String[] refBox = new String[4];
    		refBox[0] = "ent-14-0-win-20140417.zip";
    		refBox[1] = "ent-13-5-win-20140401.zip";
    		refBox[2] = "ent-13-0-win-20140401.zip";
    		refBox[3] = "ent-12-5-201301223.zip";
    		String ftpUrl = "ftp://%s:%s@%s/refs/%s"; // **username**:**password**@filePath
    													// ;
    		String[] refBox1 = { "ent-14-0-win-20140417.zip",
    				"ent-13-5-win-20140401.zip", "ent-13-0-win-20140401.zip",
    				"ent-12-5-201301223.zip" }; // name of the file which has to be
    											// download
    		String host = "sunshower"; // ftp server
    		String user = ""; // user name of the ftp server
    		String pass = ""; // password of the ftp server
    		String[] savePath = {
    				"C:/Users/bmolo/Eclipse-WorkspaceSeleniumTests/ent-14-0-win-20140417.zip",
    				"ent-13-5-win-20140401.zip", "ent-13-0-win-20140401.zip",
    				"ent-12-5-201301223.zip" };
    		ftpUrl = String.format(ftpUrl, user, pass, host, refBox1);
    		System.out.println("Connecting to FTP sunshower refs");
    		
    	int size = refBox.length;
    		System.out.println(refBox);
    		for (int i = 0; i < size; i++) {
    			System.out.println("Index[" + i + "]=" + refBox1[i]);
    		}
    
    		try {
    			URL url = new URL(ftpUrl);
    			URLConnection conn = url.openConnection();
    			InputStream inputStream = conn.getInputStream();
    			long filesize = conn.getContentLength();
    
    			System.out.println("Size of the file to download in kb is:"
    					+ filesize / 1024);
    
    			FileOutputStream outputStream = new FileOutputStream(savePath[size]);
    
    			byte[] buffer = new byte[BUFFER_SIZE];
    			int bytesRead = -1;
    			while ((bytesRead = inputStream.read(buffer)) != -1) {
    				outputStream.write(buffer, 0, bytesRead);
    			}
    			long endTime = System.currentTimeMillis();
    			System.out.println("File downloaded: " + refBox1);
    			System.out.println("Download time in sec. is:"
    					+ (endTime - startTime) / 1000);
    			outputStream.close();
    			inputStream.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		}
    	}
    
    }

    ERROR :

    Connecting to FTP sunshower refs
    [Ljava.lang.String;@7e8e68bd
    Index[0]=ent-14-0-win-20140417.zip
    Index[1]=ent-13-5-win-20140401.zip
    Index[2]=ent-13-0-win-20140401.zip
    Index[3]=ent-12-5-201301223.zip
    java.io.FileNotFoundException: refs/[Ljava.lang.String;@1f31cd12
    at sun.net.http://www.protocol.ftp.FtpURLConnec...tion.java:485)
    at FtpDownload.main(FtpDownload.java:42)
    Last edited by mademoiselle; 07-30-2014 at 12:45 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help

    When posting code please wrap it in [code] ... [/code] tags so it retains its formatting.

    A brief glimpse at the error and it seems you are getting a file not found.
    Can't tell where in your code that is being thrown from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thank you for getting back to me, can you please kindly ellaborate "Can't tell where in your code that is being thrown from"

    Quote Originally Posted by Tolls View Post
    When posting code please wrap it in [code] ... [/code] tags so it retains its formatting.

    A brief glimpse at the error and it seems you are getting a file not found.
    Can't tell where in your code that is being thrown from.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help

    Because I can't follow your code as it isn't in code tags.
    Which line of your code is that exception being thrown from?

    And please edit your original post to add in code tags.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thank you for getting back to me i have done as you have said and seems its from line 42
    Java Code:
    InputStream inputStream = conn.getInputStream();
    Quote Originally Posted by Tolls View Post
    Because I can't follow your code as it isn't in code tags.
    Which line of your code is that exception being thrown from?

    And please edit your original post to add in code tags.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help

    Thanks for doing the tags.
    It's easier to see hte problem now.
    Java Code:
            ftpUrl = String.format(ftpUrl, user, pass, host, refBox1);
    If you print out that Url you'll see that refBox1 is not what you think it is.
    It's an array of Strings, so the value inserted into the base ftpUrl is going to be the '[Ljava.lang.String;@1f31cd12' bit of your exception message.

    Not sure what exactly you are attempting to insert into that url, but you can't stick that array in there like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    What will be the best way to tackle the issue? do pardon me i am very new to java
    Quote Originally Posted by Tolls View Post
    Thanks for doing the tags.
    It's easier to see hte problem now.
    Java Code:
            ftpUrl = String.format(ftpUrl, user, pass, host, refBox1);
    If you print out that Url you'll see that refBox1 is not what you think it is.
    It's an array of Strings, so the value inserted into the base ftpUrl is going to be the '[Ljava.lang.String;@1f31cd12' bit of your exception message.

    Not sure what exactly you are attempting to insert into that url, but you can't stick that array in there like that.

  8. #8
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    What i wanted was go through the ftp site and download the reference boxes specified in the array.
    Quote Originally Posted by Tolls View Post
    Thanks for doing the tags.
    It's easier to see hte problem now.
    Java Code:
            ftpUrl = String.format(ftpUrl, user, pass, host, refBox1);
    If you print out that Url you'll see that refBox1 is not what you think it is.
    It's an array of Strings, so the value inserted into the base ftpUrl is going to be the '[Ljava.lang.String;@1f31cd12' bit of your exception message.

    Not sure what exactly you are attempting to insert into that url, but you can't stick that array in there like that.

  9. #9
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    having taken out refBox1 this is the new error i get:

    Connecting to FTP sunshower refs
    [Ljava.lang.String;@7e8e68bd
    Index[0]=ent-14-0-win-20140417.zip
    Index[1]=ent-13-5-win-20140401.zip
    Index[2]=ent-13-0-win-20140401.zip
    Index[3]=ent-12-5-201301223.zip
    Size of the file to download in kb is:0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at FtpDownload.main(FtpDownload.java:48)


    Quote Originally Posted by Tolls View Post
    Thanks for doing the tags.
    It's easier to see hte problem now.
    Java Code:
            ftpUrl = String.format(ftpUrl, user, pass, host, refBox1);
    If you print out that Url you'll see that refBox1 is not what you think it is.
    It's an array of Strings, so the value inserted into the base ftpUrl is going to be the '[Ljava.lang.String;@1f31cd12' bit of your exception message.

    Not sure what exactly you are attempting to insert into that url, but you can't stick that array in there like that.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Help

    refBox is still 4 in size, yet now refBox1 is 3 in size.

    And then you have this bit of code:

    Java Code:
    int size = refBox.length;  // 4
            System.out.println(refBox);
            for (int i = 0; i < size; i++) {
                System.out.println("Index[" + i + "]=" + refBox1[i]);  // i can be 4, while refBox1 goes up to 3 only
            }
    As you can see, just trying things and hoping for the best doesn't work.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thank you , how do i fix this?

    Quote Originally Posted by gimbal2 View Post
    refBox is still 4 in size, yet now refBox1 is 3 in size.

    And then you have this bit of code:

    Java Code:
    int size = refBox.length;  // 4
            System.out.println(refBox);
            for (int i = 0; i < size; i++) {
                System.out.println("Index[" + i + "]=" + refBox1[i]);  // i can be 4, while refBox1 goes up to 3 only
            }
    As you can see, just trying things and hoping for the best doesn't work.

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Help

    Actually learning the basics of Java would be a good place to start.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thank you , i am trying to learn

    i have purchased a few books one of them:Java: A Beginner&#39;s Guide, Sixth Edition: Amazon.co.uk: Herbert Schildt: Books

    and Watching some youtube videos.

    Quote Originally Posted by gimbal2 View Post
    Actually learning the basics of Java would be a good place to start.

  14. #14
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Please if anyone reads this kindly point me in the right direction, as stated previously i am very new to java and tried for the last 24 hours to see where this is going wrong and fixing it however i am unable to

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Help

    Try patiently following those books rather than attempting to do something as complicated as working with an FTP server. You don't even know how to work with basic arrays yet and you are already involving networking into your code!

    Most people make the mistake of trying to run before they learn how to walk. You are attempting to fly a plane before you have learned how to ride a bicycle.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thanks for the suggestion, I am test engineer in the networking field , hence the inclusion of networking in my code,3
    I can appreciate your point of view and your humble advice which I have taken on gracefully.
    please kindly allow me to ask for help where possible. I am pretty sure when you started out learning you experienced road blocks and I am pretty sure like myself you looked for help in every possible source and I am sure someone came to your rescue. Once again thank you.


    Quote Originally Posted by gimbal2 View Post
    Try patiently following those books rather than attempting to do something as complicated as working with an FTP server. You don't even know how to work with basic arrays yet and you are already involving networking into your code!

    Most people make the mistake of trying to run before they learn how to walk. You are attempting to fly a plane before you have learned how to ride a bicycle.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Help

    Your basic logic should be:
    Java Code:
    for each file in your array of files
       connect to the server.
       write file out.
    The "connect to the server" part is where you build your url.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    mademoiselle is offline Member
    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help

    Thank you

    Will see if that solves the problem


    Quote Originally Posted by Tolls View Post
    Your basic logic should be:
    Java Code:
    for each file in your array of files
       connect to the server.
       write file out.
    The "connect to the server" part is where you build your url.

Posting Permissions

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