FTPClient下载文件失败,retrieveFile()方法replyCode = 550
/ *我在localhost上运行一个FTP服务器。当我下载文件时使用ftpClient.retrieveFile()方法,它的replyCode是550。 我读了commons-net的API并找到550 replyCode,定义是“public static final int FILE_UNAVAILABLE 550”。但我无法从我的代码中找到问题。
谢谢你的帮助。
* /
FTPClient ftpClient = new FTPClient(); FileOutputStream fos = null; try { ftpClient.connect("192.168.1.102",2121); ftpClient.login("myusername", "12345678"); ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); String remoteFileName = "ftpserver.zip";//this file in the rootdir fos = new FileOutputStream("f:/down.zip"); ftpClient.setBufferSize(1024); ftpClient.enterLocalPassiveMode(); ftpClient.enterLocalActiveMode(); ftpClient.retrieveFile(remoteFileName, fos); System.out.println("retrieveFile?"+ftpClient.getReplyCode()); fos.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTPexception", e); } }
我发现Apache retrieveFile(…)有时不能使用超过一定限制的文件大小。 为了克服这个问题,我会使用retrieveFileStream()代替。 在下载之前,我已经设置了Correct FileType并将Mode设置为PassiveMode
所以代码看起来像
.... ftpClientConnection.setFileType(FTP.BINARY_FILE_TYPE); ftpClientConnection.enterLocalPassiveMode(); ftpClientConnection.setAutodetectUTF8(true); //Create an InputStream to the File Data and use FileOutputStream to write it InputStream inputStream = ftpClientConnection.retrieveFileStream(ftpFile.getName()); FileOutputStream fileOutputStream = new FileOutputStream(directoryName + "/" + ftpFile.getName()); //Using org.apache.commons.io.IOUtils IOUtils.copy(inputStream, fileOutputStream); fileOutputStream.flush(); IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(inputStream); boolean commandOK = ftpClientConnection.completePendingCommand(); ....
FTP错误550未执行请求的操作。 文件不可用,找不到,无法访问
所以我认为enconding有点奇怪,我没有设置控制编码并使用retrieveFile只是在java中发送一个普通的String。 这一行:
ftpClient.retrieveFile(new String(remoteFileName.getBytes("ms932"),"ISO-8859-1"), fos);
什么都不做,因为你是从另一个字符串创建一个新的Java字符串。 Java字符串以不同的编码保存在内存中,如果我没有记错的话,与所有编码兼容。
此外,您使用的路径是错误的,请参阅:
String remoteFileName = "//ftpserver.zip";
Ftp将导致使用/启动路径时出错,请尝试以下操作:
"ftpserver.zip"
或者如果你有一个子目录,试试这个:
"subdir/myfile.zip"
干杯
最近我遇到了同样的错误,但主要是因为路径不正确,而不是在/data/csms/trt/file.txt中附加斜杠,它被附加为/data/csms/trtfile.txt …所以没有从所需位置检索文件。
需要在连接之前调用setControlEncoding,就像这样
[...] try { ftpClient.setControlEncoding("UTF-8"); ftpClient.connect("192.168.1.102",2121); ftpClient.login("myusername", "12345678"); [...]
看起来输出路径不正确。 检查服务器上的共享根目录。 如果root是f:\并且你的文件在这个根目录中,那么你只需要这样做:`fos = new FileOutputStream(“down.zip”);
如果您的文件位于根目录的子目录中,例如f:\ sub,那么它应该是fos = new FileOutputStream("sub\\down.zip");
我有同样的问题,因为我的SDCARD / PHONE内存没有空间。