android ftp上传 下载

new Thread(){
            @Override
            public void run() {
                try {
                    FTPClient ftpClient = new FTPClient();
                    FileInputStream fis;
                    ftpClient.setConnectTimeout(3000);
                    if(p.getString("ip2","") == "" || p.getString("port2","") == ""){
                        handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_4);
                        return;
                    }
                    ftpClient.connect(p.getString("ip2",""), Integer.parseInt(p.getString("port2","")));
                    boolean isLogin = ftpClient.login(p.getString("ftpuser",""), p.getString("ftppwd",""));
                    int replyCode = ftpClient.getReplyCode();
                    ftpClient.enterLocalPassiveMode();
                    if (isLogin && FTPReply.isPositiveCompletion(replyCode)) {
                        if(ftpClient.changeWorkingDirectory("/ypwj/"+ PowerBean.area_id+"/")){

                        }else{
                            ftpClient.makeDirectory("/ypwj/"+PowerBean.area_id+"/");
                            ftpClient.changeWorkingDirectory("/ypwj/"+PowerBean.area_id+"/");
                        }


                        File d = new File(path);
                        //ftpClient.setBufferSize(1024);
                        //ftpClient.setControlEncoding("UTF-8");
                       // ftpClient.setControlEncoding("GBK");
                        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                        ftpClient.enterLocalPassiveMode();
                        fis = new FileInputStream(d);
                        //fileName = "1111.mp3";

                        String fn = new String(fileName.getBytes("GBK"),"iso-8859-1");
                        ftpClient.storeFile(fn, fis);
                        Log.v("ccc","成功");

                        ftpClient.logout();
                        ftpClient.disconnect();
                        handler.sendEmptyMessage(CodeList.UPLOAD_SUCCESS);
                    }else{
                        handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_5);
                    }

                } catch (IOException e) {
                    handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_4);
                    e.printStackTrace();
                }
            }
        }.start();
new Thread() {
                @Override
                public void run() {
                    try {
                        FTPClient ftpClient = new FTPClient();
                        FileInputStream fis;
                        ftpClient.setConnectTimeout(3000);
                        if (p.getString("ip2", "") == "" || p.getString("port2", "") == "") {
                            handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_4);
                            return;
                        }
                        ftpClient.connect(p.getString("ip2", ""), Integer.parseInt(p.getString("port2", "")));
                        boolean isLogin = ftpClient.login(p.getString("ftpuser", ""), p.getString("ftppwd", ""));
                        int replyCode = ftpClient.getReplyCode();
                        ftpClient.enterLocalPassiveMode();
                        if (isLogin && FTPReply.isPositiveCompletion(replyCode)) {
                            String p = arr.get(fileCurrent).getPath();

                            ftpClient.changeWorkingDirectory(p.substring(0, p.lastIndexOf("/") + 1));
//                            ftpClient.changeWorkingDirectory("/ypwj/");
                            ftpClient.setControlEncoding("GBK");
                            // 得到FTP当前目录下所有文件
                            FTPFile[] ftpFiles = ftpClient.listFiles();
                            // 循环遍历
                            boolean flag = false;
                            for (FTPFile ftpFile : ftpFiles) {

                                // 找到需要下载的文件
                                if (ftpFile.getName().equals(arr.get(fileCurrent).getName())) {
                                    ftpClient.enterLocalPassiveMode();
                                    // Log.v("ccc",arr.get(fileCurrent).getName());
                                    // 创建本地目录
                                    File f = new File(Environment.getExternalStorageDirectory(), "sounds");
                                    File d = new File(f, arr.get(fileCurrent).getName());

                                    // 创建输出流
                                    OutputStream outputStream = new FileOutputStream(d);
                                    // 下载单个文件
                                    flag = ftpClient.retrieveFile(new String(ftpFile.getName().getBytes("GB2312"), "ISO-8859-1"), outputStream);
                                    // 关闭文件流
                                    outputStream.close();
                                    flag = true;
                                    break;
                                }
                            }
                            if (flag) {

                                handler.sendEmptyMessage(CodeList.FTP_DOWNLOAD_SUCCESS);
                            } else {
                                handler.sendEmptyMessage(CodeList.FTP_DOWNLOAD_ERROR);
                            }
                        } else {
                            handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_5);
                        }

                    } catch (IOException e) {
                        handler.sendEmptyMessage(CodeList.DATA_PARSING_FAILURE_4);
                        e.printStackTrace();
                    }
                }
            }.start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值