Android 如何将SDCard目录和子目录递归复制到其他smb目录(samba)?

Android 如何将SDCard目录和子目录递归复制到其他smb目录(samba)?,android,smb,Android,Smb,我正在使用Jcifs库。 我尝试这个代码是工作复制父目录和文件,但子目录不复制 调用函数 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));

我正在使用Jcifs库。 我尝试这个代码是工作复制父目录和文件,但子目录不复制

调用函数

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);

            }
for(int j=0;j
作用

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;
    }
public int copyToDirectory(字符串old,字符串newDir){
试一试{
SmbFile old_file=新SmbFile(旧);
SmbFile temp_dir=新SmbFile(newDir);
Log.d(“smb_c_sour:desti_dir”,“旧的+”--“+newDir”);
//复制文件
if(旧文件.isFile()){
Log.d(“smb_c_文件”,“是”);
字符串文件_name=old.substring(old.lastIndexOf(“/”),old.length());
Log.d(“smb\U c\U文件名”,“文件名+文件名”);
String servername=“smb://+ipaddress+/”;
NtlmPasswordAuthentication auth1=新的NtlmPasswordAuthentication(服务器名,“,”);
//smb文件路径
SmbFile cp_SmbFile=新SmbFile(newDir+file_name.substring(1),auth1);
Log.d(“smb\u c\u文件\u路径”,“”+cp\u smbfile);
如果(!cp_smbfile.exists())
cp_smbfile.createNewFile();
cp_smbfile.connect();
InputStream in=新文件InputStream(String.valueOf((旧文件));
SmbFileOutputStream sfos=新的SmbFileOutputStream(cp\U smbfile);
字节[]buf=新字节[1024];
内伦;
而((len=in.read(buf))>0){
sfos.write(buf,0,len);
}
//复制目录
}else if(旧的_文件.isDirectory()){
String servername=“smb://+ipaddress+/”;
NtlmPasswordAuthentication auth1=新的NtlmPasswordAuthentication(服务器名,“,”);
Log.d(“smb_c_文件夹”,“是”);
字符串文件[]=旧的_文件.list();
int len=files.length;
Log.d(“smb_c_dirlength”,即“+len”);
对于(inti1=0;i1我找到了解决方案.)

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;

    }
public int copysdtomb(字符串old,字符串newDir){
试一试{
文件copyfile=新文件(旧文件);
SmbFile temp_dir=新SmbFile(newDir);
if(copyfile.isFile()){
SmbFile cp_SmbFile=newsmbfile(newDir+copyfile.getName());
如果(!cp_smbfile.exists())
cp_smbfile.createNewFile();
cp_smbfile.connect();
InputStream in=新文件InputStream(copyfile);
SmbFileOutputStream sfos=新的SmbFileOutputStream(cp\U smbfile);
字节[]buf=新字节[1024];
内伦;
而((len=in.read(buf))>0){
sfos.write(buf,0,len);
}
in.close();
sfos.close();
}else if(copyfile.isDirectory()){
字符串文件[]=copyfile.list();
//获取目录名
字符串old\u f=old.substring(old.lastIndexOf(“/”),old.length();
int len=files.length;
SmbFile smbdir=新的SmbFile(newDir+旧的子字符串(1));
//创建新目录
如果(!smbdir.exists()){
smbdir.mkdirs();
//返回-1;
}
对于(int i=0;i