Android 访问windows共享文件夹

1.添加依赖:

gradle工程:

implementation 'org.codelibs:jcifs:2.1.31'

maven工程:

<dependency>
  <groupId>org.codelibs</groupId>
  <artifactId>jcifs</artifactId>
  <version>2.1.31</version>
</dependency>

2.获取Smb对象:

类:SmbFile( String url, NtlmPasswordAuthentication auth )
url: 以"smb://"开头
NtlmPasswordAuthentication : windows用户凭证,由于域名,用户名,密码作为参数创建

SmbFile smbFile = new SmbFile("smb://192.168.1.1/share/", new NtlmPasswordAuthentication(domain, "userName", "password"));

3.枚举smb://192.168.1.1/share/下的所有文件及目录:

SmbFile[] files = smbFile.listFile();

4.从Windows共享目录中读取文件:SmbReader.java

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import jcifs.smb1.smb1.NtlmPasswordAuthentication;
import jcifs.smb1.smb1.SmbFile;
import jcifs.smb1.smb1.SmbFileInputStream;


public class SmbReader {

    static final String TAG_SMB_READER = "SMB_READER";
    static final int DEFAULT_BUFFER_SIZE = 524288; // 512 Kb

    final NtlmPasswordAuthentication authentication;

    public SmbReader(NtlmPasswordAuthentication authentication) {
        this.authentication = authentication;
    }

    public SmbReader(String user, String password)  {
        this(null, user, password);
    }

    public SmbReader(String domain, String user, String password) {
        this.authentication = new NtlmPasswordAuthentication(domain, user, password);
    }

    /**
     * read windows share object and save to local
     * @param url url of object, format: "smb://host/path/", such as "smb://192.168.1.2/share/"
     * @param localPath local path(android)
     * @param overwrite overwrite
     * @param excludeHideFiles exclude hide files
     * @return true | false
     */
    public boolean read(String url, String localPath, boolean overwrite, boolean excludeHideFiles) {
        boolean result = false;
        try {
            if (url.isEmpty() || localPath.isEmpty()) {
                Log.e(TAG_SMB_READER,"method: read, params error");
                return false;
            }

            SmbFile srcFile = new SmbFile(url, this.authentication);
            if (srcFile == null || !srcFile.exists()) {
                Log.e(TAG_SMB_READER,"method: read, the smb file is not exist");
                return false;
            }

            result = read(srcFile, localPath, overwrite, excludeHideFiles);
        } catch (Exception e) {
            Log.e(TAG_SMB_READER, "method: save, exception: " + e.getMessage());
        }
        return result;
    }

    private boolean read(SmbFile srcFile, String localPath, boolean overwrite, boolean excludeHideFiles) {
        try {
            File file = new File(localPath + File.separator + srcFile.getName());
            if (file.getParentFile() != null && !file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            if (srcFile.isDirectory()) {
                if (!file.exists()) {
                    file.mkdirs();
                }
                SmbFile[] subSmbFiles = excludeHideFiles ? srcFile.listFiles(file1 -> !file1.isHidden()) : srcFile.listFiles();
                for (SmbFile subSmbFile: subSmbFiles) {
                    read(subSmbFile, file.getPath(), overwrite, excludeHideFiles);
                }
            } else if (srcFile.isFile()) {
                if (overwrite && file.exists()) {
                    file.delete();
                }
                if (!read(srcFile, file)) {
                    return false;
                }
            }
        } catch (Exception e) {
            Log.e(TAG_SMB_READER, "method: save, exception: " + e.getMessage());
            return false;
        }
        return true;
    }

    private boolean read(SmbFile smbFile, File file) {
        boolean result = true;
        try (InputStream in = new BufferedInputStream(new SmbFileInputStream(smbFile));
             OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
            if (smbFile.length() > 0) {
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                int read = 0;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                out.flush();
            } else if (smbFile.length() == 0) {
                file.createNewFile();
            }
        } catch (Exception e) {
            Log.e(TAG_SMB_READER, "method: saveFile, exception: " + e.getMessage());
            result = false;
        }
        return result;
    }
}

用法:

                // read file/folder from windows share folder
                String url = "smb://192.168.1.2/share/hello.docx";
                SmbReader reader = new SmbReader("userName", "password");
                boolean result = reader.read(url, path, true, true);
                if (result) {
                    Log.i(TAG_MAIN_FRAGMENT, "read smb file object success");
                }

5.往windows共享目录写文件:SmbWriter.java

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import jcifs.smb1.smb1.NtlmPasswordAuthentication;
import jcifs.smb1.smb1.SmbFile;
import jcifs.smb1.smb1.SmbFileOutputStream;

public class SmbWriter {

    static final String TAG_SMB_WRITER = "SMB_WRITER";
    static final int DEFAULT_BUFFER_SIZE = 524288; // 512 Kb

    final NtlmPasswordAuthentication authentication;

    public SmbWriter(NtlmPasswordAuthentication authentication) {
        this.authentication = authentication;
    }

    public SmbWriter(String user, String password)  {
        this(null, user, password);
    }

    public SmbWriter(String domain, String user, String password) {
        this.authentication = new NtlmPasswordAuthentication(domain, user, password);
    }

    /**
     * write file/folder to windows share dir
     * @param srcPath local resource path, file or folder
     * @param url target path(only folder path), format: "smb://host/path/",  file: "smb://192.168.1.2/share/"
     * @param overwrite overwrite
     * @param excludeHideFiles exclude hide files
     * @return
     */
    public boolean write(String srcPath, String url, boolean overwrite, boolean excludeHideFiles) {
        boolean result = false;
        try {
            File file = new File(srcPath);
            if (!file.exists()) {
                Log.e(TAG_SMB_WRITER, "method: write, resource isn't exist or params error");
                return false;
            }

            url = url + File.separator + file.getName();
            SmbFile smbFile = new SmbFile(url, this.authentication);
            if (file.isDirectory()) {
                if (!smbFile.exists()) {
                    smbFile.mkdirs();
                }
                File[] subFiles = excludeHideFiles ? file.listFiles(subFile -> !subFile.isHidden()) : file.listFiles();
                for (File subFile : subFiles) {
                    String subSrcPath = srcPath + File.separator + subFile.getName();
                    write(subSrcPath, url, overwrite, excludeHideFiles);
                }
            } else if (file.isFile()) {
                if (smbFile.exists() && overwrite) {
                    smbFile.delete();
                }
                result = writeFile(file, smbFile);
            }
        } catch (Exception e) {
            Log.e(TAG_SMB_WRITER, "method: write, exception: " + e.getMessage());
        }
        return result;
    }

    public boolean writeFile(File file, SmbFile smbFile) {
        boolean result = false;
        try (InputStream in = new BufferedInputStream(new FileInputStream(file));
             OutputStream out = new BufferedOutputStream(new SmbFileOutputStream(smbFile));) {
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            out.flush();
            result = true;
        } catch (Exception e) {
            Log.e(TAG_SMB_WRITER, "method: writeFile, write file failed, exception: " + e.getMessage());
        }
        return result;
    }
}

用法:

                // write file/folder to windows share
                String url = "smb://192.168.1.2/share/";
                SmbWriter writer = new SmbWriter("userName", "password");
                boolean result = writer.write(path, url, true, true);
                if (result) {
                    Log.i(TAG_MAIN_FRAGMENT, "write smb file object success");
                }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 12引入了一项新的功能,即文件夹访问限制。这项功能旨在增强用户对其设备上存储的敏感文件的保护和隐私。在以前的Android版本中,应用程序可以自由地访问设备上的文件夹,包括存储在内部存储和外部存储中的文件。但是,这种开放的访问权限可能会导致安全和隐私问题,因此,Android 12引入了一套新的规则来限制对文件夹的访问。 随着Android 12的推出,对于应用程序来说,访问文件夹将更加受限制。现在,应用程序只能直接访问其私有目录和共享存储目录中的文件。私有目录仅包含该应用程序专用的文件,而共享存储目录则是用于应用程序之间共享文件的地方。对于其他文件夹,应用程序需要使用新的权限来访问。 在Android 12中,开发人员需要在其应用程序清单文件中声明新的文件访问权限,并获取用户的授权才能访问受限文件夹。这样做的目的是确保用户知道哪些应用程序可以访问其文件,并有机会选择是否授予访问权限。 这项新的文件夹访问限制功能在加强用户隐私保护的同时,也给开发人员带来了一些挑战。他们需要重新审查他们的应用程序逻辑,并确保它们不会违反新的访问规则。这将有助于提高应用程序的安全性和稳定性,并确保用户的文件和数据得到适当的保护。 总之,Android 12的文件夹访问限制功能是对以往开放的访问权限进行了改进,旨在保护用户的隐私和安全。开发人员需要遵守新的访问规则,并尊重用户的选择,以确保他们的应用程序符合最新的隐私标准。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值