该listFiles()
方法org.apache.commons.net.ftp.FTPClient
在127.0.0.1 null
上与Filezilla服务器一起正常工作,但返回到公共FTP服务器的根目录,如belnet.be.
在下面的链接上有一个相同的问题,但enterRemotePassiveMode()
似乎没有帮助.
Apache Commons FTPClient.listFiles
这可能是列表解析的问题吗?如果是这样,怎么能解决这个问题呢?
编辑:这是一个目录缓存转储:
FileZilla目录缓存转储
转储1个缓存目录
Entry 1:
Path: /
Server: anonymous@ftp.belnet.be:21, type: 4096
Directory contains 7 items:
lrw-r--r-- ftp ftp D 28 2009-06-17 debian
lrw-r--r-- ftp ftp D 31 2009-06-17 debian-cd
-rw-r--r-- ftp ftp 0 2010-03-04 13:30 keepalive.txt
drwxr-xr-x ftp ftp D 4096 2010-02-18 14:22 mirror
lrw-r--r-- ftp ftp D 6 2009-06-17 mirrors
drwxr-xr-x ftp ftp D 4096 2009-06-23 packages
lrw-r--r-- ftp ftp D 1 2009-06-17 pub
Run Code Online (Sandbox Code Playgroud)
这是我使用我所做的包装器的代码(在包装器内测试会产生相同的结果): …
是否可以使用FTPClient(Apache commons-net)检查远程目录是否存在?
我想做这样的事情:
ftp.isDirectory(String path) //returns true, false
Run Code Online (Sandbox Code Playgroud)
然后获取目录的权限(chmod):
ftp.getPermisions(String path) //returns -rwxr-xr-x
Run Code Online (Sandbox Code Playgroud) 我在Java项目中使用Apache的FTPClient和FTPServer库.服务器和客户端在同一台机器上.
我的FTPServer应该是本地服务器,与Internet无关.我可以从客户端连接到FTPServer(我得到230作为回复代码)但我似乎无能为力.我无法存储或检索任何文件.
我几乎阅读了与此事有关的每一个问题,但是那些提出其他问题的人能够发送简单的文件,并且无法发送像pdf等文件.我只需要发送或检索文本文件.
有什么建议?
FTPClient client = new FTPClient();
String host = "mypc";
String Name = "user";
String Pass = "12345";
client.connect(host);
client.login(Name,Pass);
System.out.println("Reply Code: " +client.getReplyCode());
File file = new File("C:\\.....myfile..txt");
FileInputStream in = new FileInputStream("C:\\.....myfile..txt");
boolean isStored = client.storeFile("uploadedfile.txt", in);
in.close();
client.logout();
System.out.println("isStored: " +isStored);
Run Code Online (Sandbox Code Playgroud)
我没有把真正的路径名称.它返回false,没有异常等.这可能是因为它们在同一台机器上?
编辑:原来我需要写权限才能将文件发送到ftpserver.默认情况下,它不会授予用户写入权限.如何使用Apache的ftpserver库为用户提供写入权限?
我正在尝试从Android手机(HTC Desire HD)上传图像到FTP服务器(在我的本地PC上).图像将进入FTP服务器,但它们已损坏.
并且方法(ftpClient.storeFile())抛出IOException(错误的文件号)
请帮我.
这是损坏的图像链接:
http://imageshack.us/photo/my-images/820/komikb.jpg/
这是代码:
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("192.168.2.14");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.setSoTimeout(10000);
ftpClient.enterLocalPassiveMode();
if(ftpClient.login("Administrator", "xxxx"))
{
File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
FileInputStream fs= new FileInputStream(sFile);
String fileName = sFile.getName();
Boolean result = ftpClient.storeFile("/ftpfile.atspace.co.uk/" + fileName, fs);
String has = "";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我在程序开始时打开了与FTP服务器的连接.
在我在服务器上执行操作之前,我想检查连接是否已成功建立.最简单的快速方式,如果连接消失,我会尝试再次连接.
我使用此代码执行此操作:
private boolean checkConnection()
{
try
{
boolean success = ftpClient.login(user_name, password);
if(success)
return true;
else
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个方法在连接关闭时抛出NullPointer异常.
我可以检查与之的连接,ftpClient.connect(server, port);
但这就像是要重新连接.
有什么方法可以检查连接?
ftpClient.connect
与没有活动ftp服务的现有主机一起使用时,超时仅在5分钟后发生,这太长了。
我尝试设置各种超时(setDefaultTimeout,setDataTimeout),但没有任何改变。
FtpClient
从SocketClient
那里继承有一个setConnectTiemout方法,但是当我使用java.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout
它时,运行它时会得到一个。这似乎是由于某些J2SW 1.2兼容性,如Commons-net FAQ中所述:
问:如何设置连接超时?http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions
他们建议SocketFactory
使用特定的超时从扩展的Socket类实现自己的创建对象。但是,当尝试使用时,ftpClient.setSocketFactory
我也会得到一个java.lang.NoSuchMethodError
。
有什么帮助我可以减少连接超时吗?
我创建了一个ftp客户端,它在白天连接了几次,从FTP服务器检索日志文件.
问题是几个小时后我收到来自FTP服务器的错误消息(达到-421会话限制..).当我检查与netstat的连接时,我可以看到几个'ESTABLISHED'连接到服务器,即使我已"关闭"连接.
当我尝试通过命令行或FileZilla执行相同操作时,连接已正确关闭.
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
Run Code Online (Sandbox Code Playgroud)
如何正确关闭/断开连接?我忘了什么吗?
我有一个方法从FTP服务器下载文件,它可以在较小的文件上正常工作,但是当我用它来下载~5GB大小的zip类型的文件时,它会下载它,但之后它什么也没做.当它达到下载的100%时,脚本不会继续.我是否应该等待下载完成后在后台实际执行某些操作?有文件大小限制吗?
const FTP = require('ftp')
Run Code Online (Sandbox Code Playgroud)
可以在npm找到
downloadFile: params => {
return new Promise((resolve, reject) => {
let ftpClient = new FTP()
let total = params.state.fileSize
let progress = 0
ftpClient.on('ready', _ => {
console.log(`Downloading ${params.targetedFile} ...`);
ftpClient.get(params.targetedFile, (err, stream) => {
if (err) reject(err)
stream.on('data', buffer => {
progress += buffer.length
process.stdout.write(`Progress: ${(progress/total*100).toFixed(2)}% (${progress}/${total}) \r`)
})
stream.once('close', _ => {
ftpClient.end()
console.log(`Saved downloaded file to ${params.localDir}`);
resolve(params.localDir)
})
stream.pipe(fs.createWriteStream(params.localDir))
})
})
ftpClient.connect(params.auth)
})
}
Run Code Online (Sandbox Code Playgroud)
基本上,stream.once('close', ...)
下载大文件时不会执行回调.并且它会针对相同类型的较小文件执行.
我正在尝试使用此代码从FTP服务器检索文件。
private class FtpTask extends AsyncTask<Void, Void, Long> {
protected Long doInBackground(Void... args) {
FTPClient FtpClient = new FTPClient();
int reply;
try {
FtpClient.connect(ftpServer);
FtpClient.login(ftpUser, ftpPass);
reply = FtpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
String remoteFileName = "ACPlus.ZIP";
String localFile = context.getFilesDir() + "/ACPLUS.ZIP";
// Delete local file first
File file = new File(localFile);
if(file.exists()) {
file.delete();
}
FtpClient.changeWorkingDirectory("/" + ftpFolder);
FtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
FtpClient.enterLocalPassiveMode();
OutputStream outputStream = null;
boolean success = false;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
success = FtpClient.retrieveFile(remoteFileName, outputStream); …
Run Code Online (Sandbox Code Playgroud) 在使用 .Net6 的 C# 中,我们可以使用 -object 与 FTP 服务器交互FtpWebRequest
。这效果很好。但是我们应该如何创建这个对象呢?目前我们使用WebRequest.Create()
并且微软说我们应该这样做。
但这个功能现在已经过时了。因此,当Create
-function 已过时但FtpWebRequest
-type 未过时时,FtpWebRequest
在不使用过时函数的情况下创建 -object 的正确且推荐的方法是什么?