Android的FTP文件上传,然后显示搜索栏和放大器;网络速度地位问题放大器、文件上传、地位、速度

2023-09-06 11:44:21 作者:請允許珴尐尐旳驕傲

在我的Andr​​oid项目我需要显示搜索栏和网络速度(与文件多少速度上传/使用FTP协议从服务器上下载)。在我的情况,当我尝试上载从模拟器SD卡文件,然后它显示当文件被存储在服务器末的速度和搜索栏,但我的要求是我需要显示sheekbar和网络速度,而该文件被上传到服务器。我不明白的地方我做错了我的code。在HTTP连接的此图像显示搜索栏和TestAvarage速度是罚款这样的,我需要的FTP实现上传文件到服务器。下面是使用FTP远程服务器进行文件上传我的code:

先谢谢了。

  @覆盖    保护字符串doInBackground(字符串...为arg0){        诠释计数= 0;        FTPClient ObjFtpCon =新FTPClient();        //Toast.makeText(conFTPasync doInBackground()被称之为,Toast.LENGTH_SHORT).show();        尝试{            runOnUiThread(新的Runnable(){                公共无效的run(){                    bar.setProgress(0);                    //real_time.setText(0 +秒);                    //test_avg.setText(0+kbps的);                    //peak.setText(0+kbps的);                }            });            的updateUI(PP,R.drawable.pause);            //ObjFtpCon.connect(\"ftp.customhdclips.com);            ObjFtpCon.connect(FTP+ map.get(URL)的toString());            的updateUI(状态,连接);            //如果(ObjFtpCon.login(fstech@customhdclips.com,fstech123)){            如果(ObjFtpCon.login(map.get(用户名)。的toString(),map.get(密码)。的toString())){                的updateUI(状态,连接);                //吐司(连接到FTP服务器:ftp.customhdclips.com);                ObjFtpCon.enterLocalPassiveMode(); //重要!                ObjFtpCon.cwd(/); //发送FTP CWD命令到服务器,接收回复,并返回应答code。                //如果(模式== 0){                如果(的Integer.parseInt((map.get(oprn)。的toString()))== 0){                    // 下载                    的System.out.println(下载测试被称为);                    文件OBJFILE =新的文件(Environment.getExternalStorageDirectory()                            +文件分割符+/logo.png);                    / * *吐司(下载/logo.png);吐司(文件大小                     * + objfile.getTotalSpace()+字节); * /                    objfile.createNewFile();                    FileOutputStream中objFos =新的FileOutputStream(OBJFILE);                    布尔blnresult = ObjFtpCon.retrieveFile(/ logo.png                            objFos);                    objFos.close();                    如果(blnresult){                        //吐司(下载成功);                        //吐司(在保存:+                        // objfile.getAbsolutePath());                    }                    // ************ ***********                    / *                    文件OBJFILE =新的文件(                            Environment.getExternalStorageDirectory()                                    +文件分割符+/test.txt);                    //的System.out.println(总+ objfile.getTotalSpace()+字节);                    objfile.createNewFile();                    FileOutputStream中objFos =新的FileOutputStream(OBJFILE);                    布尔blnresult = ObjFtpCon.retrieveFile(/的test.txt                            objFos);                    objFos.close();                    如果(blnresult){                        的System.out.println(下载的FTP成功);                        //吐司(下载成功);                        //吐司(在保存:+                        // objfile.getAbsolutePath());                    } * /                }                其他{                      ObjFtpCon.connect(FTP+ map.get(URL)的toString());                        的updateUI(状态,连接);                        ObjFtpCon.login(map.get(用户名)的toString(),map.get(密码)的toString());                        ObjFtpCon.enterLocalPassiveMode();                        ObjFtpCon.cwd(在/ var / www / html等/ BevdogAnd);                        ObjFtpCon.setFileType(FTP.BINARY_FILE_TYPE);                        最终长开始= System.currentTimeMillis的();                        长sleepingTime = 0;                        字符串sourceFileUri = extStorageDirectory +/ zkfile+文件名;                        文件secondLocalFile =新的文件(sourceFileUri);                          长档案大小= secondLocalFile.length();                          INT sentBytes = 0;                        的InputStream的InputStream =新的FileInputStream(secondLocalFile);                            的System.out.println(开始上传第二个文件);                            为OutputStream的OutputStream = ObjFtpCon.storeFileStream(文件名);                            字节[] = bytesIn新的字节[512];                            INT读= 0;                            而((读= inputStream.read(bytesIn))!= -1){                                的updateUI(状态,上传);                                outputStream.write(bytesIn,0,读);                                sentBytes + =读;                                最终诠释进度=(INT)((sentBytes * 100)/档案大小);                                最终长速度= sentBytes;                                持续时间=((System.currentTimeMillis的() - 启动)-sleepingTime)/ 1000;                                runOnUiThread(新的Runnable(){                                    公共无效的run(){                                        bar.setProgress(进度);                                        如果(时间!= 0){                                            //test_avg.setText((((speed /持续时间)* 1000)* 0.0078125)+kbps的);                                            test_avg.setText((速度/时间)/ 1024 +kbps的);                                            如果(PK< =(速度/时间)/ 1024){                                                PK =(速度/时间)/ 1024;                                            }                                            / *如果(PK< =((速度/时间)* 1000)* 0.0078125){                                                PK =(长)(((速度/时间)* 1000)* 0.0078125);                                            } * /                                            //peak.setText(pk +kbps的);                                        }                                    }                                });                            }                            inputStream.close();                            outputStream.close();                            布尔完成= ObjFtpCon.completePendingCommand();                            的updateUI(状态,已完成);                            如果(完成){                            }                }            }            / * ------------------------------------------------ ------------- * /            / *            网址URL =新的URL(map.get(URL)的toString());            URLConnection的conexion = url.openConnection();            conexion.connect();            的updateUI(状态,连接);            最终诠释lenghtOfFile = conexion.getContentLength();            InputStream的输入=新的BufferedInputStream(url.openStream());            OutputStream的输出=新的FileOutputStream(                            Environment.getExternalStorageDirectory()                            +文件分割符                            + Info.getInfo(CON).HTTP_DOWNLOAD_FILE_NAME);            字节的数据[] =新的字节[1024];            总长= 0;            最终长开始= System.currentTimeMillis的();            长sleepingTime = 0;            的System.out.println(开始时间 - +启动);            的updateUI(状态,下载);            而((计数= input.read(数据))!=  -  1){                而(睡眠){                    视频下载(1000);                    sleepingTime + = 1000;                }                总+ =计数;                最终诠释进度=(INT)((总* 100)/ lenghtOfFile);                最终长速度=总;                持续时间=((System.currentTimeMillis的() - 启动)-sleepingTime)/ 1000;                runOnUiThread(新的Runnable(){                    公共无效的run(){                        bar.setProgress(进度);             * /            / * ------------------------------------------------ ----------------------- * /            其他{                的System.out.println(输入的密码不正确);                //Toast.makeText(con,用户名和/或密码不正确,Toast.LENGTH_SHORT).show();            }        }        赶上(例外五){            e.printStackTrace();            //吐司(e.getLocalizedMessage());        }        尝试{            ObjFtpCon.logout();            ObjFtpCon.disconnect();        }        赶上(IOException异常五){            e.printStackTrace();            //吐司(e.getLocalizedMessage());        }        返回null;    } 

解决方案

  mSeekBar.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){    @覆盖    公共无效onStopTrackingTouch(搜索栏搜索栏){        // TODO自动生成方法存根    }    @覆盖    公共无效onStartTrackingTouch(搜索栏搜索栏){        // TODO自动生成方法存根    }    @覆盖    公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){        // TODO自动生成方法存根        如果(FROMUSER){            mPlayer.seekTo(进度);            mSeekBar.setProgress(进度);        }    }}); 
android ftp上传文件 没网盘咋传输文件 全平台高效传输方案一网打尽

In my android project i need to display seekbar and network speed(With how much speed the file is Uploading/Downloading from server using FTP Protocol). In my situation when i try to upload a file from emulator sdcard then it's showing the speed and seekbar at the end when the file is stored in the server, but my requirement is i need to show sheekbar and network speed while the file is uploading into server. I am not understand where i did wrong in my code. In this attached image showing seekbar and TestAvarage speed for HTTP is fine like this i need to implement in FTP for upload a file to server. Below is my code for file upload using ftp to remote server:

Thanks In Advance.

        @Override
    protected String doInBackground(String... arg0) {
        int count = 0;
        FTPClient ObjFtpCon = new FTPClient();
        //Toast.makeText(con, "FTPasync doInBackground() is called" ,Toast.LENGTH_SHORT).show();
        try {
            runOnUiThread(new Runnable() {
                public void run() {

                    bar.setProgress(0);
                    //real_time.setText(0 + " secs");
                    //test_avg.setText(0+ " kbps");                     
                    //peak.setText(0+" kbps");
                }
            });
            updateUI(pp, R.drawable.pause);
            //ObjFtpCon.connect("ftp.customhdclips.com");
            ObjFtpCon.connect("ftp."+map.get("url").toString());
            updateUI(status, "Connecting");

            //if (ObjFtpCon.login("fstech@customhdclips.com", "fstech123")) {
            if (ObjFtpCon.login(map.get("username").toString(), map.get("password").toString())) {  
                updateUI(status, "Connected");
                // toast("Connected to FTP Server : ftp.customhdclips.com");
                ObjFtpCon.enterLocalPassiveMode(); // important!
                ObjFtpCon.cwd("/");// to send the FTP CWD command to the server, receive the reply, and return the reply code. 


                //if (mode == 0) {
                if(Integer.parseInt((map.get("oprn").toString()))== 0){ 
                    // Download
                    System.out.println("download test is called");
                    File objfile = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "/logo.png");

                    /* * toast("Downloading /logo.png"); toast("File Size : "
                     * + objfile.getTotalSpace() + " bytes");*/

                    objfile.createNewFile();
                    FileOutputStream objFos = new FileOutputStream(objfile);
                    boolean blnresult = ObjFtpCon.retrieveFile("/logo.png",
                            objFos);
                    objFos.close();
                    if (blnresult) {
                        // toast("Download succeeded");
                        // toast("Stored at : " +
                        // objfile.getAbsolutePath());
                    }




                    //***********************************************************


                    /*
                    File objfile = new File(
                            Environment.getExternalStorageDirectory()
                                    + File.separator + "/test.txt");

                    // System.out.println("total" + objfile.getTotalSpace() + " bytes");

                    objfile.createNewFile();
                    FileOutputStream objFos = new FileOutputStream(objfile);
                    boolean blnresult = ObjFtpCon.retrieveFile("/test.txt",
                            objFos);
                    objFos.close();
                    if (blnresult) {
                        System.out.println("download in ftp is successful");
                        // toast("Download succeeded");
                        // toast("Stored at : " +
                        // objfile.getAbsolutePath());
                    }*/


                } 

                else {
                      ObjFtpCon.connect("ftp."+map.get("url").toString());
                        updateUI(status, "Connecting");

                        ObjFtpCon.login(map.get("username").toString(), map.get("password").toString());
                        ObjFtpCon.enterLocalPassiveMode();
                        ObjFtpCon.cwd("/var/www/html/BevdogAnd");                                

                        ObjFtpCon.setFileType(FTP.BINARY_FILE_TYPE);

                        final long started = System.currentTimeMillis();
                        long sleepingTime= 0;
                        String sourceFileUri =extStorageDirectory+"/zkfile"+filename;
                        File secondLocalFile = new File(sourceFileUri);
                          long fileSize = secondLocalFile.length();
                          int sentBytes = 0;
                        InputStream inputStream = new FileInputStream(secondLocalFile);    

                            System.out.println("Start uploading second file");
                            OutputStream outputStream = ObjFtpCon.storeFileStream(filename);
                            byte[] bytesIn = new byte[512];
                            int read = 0;                           
                            while ((read = inputStream.read(bytesIn)) != -1) {
                                updateUI(status, "Uploading");
                                outputStream.write(bytesIn, 0, read);
                                sentBytes+=read;
                                final int progress = (int) ((sentBytes * 100) / fileSize);
                                final long speed = sentBytes;

                                duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
                                runOnUiThread(new Runnable() {
                                    public void run() {

                                        bar.setProgress(progress);
                                        if (duration != 0) {
                                            //test_avg.setText((((speed / duration)*1000)*0.0078125) + " kbps");
                                            test_avg.setText((speed / duration) / 1024 + " kbps");
                                            if (pk <= (speed / duration) / 1024) {
                                                pk = (speed / duration) / 1024;
                                            }
                                            /*if (pk <= ((speed / duration)*1000)*0.0078125) {
                                                pk = (long)(((speed / duration)*1000)*0.0078125);
                                            }*/
                                            //peak.setText(pk + " kbps");

                                        }
                                    }
                                });
                            }
                            inputStream.close();
                            outputStream.close();

                            boolean completed = ObjFtpCon.completePendingCommand();
                            updateUI(status, "Completed");
                            if (completed) {

                            }    
                }
            }

            /*-------------------------------------------------------------*/
            /*
            URL url = new URL(map.get("url").toString());
            URLConnection conexion = url.openConnection();
            conexion.connect();
            updateUI(status, "Connected");

            final int lenghtOfFile = conexion.getContentLength();
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(
                            Environment.getExternalStorageDirectory()
                            + File.separator
                            + Info.getInfo(con).HTTP_DOWNLOAD_FILE_NAME);
            byte data[] = new byte[1024];
            long total = 0;
            final long started = System.currentTimeMillis();
            long sleepingTime= 0;
            System.out.println("started time --"+started);
            updateUI(status, "Downloading");
            while ((count = input.read(data)) != -1) {
                while (sleep) {
                    Thread.sleep(1000);
                    sleepingTime +=1000;
                }
                total += count;
                final int progress = (int) ((total * 100) / lenghtOfFile);
                final long speed = total;
                duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
                runOnUiThread(new Runnable() {
                    public void run() {
                        bar.setProgress(progress);

             */



            /*-----------------------------------------------------------------------*/


            else{
                System.out.println("password entered is incorrect");
                //Toast.makeText(con, "Username or/and password is incorrect", Toast.LENGTH_SHORT).show();
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
            // toast(e.getLocalizedMessage());
        }

        try {
            ObjFtpCon.logout();
            ObjFtpCon.disconnect();
        } 
        catch (IOException e) {
            e.printStackTrace();
            // toast(e.getLocalizedMessage());
        }
        return null;
    }

解决方案

   mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // TODO Auto-generated method stub
        if(fromUser){
            mPlayer.seekTo(progress);
            mSeekBar.setProgress(progress);
        }
    }
});