2016-08-13 16 views
-1

私はJcifsライブラリを使用しています。 私は任意の提案やhelp.sorryのために事前に再帰的にディレクトリとサブディレクトリを他のsmbディレクトリ(samba)にコピーする方法はありますか?

for (int j = 0; j < AppConst.checkfilelist.size(); j++) { 
       String old = AppConst.checkfilelist.get(j); 
       Log.d("smb_c_check_item", "" + AppConst.checkfilelist.get(j)); 
       copyToDirectory(old, AppConst.destinationpath); 

      } 

機能

public int copyToDirectory(String old, String newDir) { 
     try { 
      SmbFile old_file = new SmbFile(old); 
      SmbFile temp_dir = new SmbFile(newDir); 

      Log.d("smb_c_sour:desti_dir", "" + old + "---" + newDir); 

      // copy file 
      if (old_file.isFile()) { 

       Log.d("smb_c_file","yes"); 

       String file_name = old.substring(old.lastIndexOf("/"), old.length()); 
       Log.d("smb_c_file_name", "" + file_name); 

       String servername="smb://+ ipaddress+/"; 


       NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication(servername, "", ""); 

       // smb file path 
       SmbFile cp_smbfile = new SmbFile(newDir + file_name.substring(1),auth1); 
       Log.d(" smb_c_file_path", "" + cp_smbfile); 

       if (!cp_smbfile.exists()) 
        cp_smbfile.createNewFile(); 
       cp_smbfile.connect(); 

       InputStream in = new FileInputStream(String.valueOf((old_file))); 

       SmbFileOutputStream sfos = new SmbFileOutputStream(cp_smbfile); 

       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = in.read(buf)) > 0) { 
        sfos.write(buf, 0, len); 
       } 

       // copy directory 
      } else if (old_file.isDirectory()) { 

       String servername="smb://+ ipaddress+/"; 
       NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication(servername, "", ""); 
       Log.d("smb_c_folder","yes"); 
       String files[] = old_file.list(); 

       int len = files.length; 
       Log.d("smb_c_dirlength", "" + len); 

       for(int i1=0;i1<len;i1++){ 
        Log.d("smb_c_dir---",""+files[i1]); 
       } 

       // remove last character 
       old = old.substring(0, old.length() - 1); 
       // get dir name 
       String old_f = old.substring(old.lastIndexOf("/"), old.length()); 
       Log.d("smb_c_old_f", "" + old_f); 

       //create smbfile path 
       SmbFile smbdir = new SmbFile(newDir + old_f.substring(1),auth1); 


       // create new directory 
       if (!smbdir.exists()) { 
        Log.d("smb_c_mkdir", "created"); 
        smbdir.mkdirs(); 
        //return -1; 
       } 

       Log.d("smb_c_dir", "" + smbdir); 
       for (int i = 0; i < len; i++) { 
        copyToDirectory(old + "/" + files[i], smbdir + "/"); 
        Log.d("smb_copy_rec", "" + old + "/" + files[i] + ":" + smbdir + "/"); 
       } 


      } else if (!temp_dir.canWrite()) 
       Log.d("smb_c_dir_noperm","yes"); 
       return -1; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return 0; 
    } 

ありがとう関数を呼び出す

...このコードはコピー親ディレクトリとファイルを作業しているが、サブディレクトリはコピーしないようにしよう私の悪いengのために。

答えて

-1

私は解決策を見つけました..

public int copySdToSmb(String old, String newDir) { 
     try { 

      File copyfile = new File(old); 
      SmbFile temp_dir = new SmbFile(newDir);      

      if (copyfile.isFile()) { 

       SmbFile cp_smbfile = new SmbFile(newDir + copyfile.getName());      
       if (!cp_smbfile.exists()) 
        cp_smbfile.createNewFile(); 
       cp_smbfile.connect(); 
       InputStream in = new FileInputStream(copyfile); 
       SmbFileOutputStream sfos = new SmbFileOutputStream(cp_smbfile); 

       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = in.read(buf)) > 0) { 
        sfos.write(buf, 0, len); 
       } 
       in.close(); 
       sfos.close(); 

      } else if (copyfile.isDirectory()) { 
       String files[] = copyfile.list(); 
       // get dir name 
       String old_f = old.substring(old.lastIndexOf("/"), old.length());    
       int len = files.length; 
       SmbFile smbdir = new SmbFile(newDir + old_f.substring(1)); 

       // create new directory 
       if (!smbdir.exists()) {  
        smbdir.mkdirs(); 
        //return -1; 
       } 
       for (int i = 0; i <= len; i++) { 
        copySdToSmb(old + "/" + files[i], smbdir + "/"); 

       } 
      } else if (!temp_dir.canWrite()) {    
       return -1; 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 

     } 
     return 0; 

    } 
関連する問題