Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:46:51+00:00 2026-06-07T04:46:51+00:00

Below code uploads file on the FTP server public class UploadFile { static ResourceBundle

  • 0

Below code uploads file on the FTP server

public class UploadFile {
    static ResourceBundle rsBundle = ResourceBundle.getBundle("com.mindcraft.resources.resources");
    public void upload(String host,String username,String pwd,String inputfile,String uploadpath,String zip_filename)
    {      
        //String zip_file_name= rsBundle.getString("zip_filename");
        FTPClient ftp=new FTPClient();
        try {          
            int reply;  
            ftp.connect(host); 
            ftp.login(username, pwd); 
            reply = ftp.getReplyCode();
            System.out.println("reply1" + reply);
            if(!FTPReply.isPositiveCompletion(reply)) 
            {             
                ftp.disconnect();           

            }           
            System.out.println("FTP server connected."); 
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            InputStream input= new FileInputStream(inputfile);

            System.out.println("Directory.." + ftp.printWorkingDirectory());

            String dirTree=uploadpath;
            boolean dirExists = true;
            String[] directories = dirTree.split("/");
            for (String dir : directories )
            {
                if (!dir.isEmpty() )
                {
                    if (dirExists)
                    {
                        dirExists = ftp.changeWorkingDirectory(dir);
                        ftp.storeFile(dirTree+zip_filename,input);
                        System.out.println("1..");
                    }
                    if (!dirExists)
                    {

                        System.out.println("dir tree" + ftp.printWorkingDirectory());


                        if (!ftp.makeDirectory(dir))
                        {

                            throw new IOException("Unable to create remote directory '" + dir + "'.  error='" + ftp.getReplyString()+"'");
                            }
                        if (!ftp.changeWorkingDirectory(dir))
                        {

                            throw new IOException("Unable to change into newly created remote directory '" + dir + "'.  error='" + ftp.getReplyString()+"'");
                        }
                        System.out.println("dir tree" + ftp.printWorkingDirectory());
                        ftp.storeFile(dirTree+zip_filename,input);
                    }

                    }
                }      

            System.out.println( ftp.getReplyString() );
            input.close();
            ftp.logout();        
            } 
        catch(Exception e) 
            {                      
            System.out.println("err"+ e);          
            e.printStackTrace();     
            }
        finally 
        {
            if(ftp.isConnected())
            {
                try
                { 
                    ftp.disconnect();

                }
                catch(Exception ioe)
                {

                }

            }

        }
        } 
}

It works fine when the upload path has one folder eg. /folder1/

But It uploads blank file of byte 0 when there is subfolder or more than one directory eg. /folder1/folder2/

What can be the issue??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:46:52+00:00Added an answer on June 7, 2026 at 4:46 am

    ftp.storeFile(dirTree+zip_filename,input); should be called after the for creating all subdirectories, and going to the correct directory.

    BTW could have helped introducing a function makeAndGoToDirectory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the code below that uploads a file and inserts data into
I have a mysqli code below where it uploads a file and insert data
Below is part of the code where it uploads a file (fileImage) into a
How do I browse to upload a file using Dojo? See the code below:
below code is my databasehandler class i got it from a tutorial. Beside that
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
I am using the code below to upload an image through ftp $sFile=$ftp_dir./.$image_name; $image=$database_row[image];//image
I'm trying to upload PHP file via php using ftp details as mentioned below.
I have an issue where the code below sends the attachment but the file
Below I have included my code that uploads multiple images to a folder and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.