【问题标题】:Write/upload a file using Samba/JCIFS issue (SmbAuthException: Access is denied)使用 Samba/JCIFS 问题写入/上传文件(SmbAuthException:访问被拒绝)
【发布时间】:2013-01-11 14:43:25
【问题描述】:

所以我正在尝试将文件从 android 设备写入 windows 共享文件夹。我正在使用最新版本的 JCIFS,code which displays available network shares 工作正常。所以我假设 JCIFS 和我的 LAN、WiFi 等一切正常。这是文件上传的代码(实际上我只是想将文本 Sring 写入文件):

    public boolean save2Samba(String text, String fileName) {
        try {

            // My Windows shares doesn't require any login/password
            // String name="login";//my windows username
            // String password="password1";//my windows password

            // sSambaFolder contains a path like MYPC/E/SharedFolderName/
            String url = "smb://" + sSambaFolder.toLowerCase()+fileName;

            SmbFile file = null;
            try {
                // assume ANONYMOUS is my case but there is no description of this in JCIFS API
                NtlmPasswordAuthentication auth = NtlmPasswordAuthentication.ANONYMOUS;
                file = new SmbFile(url, auth);
                android.util.Log.i("TestApp",url);
                // output is like smb://mypc/e/sharedfoldername/file.txt;
                SmbFileOutputStream out = new SmbFileOutputStream(file);
                out.write(text.getBytes());
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

由于记录了 url,我确信它是正确的(我也使用上面提到的代码检查了 url,它浏览了文件夹的包含)。
但问题是我总是一样:

W/System.err(3214): jcifs.smb.SmbAuthException: Access is denied.

共享不受密码保护,因此我不需要任何用户名/密码即可访问。我可以从另一台 WinPC 读取/写入/删除文件,并且不需要授权。我也尝试为 WinPC 上的用户创建一个共享密码,但结果是一样的。所以我尝试了几个版本的 NtlmPasswordAuthentication 都没有运气:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(":");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("Administrator:"); //actual username on WinPC with shares
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("Administrator");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,"Administrator","");
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,"","");

那么我做错了什么以及在不需要身份验证即可访问共享文件夹时如何实现我的目标?
顺便说一句,我基于 linux 并使用 samba 客户端的三星电视正在访问同一个共享文件夹没问题,从那里播放 MP3(嗯,是的,它只读)。由于我的 AOS 设备通过 WiFi(而不是通过以太网连接的电视)访问我的 LAN,我还检查了使用 notebook+WiFi 对共享文件夹的访问,没有发现任何问题。
已添加:
我现在正在尝试执行以下几行:

file = new SmbFile(url, auth);
android.util.Log.i("save2Samba", "file.exists(): " + file.exists());

并获得相同的访问被拒绝。我什至没有尝试写文件...

【问题讨论】:

    标签: android samba jcifs


    【解决方案1】:

    天哪!!!解决方法很简单!!!访问不受登录名/密码保护的网络,因此不需要任何授权不是 NtlmPasswordAuthentication.ANONYMOUS 但它是:

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, null, null);
    

    该死的不是那么明显!

    【讨论】:

    • 您需要先在 Windows 上启用访客帐户,然后才能使用。
    • 在我的情况下,它是 WinXP,您不需要激活来宾帐户才能使其工作(现在已经检查了两次)。至于Win7,大概就是这样。如果谈到网络,Win7 变得非常难以调整,这就是我不喜欢它与 XP 相比的主要原因。
    【解决方案2】:

    试试这段代码

     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
                                username, password);
        sharepath = "smb://" + pathtosharefolder + test.txt;  
    
    
     sFile = new SmbFile(sharepath, auth);
        SmbFileOutputStream out = new SmbFileOutputStream(file, true);
        out.write(text.getBytes());
    

    还要检查您是否共享了您正在访问的文件夹路径。还要检查它是否已授予该文件夹的写权限

    【讨论】:

    • ???这是我的代码的复制粘贴行吗?重点是什么?已授予写入权限,并且共享文件夹确实存在,这是肯定的
    • 真的很抱歉我的错误。您可以尝试使用此代码并告诉我吗
    • 好的。那么我想在第一行代码中使用什么作为用户名和密码?就像我之前提到的,不需要身份验证,因此没有设置用户和密码。
    • 您的代码的唯一区别是 new SmbFileOutputStream(file, true);允许文件追加。反正没有运气。仍然拒绝访问
    猜你喜欢
    • 2015-03-29
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多