1

在我的 android 项目中,我需要显示搜索栏和网络速度(文件使用 FTP 协议从服务器上传/下载的速度有多快)。在我的情况下,当我尝试从模拟器 sdcard 上传文件时,它会在文件存储在服务器中时显示速度和搜索栏,但我的要求是我需要在文件上传到时显示搜索栏和网络速度服务器。我不明白我在代码中哪里做错了。在显示 HTTP 的搜索栏和 TestAvarage 速度的附加图像中,我需要在 FTP 中实现以将文件上传到服务器。下面是我使用 ftp 将文件上传到远程服务器的代码:

在此处输入图像描述

提前致谢。

        @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;
    }

在此处输入图像描述

4

1 回答 1

0
   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);
        }
    }
});
于 2012-10-25T10:34:12.810 回答