gpt4 book ai didi

android - 无法从ftp android获取文件列表

转载 作者:行者123 更新时间:2023-11-29 21:30:30 24 4
gpt4 key购买 nike

我想连接 FTP 并从那里获取文件列表。这是我的代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


task getson = new task();
getson.execute();




}

FTPClient client;

public FTPClient connectWithFTP() throws IOException{
client = new FTPClient();
try {

client.connect(server, 21);
System.out.println("status :: " + client.getReplyString());
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new Exception("Connect failed: " + client.getReplyString());
}
try {
client.enterLocalPassiveMode();
if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
Log.v(getClass().toString(), "Setting binary file type failed.");
}
} catch(Exception e) {
e.printStackTrace();
}
checkFiles(client);
} catch (Exception e) {
System.out.println("status :: " + client.getReplyString());
}
return client;
}

private class task extends AsyncTask <Void, Void, FTPClient> {

@Override
protected void onPreExecute() {
// Do stuff before the operation
}


@Override
protected FTPClient doInBackground(Void... params) {
FTPClient ftp = null;
try {
ftp = connectWithFTP();
} catch (IOException e) {
e.printStackTrace();
}
return ftp;
}
}

public void disconnectWithFTP() throws IOException{
client.logout();
client.disconnect();
}

private void checkFiles(FTPClient clients){
try {
FTPFile[] ftpFiles = clients.listFiles();
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
Calendar date = ftpFiles[i].getTimestamp();
Log.v("aasd", name+", "+date);

}
} catch(Exception e) {
e.printStackTrace();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

连接工作,但是当我想获取文件列表时,我得到:

10-29 12:25:25.113: I/System.out(1980): status :: 220 (vsFTPd 2.2.2)
10-29 12:25:25.542: V/class com.example.ftp.MainActivity(1980): Setting binary file type failed.
10-29 12:25:36.332: W/System.err(1980): java.io.IOException: Unable to determine system type - response: 530 Please login with USER and PASS.
10-29 12:25:36.343: W/System.err(1980): at org.apache.commons.net.ftp.FTPClient.getSystemType(FTPClient.java:2721)
10-29 12:25:36.347: W/System.err(1980): at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3256)
10-29 12:25:36.355: W/System.err(1980): at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)
10-29 12:25:36.363: W/System.err(1980): at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2977)
10-29 12:25:36.371: W/System.err(1980): at com.example.ftp.MainActivity.checkFiles(MainActivity.java:87)
10-29 12:25:36.378: W/System.err(1980): at com.example.ftp.MainActivity.connectWithFTP(MainActivity.java:53)
10-29 12:25:36.382: W/System.err(1980): at com.example.ftp.MainActivity$task.doInBackground(MainActivity.java:72)
10-29 12:25:36.390: W/System.err(1980): at com.example.ftp.MainActivity$task.doInBackground(MainActivity.java:1)
10-29 12:25:36.398: W/System.err(1980): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-29 12:25:36.398: W/System.err(1980): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-29 12:25:36.398: W/System.err(1980): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-29 12:25:36.402: W/System.err(1980): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-29 12:25:36.402: W/System.err(1980): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-29 12:25:36.402: W/System.err(1980): at java.lang.Thread.run(Thread.java:1019)

这个服务器是开放的,没有登录名和密码。为什么我无法获取文件列表?

最佳答案

我解决了我的问题。这是解决方案:

client.login("anonymous", "anonymous");

关于android - 无法从ftp android获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657200/

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