【问题标题】:Uploading specific file to a folder (FTP) in java将特定文件上传到java中的文件夹(FTP)
【发布时间】:2018-05-04 04:02:42
【问题描述】:

美好的一天,

我只需要你关于我的程序的帮助.. 基本上我的程序打算从本地 pc 传输文件或复制文件并将其传输到远程站点 (FTP) 这是我的代码:

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();
                        }
                    }

我有一个错误:

java.io.FileNotFoundException:\Test(指定路径无效)

但目标文件夹是Test 这是特定文件夹/Test/file 我希望你能帮助解决这个问题。先感谢您!

已编辑

我尝试使用@Whome 所说的内容,在第一次运行时它可以工作,然后在尝试重新运行后突然无法正常工作并出现上述相同的错误。

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

【问题讨论】:

  • 您可以访问 /Test 吗?另外,您的程序是否发送了“\Test”或“/Test”的命令?

标签: java file ftp transfer


【解决方案1】:

在上传文件之前尝试使用ftpclient.changeWorkingDirectory("/Test"),并可能在前面使用makeDirectory("/Test")。为什么你有 foreach 和 for(idx) 循环?更改工作目录后,只需使用不带完整路径的文件名进行上传。

【讨论】:

  • 感谢您的回答..我会尽力按照您所说的进行工作..我只想告诉您它是否有效,再次感谢!
  • 很抱歉再次打扰您,但它已经在工作,然后突然当我再次尝试重新运行它时它不起作用......您知道这种情况会发生什么吗?
最近更新 更多