我正在尝试从ftp服务器检索文件但是我收到如下错误。你能帮我吗
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream.GetField;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
公共类FtpTest_V1 {
public static void main(String [] args){
String server = "192.168.200.8"; int port = 21; String user = "Test_user"; String pass = "123456**"; FileOutputStream fos = null; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); ftpClient.enterLocalPassiveMode(); int replyCode = ftpClient.getReplyCode(); System.out.println(replyCode); //isPositiveCompletion if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println(FTPReply.isPositiveCompletion(replyCode)); System.out.println(FTPReply.isNegativeTransient(replyCode)); System.out.println("Connect failed"); return; } boolean success = ftpClient.login(user, pass); //System.out.println(success); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } // Lists files and directories FTPFile[] files1 = ftpClient.listFiles("TEST2"); //printFileDetails(files1); // uses simpler methods String[] files2 = ftpClient.listNames("TEST2"); printNames(files2); String[] files = files2; for (String aFile: files) { String filename = aFile; fos = new FileOutputStream(filename); // Download file from FTP server ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos); } } catch (IOException ex) { System.out.println("An Error Occured"+ex.getMessage()); System.out.println("Warning! Something wrong happened"); ex.printStackTrace(); } finally { // logs out and disconnects from server try { if (fos != null) { fos.close(); } if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } private static void printNames(String files[]) { if (files != null && files.length > 0) { int i =0; for (String aFile: files) { i++; } System.out.println("Number of files = "+i); for (String aFile: files) { System.out.println(aFile); } } } private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER : " + aReply); } }
}
}
这是我的输出:
服务器:220-FileZilla Server版本0.9.43 beta
SERVER:220 Hello FTP SERVER
220
服务器:230登录
文件数= 2
TEST2 / testfile.csv
TEST2 / tttt.csv
错误OccuredTEST2 \ testfile.csv(系统找不到指定的路径)
警告!发生了一些错误
java.io.FileNotFoundException:TEST2 \ testfile.csv(系统找不到路径 指定)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream。(Unknown Source)
at java.io.FileOutputStream。(Unknown Source)
at FtpTest_V1.main(FtpTest_V1.java:74)
答案 0 :(得分:1)
斜杠应该像这样倒退:“C:\ \ test \ \ FTP_TEST \ \ GET”(没有中间空格,我这样说是因为此页面中的html解析器将其更改为只有一个斜杠) 内容:
ftpClient.retrieveFile("C://test//FTP_TEST//GET" + filename, >fos);
甚至更好:
"C:" + File.separatorChar + "test" + File.separatorChar + "FTP_TEST" + File.separatorChar + "GET"
答案 1 :(得分:0)
您好我已经解决了更改代码的问题。尝试catch块如下
ftpClient.connect(服务器,端口);
ftpClient.login(user,pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String [] files = ftpClient.listNames(“TEST_FILE”);
for (String aFile: files) { String remoteFile1 = aFile; File downloadFile1 = new File("C:/test/FTP_TEST
/ GET /“+ remoteFile1.replace(”TEST_FILE“,”“));
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1)); boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1); outputStream1.close(); if (success) { System.out.println("File #1 has been downloaded successfully."); } }