无法在android中的FTP服务器上上传文件 [英] Unable to Upload file on FTP Server in android

查看:129
本文介绍了无法在android中的FTP服务器上上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  try {
String extStorageDirectory = Environment.getExternalStorageDirectory()。 toString
FTPClient ftpClient = new FTPClient();
ftpClient.connect(xxx.xxx.xx.xx);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
布尔状态= 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<!>登录状态== 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<上传状态== false

我想在新建目录 文件夹和文件名 AA.txt **,但它给出了上传状态为false 。**

是在服务器或我的代码中的问题?
请帮助!!!!!!!!!
在此先感谢

解决方案

试试我的代码如下:上传并下载歌曲服务器。我使用的是Apache的通用库。



请在下面的代码中对目录和文件名进行更改。



上传:

  public void goforIt(){


FTPClient con = null;

尝试
{
con = new FTPClient();
con.connect(192.168.2.57); (con.login(Administrator,KUjWbk))
{
con.enterLocalPassiveMode();

if //重要!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data =/sdcard/vivekm4a.m4a;

FileInputStream in = new FileInputStream(new File(data));
布尔结果= 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;

尝试
{
con = new FTPClient();
con.connect(192.168.2.57); (con.login(Administrator,KUjWbk))
{
con.enterLocalPassiveMode();

if //重要!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data =/sdcard/vivekm4a.m4a;

OutputStream out = new FileOutputStream(new File(data));
布尔结果= con.retrieveFile(vivekm4a.m4a,out);
out.close();
if(result)Log.v(下载结果,成功);
con.logout();
con.disconnect();

$ b $ catch(Exception e)
{
Log.v(下载结果,失败);
e.printStackTrace();
}



}


I am trying to Upload file like this

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();
        }

below is my logcat status

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

I want to Upload file on "New Directory" folder and file name AA.txt ** but it is giving Upload status false.**

Is the problem in server or in my Code??? Please help!!!!!!!!! Thanks in advance

解决方案

Try my code below, i used this to upload and download a song on the server. I am using the Apache's common lib.

Please make the changes for the directories and file name in the below code.

UPLOAD:

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();
        }






    }

DOWNLOAD:

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();
    }



}

这篇关于无法在android中的FTP服务器上上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆