FTP不在android下载文件

时间:2012-11-07 09:49:38

标签: android ftp

我已经使用commons-net-2.0-ftp.jar从ftp写入了读取数据下载文件的代码这个jar我可以读取数据 和contents.toString()得到完整的字符串但是当它保存在SD卡上时它只是空文件保存没有数据包含在这个文件中 .i是新的android devloper。可以任何人都有解决方案

public void onClicks(View v){

    FTPClient ftp=new FTPClient();
    try {
        //for FTP Host
        ftp.connect("********");
        ftp.login("*********", "********");
        ftp.enterLocalPassiveMode();
        String directory="/krishna/Test";
        ftp.changeWorkingDirectory(directory);
         final TextView ftpText = (TextView)findViewById(R.id.tvshow);
           StringBuffer contents = new StringBuffer();
        ftp.getReplyString();
         String[] names = ftp.listNames();

     for (String name : names) {
           BufferedReader reader = null;
             try{
                  String sCurrentLine;
                  InputStream stream = ftp.retrieveFileStream(name);
                    reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                    String remoteFile1 = name;
                    File downloadFile1 = new File("sdcard/"+name);
                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
                    InputStream inputStream = ftp.retrieveFileStream(remoteFile1);
                    byte[] bytesArray = new byte[4096];
                    int bytesRead = -1;
                    while ((sCurrentLine = reader.readLine()) != null) {
                        System.out.println(sCurrentLine);

                           contents.append(sCurrentLine)
                             .append(System.getProperty(
                                     "line.separator"));

                    }
                    boolean success = ftp.completePendingCommand();
                    }catch (Exception e){
                        //Catch exception if any
                        System.err.println("Error: " + e.getMessage());
                    }
         }
           ftpText.setText(contents.toString());
          contents.toString();
        ftp.logout();
        ftp.disconnect();
        Toast.makeText(this,"Connected FTP" , Toast.LENGTH_SHORT).show();
    }catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this,"Not Connected FTP" , Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this,"Not Connected FTP" , Toast.LENGTH_SHORT).show();
    }
    //this.

0 个答案:

没有答案