【问题标题】:How to read/write a files to a network attached storage Java Android如何将文件读/写到网络附加存储Java Android
【发布时间】:2017-01-18 07:13:40
【问题描述】:

我目前正在 Android Studio 中开发一个应用程序,用于将测试文件写入和读取到网络附加存储。然而,事实证明很难找到写入方法。我一直在尝试使用 jcifs。网络附加存储需要使用用户名和密码登录。使用 jcifs 我一直在尝试使用 NtlmPasswordAuthentication 但是我似乎可以正确实现它,因为每次我尝试运行此方法时,应用程序都会强制关闭。

请有人展示如何写入和读取需要登录名和密码的网络连接存储。

感谢大家的帮助!

【问题讨论】:

    标签: java android eclipse android-studio jcifs


    【解决方案1】:

    我注意到我发布了这篇文章,并且我确实有一个解决方案可以让我从网络附加存储中读取文本文件。 希望这会有所帮助!

    private String ReadFilefromNAS(String s, String Username, String Password) {
        //s is the filename path on the NAS
        String file = "";
        SmbFile dir;
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, Username, Password);
            dir = new SmbFile(FilePath + s, auth);
            //If your not using auth use the below
            //dir = new SmbFile(FilePath + s);
            InputStream in = dir.getInputStream();
            BufferedReader q= new BufferedReader(new InputStreamReader(in));
            String line;
            int i = 0;
            while ((line = q.readLine()) != null) {
                if(i==0){
                    file+=line;
                }else {
                    file += "\n" + line;
                }
                i+=1;
            }
        }catch(Exception e){
            Log.e("ERROR",e.toString());
        }
        return file;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 2014-11-05
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2012-04-03
      • 2011-02-28
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多