gpt4 book ai didi

java - 无法在 Android 中的 FTP 服务器上上传文件

转载 作者:行者123 更新时间:2023-12-02 07:42:15 25 4
gpt4 key购买 nike

我正在尝试像这样上传文件

try {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString
FTPClient ftpClient = new FTPClient();
ftpClient.connect("xxx.xxx.xx.xx");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
boolean status=ftpClient.login("username", "password");
Log.d(TAG, "login status=="+status);
status=ftpClient.changeWorkingDirectory("New directory");
Log.d(TAG, "changeWorkingDirectory status=="+status);
status=ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
Log.d(TAG, "setFileType status=="+status);
ftpClient.enterLocalPassiveMode();
String srcFilePath=extStorageDirectory + "/AA.txt";
FileInputStream srcFileStream = new FileInputStream(new File(srcFilePath));
status=ftpClient.storeFile("AA.txt", srcFileStream);
Log.d(TAG, "upload status=="+status);
ftpClient.logout();
ftpClient.disconnect();
}
else
{
Log.d(TAG, "connectfail");
}

} catch (SocketException e) {
Log.d(TAG, "SocketException status=="+e.toString());
e.printStackTrace();
} catch (FileNotFoundException e) {
Log.d(TAG, "FileNotFoundException status=="+e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "IOException status=="+e.toString());
e.printStackTrace();
}

下面是我的 logcat 状态

07-11 12:24:43.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 138<!> login status==true
07-11 12:24:48.379: D/FTPDownloadDroid(10647): <!>com.ss.dr 141<!> changeWorkingDirectory status==true
07-11 12:24:48.859: D/FTPDownloadDroid(10647): <!>com.ss.dr 143<!> setFileType status==tr
07-11 12:24:54.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 150<!> upload status==false

我想将文件上传到“新目录”文件夹,文件名为AA.txt **,但它给出上传状态错误 .**

是服务器有问题还是我的代码有问题???请帮忙!!!!!!!!!提前致谢

最佳答案

尝试下面的代码,我用它在服务器上上传和下载歌曲。我正在使用 Apache 的公共(public)库。

请更改以下代码中的目录和文件名。

上传:

public void goforIt(){


FTPClient con = null;

try
{
con = new FTPClient();
con.connect("192.168.2.57");

if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";

FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/vivekm4a.m4a", in);
in.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}






}

下载:

public void goforIt(){
FTPClient con = null;

try
{
con = new FTPClient();
con.connect("192.168.2.57");

if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";

OutputStream out = new FileOutputStream(new File(data));
boolean result = con.retrieveFile("vivekm4a.m4a", out);
out.close();
if (result) Log.v("download result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
Log.v("download result","failed");
e.printStackTrace();
}



}

关于java - 无法在 Android 中的 FTP 服务器上上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427346/

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