Results 1 to 7 of 7

Thread: FTP problem

  1. #1
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default FTP problem

    Hello,

    This is the code that I am using to upload a file called "test" into a FTP. It's a txt file.
    The problem is that it uploads empty files with the same name of "test", and not the file I want.

    Can anyone tell me what I am doing wrong? Or suggest a better method?

    Java Code:
            URL url;
            try {
                url = new URL("ftp://user:pass@whatever.com/test.txt;type=i");
                URLConnection urlc = url.openConnection();            
                
                OutputStream os = urlc.getOutputStream(); // To upload
            }catch (MalformedURLException ex) {
                System.out.println("URL Exception: " + ex);
            }catch (IOException ex) {
                System.out.println("URL IOException: " + ex);
            }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: FTP problem

    Can you post a small complete program that compiles and executes for testing?

  3. #3
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: FTP problem

    There is one button in the program. You can find the programming of that button below.
    I have added a .zip file in attachment that contains the program itself.

    Needless to say I am not allowed to give you the real account and its username and password, so you'll have to fill that in for another FTP.

    The problem seems to be that the program creates an empty file that doesn't really exist and pushes that to the FTP. The actual file that I want to send is in the main folder of the programs folder.
    Thx.

    Java Code:
    private void Button_SendActionPerformed(java.awt.event.ActionEvent evt) {                                            
            String user="...",pass="...",host="whatever.com";
            URL url;
            try {
                url = new URL("ftp://"+user+":"+pass+"@"+host+"/"+"test.txt;type=i");
                URLConnection urlc = url.openConnection();            
                
                //InputStream is = urlc.getInputStream(); // To download
                OutputStream os = urlc.getOutputStream(); // To upload
            }catch (MalformedURLException ex) {
                System.out.println("URL Exception: " + ex);
            }catch (IOException ex) {
                System.out.println("URL IOException: " + ex);
            }       
            
        }
    Attached Files Attached Files

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: FTP problem

    There is no need for any GUI etc for testing. Can you write a small, simple program that compiles, executes and shows the problem.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: FTP problem

    That URL is pointing to a file on the FTP server, so how do you expect to upload a file like that?
    You probably ought to look at a package to do the bulk of this for you.
    Apache has an FTP client I believe.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    hareshom is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: FTP problem

    With OutputStream os = urlc.getOutputStream(); you will get the Outputstream;
    Now you need to read the file from local dir. and write it to that Outputstream.

    os.write(byte[] b, int off, int len) ;

  7. #7
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: FTP problem

    Thx all for the tips.

    I managed to write a program that does the trick just fine(see below).
    Small disadvantage is that you can have communication problems due to your firewall.

    For anyone who's wondering how I did it:

    Java Code:
            String host="whatever.com";
    	    String user="user";
    	    String pass="pass";
            String filename="text.txt";        
            FTPClient client = new FTPClient();
            FileInputStream fis = null;
            
            try {            
                client.connect(host);
                client.login(user,pass);
                
                fis = new FileInputStream(filename);
                
                client.storeFile(filename, fis);
                client.logout();            
            } catch (IOException ex) {
                System.out.println("Exception FTP: " + ex);
            }finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    client.disconnect();
                } catch (IOException e) {
                    System.out.println("Exception FTP_end: " + e);
                }
            }

Posting Permissions

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