我是Android的新手.我正在尝试使用Apache Commons FTPClient将文件从ftp服务器下载到SD卡.行 InputStream input = client.retrieveFileStream("/"+ fileName); 始终返回null.但该文件位于Ftp位置.请帮助我知道错误在哪里.
我在清单中设置了以下权限; android:name ="android.permission.INTERNET"和android:name ="android.permission.WRITE_EXTERNAL_STORAGE"
我的守则
private static void downLoad(){
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("ftp.doamin.com");
client.login("8888", "8888");
String filePath = "/mnt/sdcard/download/CheckboxHTML.txt" ;
String fileName = "CheckboxHTML.txt";
fos = new FileOutputStream(filePath);
InputStream input = client.retrieveFileStream("/" + fileName);
byte[] data = new byte[1024];
int count = input.read(data);
while ((count = input.read(data)) != -1) {
fos.write(data, 0, count);
}
fos.close();
if(!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
} …Run Code Online (Sandbox Code Playgroud) 我收到了错误消息:
从ftp服务器下载文件时出错:ftp://speedtest.tele2.net:IPv6地址无效
从这条线:
String serverAddress = "ftp://speedtest.tele2.net";
FTPClient ftp = new FTPClient();
ftp.connect(serverAddress);
Run Code Online (Sandbox Code Playgroud)
我不明白该怎么办?
我得到的唯一建议是关于这个系统属性,但这并没有解决问题.
java.net.preferIPv4Stack -> true
java.net.preferIPv6Addresses -> true
Run Code Online (Sandbox Code Playgroud)
有人得到建议或推荐吗?非常感谢你!
我使用 Apache Common NetFTPClient并在上传文件之前使用如下所示的方法进行设置ftpClient。
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用 JSch。文件上传之前是否需要相同的设置以及它们的外观?谢谢。
我正在尝试将文件从我的Java应用程序传输到FTP服务器程序正常工作,文件被传输,但是当我在FTO文件夹中打开时,文件已损坏,我认为文件在文件丢失期间丢失传递.为什么?我该如何解决这个问题?
另一个问题,while如果我想停止文件上传,我怎么能停止?
谢谢大家!
我班里的代码:
FTPClient client = new FTPClient();
InputStream is = null;
//...
try{
client.connect(MY_FTP_URL);
client.login(USER, PASS);
InputStream is = new FileInputStream(file_path);
OutputStream os = client.storeFileStream(file_name);
byte[] buffer = new byte[1024];
int len;
//I use this way to check the transfer progress
while((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
os.flush();
}
os.close();
} catch (IOException e){
e.printStackTrace();
} finally{
try{
if(is != null){
is.close();
}
client.disconnect();
} catch(IOException e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个 grails 应用程序,它应该通过 FTP 将一堆图像上传到服务器。为此,我正在使用 commons-net。奇怪的是,如果我为每个文件创建一个新连接,它可以正常工作,但是如果我连接过一次然后开始发送文件,文件就会损坏!下面是我的代码,它可以工作,但我不想为每个文件创建一个新连接:
filesList.each{ f->
String ftpUser = ConfigurationHolder.config.ftp.user
String ftpPassword = ConfigurationHolder.config.ftp.password
String ftpHost = ConfigurationHolder.config.ftp.host
log.debug "ftp> ${ftpUser}@${ftpHost}"
JakartaFtpWrapper ftp = new JakartaFtpWrapper();
ftp.connectAndLogin(ftpHost, ftpUser, ftpPassword)
ftp.setDataTimeout(1000*60*60*5)
log.debug "Welcome message[${ftp.getReplyString()}]"
log.debug "Current Directory[${ftp.printWorkingDirectory()}]";
log.debug "remote dir[${remoteDir}]"
ftp.makeDirectory(remoteDir)
ftp.cwd(remoteDir)
log.debug "uploading file path[${f}]..."
ftp.binary()
ftp.enterLocalPassiveMode()
def input = new FileInputStream(f.getAbsolutePath());
OutputStream output = ftp.storeFileStream(f.getName())
Util.copyStream(input, output);
output.flush()
input.close();
output.close();
ftp.logout();
ftp.disconnect();
}
Run Code Online (Sandbox Code Playgroud)
如果我从每个中删除连接,图像就会损坏!我在这里做错了吗?
编辑**:这个不起作用:
String ftpUser = ConfigurationHolder.config.malibu.ftp.user
String ftpPassword = ConfigurationHolder.config.malibu.ftp.password
String ftpHost = ConfigurationHolder.config.malibu.ftp.host …Run Code Online (Sandbox Code Playgroud) 我正在用 Java 开发一个应用程序,该应用程序必须从服务器下载一些非常大的文件到客户端。到目前为止,我正在使用 apache commons-net:
FileOutputStream out = new FileOutputStream(file);
client.retrieveFile(filename, out);
Run Code Online (Sandbox Code Playgroud)
在客户端完成下载文件之前,连接通常会失败。我需要一种方法来从连接失败的点恢复文件的下载,而无需再次下载整个文件,这可能吗?
我在使用 commons-net FTPClient 时遇到问题。如果我从我的 ftp 服务器上使用retrieveFileStream() 下载文件,它可以工作,但我得到的结果是“150 Opening BINARY mode data connection for ...”。如果我调用 noop() 我会得到“226 传输完成”结果。对于接下来的每个操作,我都会得到前一个操作的结果。
我发现,FTPClient 读取结果直到行尾,如果有两个结果行(如在retrieveFileStream()之后),我会在下一个命令之后得到第二个结果行。我通过重写 FTPClient.retrieveFileStream() 做了一个解决方法,如下所示:
public static void main(String[] args){
try {
MyFTPClient ftpClient = new MyFTPClient();
try {
ftpClient.connect(ftphost, 21);
if(!ftpClient.login( ftpuser, ftppassword )){
throw new RuntimeException(ftpClient.getReplyString());
}
if(!ftpClient.changeWorkingDirectory("in")){
throw new RuntimeException(ftpClient.getReplyString());
}
FTPFile[] files = ftpClient.listFiles();
for(FTPFile file: files){
if(file.getName().startsWith(FILENAME) && (file.getType() == FTPFile.FILE_TYPE)){
InputStream in = ftpClient.retrieveFileStream(file.getName());
CsvFile csvFile = new CsvFile(in, "ISO-8859-1", ';', "yyyyMMdd", "#.00", Locale.US, false);
in.close();
in = …Run Code Online (Sandbox Code Playgroud) 我有ftp端口: ftp://173.201.0.1/
我试图通过以下方式连接它:
String Ftp_Path = "ftp://173.201.0.1/";
public List<String> GetFileList()
{
String ftpServerIP = Ftp_Path;
String ftpUserID = Ftp_UserName;
String ftpPassword = Ftp_Password;
FTPFile[] downloadFiles = null;
StringBuilder result = new StringBuilder();
FTPClient ftp = new FTPClient();
List<String> xlsFiles = null;
try {
ftp.connect(Ftp_Path);
ftp.login(ftpUserID, ftpPassword);
downloadFiles=ftp.listFiles();
xlsFiles = new ArrayList<String>();
for(FTPFile i : downloadFiles) {
if(i.toString().endsWith(".xls")) {
xlsFiles.add(i.toString());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xlsFiles;
}
Run Code Online (Sandbox Code Playgroud)
但我在网上得到错误:
ftp.connect(Ftp_Path);
Run Code Online (Sandbox Code Playgroud)
以下是错误.
java.net.UnknownHostException: …Run Code Online (Sandbox Code Playgroud) private static void getFTPFileProperties(FTPClient client,
String ftpLocation, String pattern) throws IOException {
FTPFile[] fileList=null;
fileList = client.listFiles();
for(int i=0;i<fileList.length;i++)
{
FTPFile file= fileList[0];
Calendar cal = file.getTimestamp();
DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormater.format(cal.getTime()));
}
}
Run Code Online (Sandbox Code Playgroud)
我已经编写了上述函数来检索文件详细信息。但不知何故,我正在检索文件中没有秒部分的细节。我检索lastModifiedDate的2013-08-08 00:00:00地方作为其实际lastModifiedDate就是2013-08-08 12:53:27 PM
我正在尝试使用 Camel FTP Producer 将文件发送到第三方 ftp 服务器(由亚马逊托管,它会出现),并且遇到了Writing File failed with: File operation failed... Host attempting data connection x.x.x.x is not the same as server y.y.y.y我以前从未见过的问题。
生产者被配置为被动模式,根据TRACE级别的日志,这是启用的。(虽然错误消息听起来更像是与活动模式问题有关)
该y.y.y.yIP地址是那些NSLOOKUP目标域上市之一,所以该位才有意义。但是,该x.x.x.xIP 与不同的 Amazon 托管服务器有关,因此我认为已经执行了某种切换或负载平衡,而 FTP 客户端不喜欢那样。
是否有某种方法可以配置 Camel FTP 以允许这样做(我假设这是一个安全功能),还是被动模式应该允许这样做?
我对 ftp 服务器提供商没有影响,所以很遗憾,除了我的客户端选项之外,我无法更改任何内容。
感谢您的关注!