从FTP服务器检索Java文件
我试图从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;
public class 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); } }
}
}
以下是我的输出:
SERVER : 220-FileZilla Server version 0.9.43 beta
SERVER : 220 Hello FTP SERVER
220
SERVER : 230 Logged on
number of files = 2
TEST2/testfile.csv
TEST2/tttt.csv
An Error OccuredTEST2\testfile.csv (The system cannot find the path specified)
Warning! Something wrong happened
java.io.FileNotFoundException: TEST2\testfile.csv (The system cannot find the path specified)
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)
# 1 楼答案
您好,我已经解决了更改代码时的问题。试抓块如下所示
# 2 楼答案
斜杠应该像这样倒过来:“C:\\test\\FTP\u test\\GET”(没有中间的空格,我这样说是因为这个页面中的html解析器只将其更改为一个斜杠) 国际贸易组织:
或者更好: