gpt4 book ai didi

java - 为什么 printWorkingDirectory() 仅返回根目录?

转载 作者:行者123 更新时间:2023-12-02 07:03:54 26 4
gpt4 key购买 nike

我正在尝试使用 Apache Commons FTP 中的 printWorkingDirectory(),但它只返回“/”根目录,无论我实际位于哪个目录。我可以导航目录、列出文件等。当我上传文件上传到 ftp 时,无论上传文件时我位于哪个目录,它都会上传到根目录。当我长按本地文件时,它会触发上传对话框。

    public boolean ftpConnect(String host, String username, String password,
int port) {
try {
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host, port);

// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// login using username & password
boolean status = mFTPClient.login(username, password);

/*
* Set File Transfer Mode
*
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
* transferring text, image, and compressed files.
*/
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();

ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
return status;
}
} catch (Exception e) {
// Log.d(TAG, "Error: could not connect to host " + host );
}

return false;
}

public boolean ftpDisconnect() {
try {
mFTPClient.logout();
mFTPClient.disconnect();
return true;
} catch (Exception e) {
// Log.d(TAG,
// "Error occurred while disconnecting from ftp server.");
}

return false;
}

public String ftpGetCurrentWorkingDirectory() {
try {
String workingDir = mFTPClient.printWorkingDirectory();
return workingDir;
} catch (Exception e) {
Log.i("Error working dir", e.toString());
}

return null;
}

public boolean ftpChangeDirectory(String directory_path) {
try {
if (mFTPClient.changeWorkingDirectory(directory_path))
return true;
} catch (Exception e) {
Log.i("directory path", directory_path);
}

return false;
}

public boolean ftpUpload(String srcFilePath, String desFileName,
String desDirectory) {
boolean status = false;
try {
FileInputStream srcFileStream = new FileInputStream(srcFilePath);

// change working directory to the destination directory
if (ftpChangeDirectory(desDirectory)) {
status = mFTPClient.storeFile(desFileName, srcFileStream);
}
showServerReply(mFTPClient);
srcFileStream.close();
return status;
} catch (Exception e) {
Log.i("Error", e.toString());
}

return status;
}

public void l_Show_Alert_box(Context context, String message, int position) {
final String selectedFileString = l_directoryEntries.get(position)
.getText();
final File tmpFile = new File(selectedFileString);
final String fname = tmpFile.getName();
String btn;
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.create();

if (tmpFile.isDirectory()) {
btn = "Make Directory";
alertDialog.setIcon(R.drawable.newfolder);
alertDialog.setTitle(getString(R.string.mkdir));

alertDialog.setButton(btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ftpMakeDirectory("/" + fname);
map.clear();
r_directoryEntries.clear();
r_itla.notifyDataSetChanged();
ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
return;
}

});
} else {
btn = "Upload";
alertDialog.setIcon(R.drawable.upload);
alertDialog.setTitle(getString(R.string.upload));
alertDialog.setButton(btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String cd;
try {
cd = mFTPClient.printWorkingDirectory().toString(); // Debug shows the working dir as being "/"
//ftpUpload(currenLocalFile.getPath(), "test.txt", cd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

map.clear();
r_directoryEntries.clear();
r_itla.notifyDataSetChanged();
ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
return;
}

});
}
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});

alertDialog.setMessage(message);
alertDialog.show();
}


}

为什么 printWorkingDirectory() 没有返回正确的目录?

最佳答案

您需要调用changeWorkingDirectory(String pathname)方法来更改工作目录,否则它将保留在根目录中。

关于java - 为什么 printWorkingDirectory() 仅返回根目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16328577/

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