前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android FTP上传文件

Android FTP上传文件

作者头像
码客说
发布2019-10-22 14:34:24
4.8K0
发布2019-10-22 14:34:24
举报
文章被收录于专栏:码客码客

前言

Android 上用 FTP上传文件 通常可以用以下的这两个Jar

  • commons-net
  • ftp4j

我这里就用第一种方式 第二种请参考通过FTP4J 实现FTP各种操作

使用方式

引用

代码语言:javascript
复制
//FTP
compile group: 'commons-net', name: 'commons-net', version: '3.5'

代码

代码语言:javascript
复制
class UploadTask extends AsyncTask<String, Object, Integer> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(String... params) {
        String ftp_url = "192.168.1.100";
        String ftp_name = "name";
        String ftp_pwd = "pwd";

        String fileName = params[0];

        String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String filePath = basePath + File.separator + "caiyun" + File.separator + fileName;
        String remotePath = File.separator + fileName.substring(0, 2);
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis;
        int returnMessage = 0;
        try {
            ftpClient.connect(ftp_url, 21);
            boolean loginResult = ftpClient.login(ftp_name, ftp_pwd);
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.makeDirectory(remotePath);
                // 设置上传目录
                ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                fis = new FileInputStream(filePath);

                //不计算进度条
                //ftpClient.storeFile(fileName, fis);

                //计算进度条
                int n = -1;
                long pContentLength = fis.available();
                long trans = 0;
                int bufferSize = ftpClient.getBufferSize();
                byte[] buffer = new byte[bufferSize];
                OutputStream outputstream = ftpClient.storeFileStream(new String(fileName.getBytes("utf-8"), "iso-8859-1"));
                while ((n = fis.read(buffer)) != -1) {
                    outputstream.write(buffer, 0, n);
                    trans += n;
                    //trans已传输字节  pContentLength总字节
                    publishProgress(trans, pContentLength);
                }
                fis.close();
                outputstream.flush();
                outputstream.close();

                returnMessage = 1;   //上传成功
            } else {// 如果登录失败
                returnMessage = 0;
            }


        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
        return returnMessage;
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (result == 1) {
            //上传成功后调用
        }

    }

    @Override
    protected void onProgressUpdate(Object... values) {
        super.onProgressUpdate(values);
        //获取进度
        long trans = (long) values[0];
        long pContentLength = (long) values[1];
        int progress = (int) (trans * 100 / pContentLength);
        
    }
}

调用方式

代码语言:javascript
复制
new UploadTask("123.jpg").execute();
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 使用方式
    • 代码
      • 调用方式
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档