Error with FTP Uploading.

Discussion in 'Java' started by Branin, Feb 10, 2011.

  1. Branin

    Branin New Member

    Joined:
    Feb 10, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Greetings,

    I have come to go4expert in search of a solution for this error I am having with my Java Applet - What I am trying to do here is be able to gather certain information that is used in my program and then upload that information to my FTP in a .txt file (or any filetype, for that matter) - I am a relativly new Java coder and am just having problems with the FTP connection bit of this Applet, It would be highly appreciated if someone with a little more knowledge would take a look at my code and tell me what I am doing wrong, I have edited out my ftp password however if it should be required I will give it to anyone willing to help (as it is just a free FTP anyway) - I have pasted the code below, the main errors I am getting is when I click to upload the file contents to FTP it either gives an error saying it cannot change to the Directory mentioned, the directory that I've selected to navigate to will give an error saying the directory is non-existent (this is the directory my free host gave to me) however when I leave that same box blank instead it will then give an error telling me that the filename is invalid for PUT - any information would be GREATLY appreciated!
     
  2. Branin

    Branin New Member

    Joined:
    Feb 10, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Could not post the code in previous post, have uploaded in a .txt attachment
     

    Attached Files:

  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Actually most people here would like the code to be inside a post..
    I dont know much about Java..but all i can do for you is post the code in a post as to get more help....

    Code:
    package OldVersions.EffigyListV3;
    
    import java.net.*;
    
    import java.io.*;
    
    
    
    public class SimpleFTPClient
    
    {
    
        /** The URL connection object */
    
        private URLConnection m_client;
    
    
    
        /** The FTP host/server to be connected */
    
        private String host;
    
    
    
        /** The FTP user */
    
        private String user;
    
    
    
        /** The FTP user’s password */
    
        private String password;
    
    
    
        /** The remote file that needs to be uploaded or downloaded */
    
        private String remoteFile;
    
    
    
        /** The previous error message triggered after a method is called */
    
        private String erMesg;
    
    
    
        /** The previous success message after any method is called */
    
        private String succMesg;
    
    
    
        public SimpleFTPClient(String host, String user, String password, String remoteFile){
    
            this.host = host;
    
            this.user = user;
    
            this.password = password;
    
            this.remoteFile = remoteFile;
    
        }
    
    
    
        /** Setter method for the FTP host/server */
    
        public void setHost(String host)
    
        {
    
            this.host = host;
    
        }
    
    
    
        /** Setter method for the FTP user */
    
        public void setUser(String user)
    
        {
    
            this.user = user;
    
        }
    
    
    
        /** Setter method for the FTP user’s password */
    
        public void setPassword(String p)
    
        {
    
            this.password = p;
    
        }
    
    
    
        /** Setter method for the remote file, this must include the sub-directory path relative
    
       to the user’s home directory, e.g you’e going to download a file that is within a sub directory
    
       called “sdir”, and the file is named “d.txt”, so you shall include the path as “sdir/d.txt”
    
         */
    
        public void setRemoteFile (String d)
    
        {
    
            this.remoteFile = d;
    
        }
    
    
    
        /** The method that returns the last message of success of any method call */
    
        public synchronized String getLastSuccessMessage()
    
        {
    
            if (succMesg==null) return ""; return succMesg;
    
        }
    
    
    
        /** The method that returns the last message of error resulted from any exception of any method call */
    
        public synchronized String getLastErrorMessage()
    
        {
    
            if (erMesg==null) return ""; return erMesg;
    
        }
    
    
    
        /** The method that handles file uploading, this method takes the absolute file path
    
       of a local file to be uploaded to the remote FTP server, and the remote file will then
    
       be transfered to the FTP server and saved as the relative path name specified in method setRemoteFile
    
       @param localfilename – the local absolute file name of the file in local hard drive that needs to
    
          FTP over
    
         */
    
        public synchronized boolean uploadFile (String localfilename)
    
        {
    
            try{
    
                InputStream is = new FileInputStream(localfilename);
    
                BufferedInputStream bis = new BufferedInputStream(is);
    
                OutputStream os =m_client.getOutputStream();
    
                BufferedOutputStream bos = new BufferedOutputStream(os);
    
                byte[] buffer = new byte[1024];
    
                int readCount;
    
    
    
                while( (readCount = bis.read(buffer)) > 0)
    
                {
    
                    bos.write(buffer, 0, readCount);
    
                }
    
                bos.close();
    
    
    
                this.succMesg = "Uploaded!";
    
    
    
                return true;
    
            }
    
            catch(Exception ex)
    
            {
    
                StringWriter sw0= new StringWriter();
    
                PrintWriter p0= new PrintWriter(sw0,true);
    
                ex.printStackTrace(p0);
    
                erMesg = sw0.getBuffer().toString();
    
    
    
                return false;
    
            }
    
        }
    
    
    
        /** The method to download a file and save it onto the local drive of the client in the specified absolut path
    
       @param localfilename – the local absolute file name that the file needs to be saved as */
    
        public synchronized boolean downloadFile (String localfilename)
    
        {
    
            try{
    
                InputStream is = m_client.getInputStream();
    
                BufferedInputStream bis = new BufferedInputStream(is);
    
    
    
                OutputStream os = new FileOutputStream(localfilename);
    
                BufferedOutputStream bos = new BufferedOutputStream(os);
    
    
    
                byte[] buffer = new byte[1024];
    
                int readCount;
    
    
    
                while((readCount = bis.read(buffer)) > 0)
    
                {
    
                    bos.write(buffer, 0, readCount);
    
                }
    
                bos.close();
    
                is.close(); // close the FTP inputstream
    
                this.succMesg = "Downloaded!";
    
    
    
                return true;
    
            }catch(Exception ex)
    
            {
    
                StringWriter sw0= new StringWriter();
    
                PrintWriter p0= new PrintWriter(sw0,true);
    
                ex.printStackTrace(p0);
    
                erMesg = sw0.getBuffer().toString();
    
    
    
                return false;
    
            }
    
        }
    
    
    
        /** The method that connects to the remote FTP server */
    
        public synchronized boolean connect()
    
        {
    
            try{
    
    
    
                URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i");
    
    
    
                m_client = url.openConnection();
    
    
    
                return true;
    
    
    
            }
    
            catch(Exception ex)
    
            {
    
                StringWriter sw0= new StringWriter();
    
                PrintWriter p0= new PrintWriter (sw0, true);
    
                ex.printStackTrace (p0);
    
                erMesg = sw0.getBuffer().toString();
    
                return false;
    
            }
    
        }
    
    
    
        public static void main(String[] s) {
    
            SimpleFTPClient test = new SimpleFTPClient("host", "user",
    
                  "password", "/");// used to be: "/www/99k.org/e/f/f/effigylist/htdocs/"); for the 4th parameter, i have tried / and leaving this box blank to try and fix errors, no luck
    
          test.connect();
    
          System.out.println("Errors(connection): " + test.getLastErrorMessage());
    
          System.out.println("Other(connection): " + test.getLastSuccessMessage());
    
          
    
          test.uploadFile("C:\\Users\\Chad\\Documents\\EffigyListFTPTest.txt");
    
          System.out.println("Errors(upload): " + test.getLastErrorMessage());
    
          System.out.println("Other(upload): " + test.getLastSuccessMessage());
    
        }
    
    } 
     
  4. Branin

    Branin New Member

    Joined:
    Feb 10, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for doing that, I actually tried to post at first but had trouble with it saying I had to many links in it. Cheers ^^
     
  5. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Hmm!! Strange...Maybe shabbir could help you on this...
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The reason is code have www in the code which is detected as a try to place link. This limitation is till any user reaches double digit post count.
     
  7. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks for letting us know that!!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice