10

So I'm trying to write a file from android device to windows shared folder. I'm using latest version of JCIFS and code which displays available network shares works fine. So I assume everything is ok with JCIFS and with my LAN, WiFi etc. Here is the code for file upload (actually I just want to write a text Sring to a File):

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

Since the url is logged I'm sure it is correct (also I checked the url using the code I mentioned above and it browses the folder's contain).
But the problem is Im always getting the same:

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

Shares are not password protected, so I don't need any username/password to get access. I could read/write/delete files from another WinPC and no authorization is required. Also I tried to create a password for user on WinPC with shares but result was the same. So I tried several versions of NtlmPasswordAuthentication with no luck:

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

So what am I doing wrong and how to achive my goal when there is no auth is required to get access to shared folder?
BTW my Samsung TV which is linux based and uses samba client is accessing the same shared folder with no problem and plays MP3 from there (well, yes, it reads only). Since my AOS device is accessing my LAN via WiFi (instead of TV which is connected via Ethernet) I also checked access to shared folder using notebook+WiFi and found no problems.
Added:
I'm trying now to execute following lines:

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

and getting the same Access denied. I'm not even trying to write file...

Community
  • 1
  • 1
Stan
  • 6,121
  • 8
  • 48
  • 85

2 Answers2

12

OMG!!! Solution was so simple!!! To access network which is not login/password protected and thus doesn't need any authorization is not NtlmPasswordAuthentication.ANONYMOUS BUT it is:

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

damn it wasn't that obvious!

Stan
  • 6,121
  • 8
  • 48
  • 85
  • 2
    You'll need to enable guest account on windows before this will work. – Daan Olislagers Oct 15 '13 at 08:32
  • 1
    In my case it was WinXP and you don't need a guest account to be active for this to work (had checked it twice right now). As for Win7 it is probably so. Win7 became very hard to tune up if speaking about networking and thats the main reason I don't like it comparing to XP. – Stan Oct 15 '13 at 09:02
2

try with this code

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

Also check you have share the folder path that you are accessing. Also check it has given write permission to the folder

rwe
  • 177
  • 1
  • 1
  • 10
  • ??? is this a copy-pasted lines of my code? Whats the point? Write permissions are given and shared folder does exist thats for sure – Stan Feb 15 '13 at 16:28
  • really sorry about my mistake. can u try with this code and let me know please – rwe Feb 27 '13 at 08:09
  • ok. so what am I suppose to use as username and password in 1st line of code? Like I mentioned before there is no authentication needed so there are no user and password was set. – Stan Feb 27 '13 at 15:58
  • the only difference in your code is new SmbFileOutputStream(file, true); which allows file to append. No luck anyway. Still Access denied – Stan Feb 27 '13 at 16:16