我試圖從ftp服務器檢索文件,但出現以下錯誤。請你幫我從FTP服務器檢索Java文件
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 {
公共靜態無效的主要(字串[] 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); } }
}
}
這是我的輸出:
SERVER:220的FileZilla服務器版本0.9.43測試
SERVER:220你好FTP服務器
SERVER:230登錄的
數量的文件= 2
TEST2/testfile.csv
TEST2/tttt.csv
發生錯誤TEST2 \ testfile.csv(系統找不到指定的路徑)
警告!發生了什麼錯誤
java.io.FileNotFoundException:TEST2 \ testfile.csv(系統找不到指定的路徑 )
在java.io.FileOutputStream中。開(本機方法)
在java.io.FileOutputStream中。(來源不明)
在java.io.FileOutputStream中。(來源不明)
在FtpTest_V1.main(FtpTest_V1.java:74)
很抱歉,但它仍然提供了相同的錯誤java.io.FileNotFoundException:TEST2 \ 0000047521LZAT00090001_10Palet_253Koli的.csv(系統找不到指定的路徑) \t在java.io.FileOutputStream.open(本機方法) \t在java.io.FileOutputStream中。(來源不明) \t在java.io.FileOutputStream中。(Unknown Source) \t at FtpTest_V1.main(FtpTest_V1.java:75) –
zorox
eclipse顯示fos = new FileOutputStream(filename);作爲錯誤。 – zorox
@zorox你需要傳遞FileOutputStream的完整路徑,不僅是文件名,這種方式只在源文件夾中看起來 – Typo