gpt4 book ai didi

从 FTP 服务器检索 Java 文件

转载 作者:行者123 更新时间:2023-12-01 13:36:21 24 4
gpt4 key购买 nike

我试图从 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)

最佳答案

斜杠应该像这样向后:“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"

关于从 FTP 服务器检索 Java 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com