从安卓手机上传文件到FTP服务器? [英] Uploading a file to a FTP server from android phone?

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

问题描述

以下是假设创建文本文档并将其上传到我的 FTP 服务器的代码.出于某种原因,它似乎不起作用.我习惯了

Following is the code that's suppose to create a text document and upload it to my FTP server. For some reason it doesn't seem to work. I used to the libraries provided at

http://lavalatwork.blogspot.tw/2010/09/using-apache-commons-ftp-library-in.html

用于与 FTP 服务器通信.

for communicating with the FTP server.

try
    {
        final String testString = new String("Hello");
        FileOutputStream fOut = openFileOutput("samplefile.txt",
                MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut); 

        osw.write(testString);
        osw.flush();
        osw.close();
    }


    catch(IOException ex)
    {

    }


    FTPClient mFTP = new FTPClient();
    try {
        // Connect to FTP Server
        mFTP.connect("192.168.10.101");
        //mFTP.login("user", "password");
        mFTP.setFileType(FTP.BINARY_FILE_TYPE);
        mFTP.enterLocalPassiveMode();

        // Prepare file to be uploaded to FTP Server
        File file = new File(getFileStreamPath("samplefile.txt")+ "");
        FileInputStream ifile = new FileInputStream(file);

        // Upload file to FTP Server
        mFTP.storeFile("filetotranfer",ifile);
        mFTP.disconnect();          
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

任何帮助将不胜感激.

推荐答案

看到这个......这将帮助您纠正代码中的问题.

See this...... This will help you rectify the probs in your code.

我已经使用 apache 的公共库 向服务器上传和下载音频文件...看到这个...

I have used the apache's commons library to upload and download an Audio file to and from the Server... see this...

上传:

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



}

这篇关于从安卓手机上传文件到FTP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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