简体   繁体   中英

Uploading specific file to a folder (FTP) in java

Good Day,

I just need you help regarding on my program.. basically my program is intend to transfer files or copy files from the local pc and transfer it to a remote site (FTP) Here is my code:

FTPClient destFtpClient = new FTPClient();
                    destFtpClient.connect(destIPAddressCom, intPort);
                    destFtpClient.login(destFtpID, destFtpPwd);
                    destFtpClient.enterLocalPassiveMode();

                    destFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

                    String newRoot = recipeRoot.toString();
                    File[] transFiles = new File(newRoot).listFiles();
                        for(File file : transFiles) {
                            for(int i = 0; i < transFiles.length; i++){
                            File destFile = new File(destTest); //destination path
                            //File newDestFile = new File(destFile +File.separator+file.getName()); // destination path with the file
                            FileInputStream fisFile = new FileInputStream(destFile);
                            destFtpClient.storeFile(file.getName(), fisFile);
                            fisFile.close();
                        }
                    }

I've got an error:

java.io.FileNotFoundException: \\Test (The specified path is invalid)

but the destination folder is Test this is the specific folder /Test/file I hop you can help regarding on this one. Thank you in advance!

EDITED

I tried to used what @Whome said and on the first run it works then suddenly after trying to rerun it does not work and got the same error above.

destFtpClient.changeWorkingDirectory("//Test");
                                destFtpClient.makeDirectory("//Test");
                                File destFile = new File(destTest);
                                FileInputStream fisFile = new FileInputStream(p1dest);
                                destFtpClient.storeFile(file.getName(), fisFile);

Try using ftpclient.changeWorkingDirectory("/Test") before uploading files and possibly leading makeDirectory("/Test") . Why do you have foreach and for(idx) loops? Once your working directory is changed then upload just using a filename without a full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM