【问题标题】:Android upload to network drive(samba share) performance issueAndroid 上传到网络驱动器(samba 共享)性能问题
【发布时间】:2017-01-25 15:54:47
【问题描述】:

当我从平板电脑将100kb 的图片上传到与 JCIFS 共享的 samba 时遇到问题,大约需要 10-20 分钟(在我将缓冲区从 1024 to 20971520 更改之前花了将近 6 个小时)但是它不再产生任何增加它的效果

这不是连接问题,因为我已经使用ES File 对其进行了测试,它立即上传了我的图片

private class MyCopy extends AsyncTask<String, String, String> {

    String z = "";
    String username = "", password = "", servername = "", filestocopy = "";

    @Override
    protected void onPreExecute() {   
            username = edtusername.getText().toString();
            password = edtpassword.getText().toString();
            servername = "smb://" + edtservername.getText().toString();
            filestocopy = editdir.getText().toString();
        }

       protected String doInBackground(String... params) {
   //         String buffer;
  //          buffer = setingPreferences.getString("buffer", "");
            File file = new File(filestocopy);
            String filename = file.getName();

            NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication(
                    servername, username, password);

            try {

                SmbFile sfile = new SmbFile(servername + "/" + filename, auth1);
                if (!sfile.exists())
                    sfile.createNewFile();
                sfile.connect();

                InputStream in = new FileInputStream(file);

                SmbFileOutputStream sfos = new SmbFileOutputStream(sfile);

                byte[] buf = new byte[20971520]; //(parseInt(buffer))
                int len;
                while ((len = in.read(buf)) > 0){
                    sfos.write(buf, 0, len);

                }
                in.close();
                sfos.close();

                z = "File copied successfully";
            } catch (Exception ex) {

                z = z + " " + ex.getMessage().toString();
            }

            return z;
        }
    }

【问题讨论】:

  • 您是否尝试过在 J2SE 环境中使用 JCIFS 来检查它是 Android 问题还是 JCIFS 只是问题?这个库似乎很旧(最后一次真正的更新是 2011 年)......

标签: android image-uploading smb jcifs


【解决方案1】:

缓冲区大小不应该有明显差异,但绝对不应该是 20M。改用 4k 之类的东西。

您确定是实际文件传输需要这么长时间吗?没有理由 100k 最多只需要几秒钟。您是否尝试在每个步骤之间放置日志语句,包括在身份验证调用之前和之后、createNewFile()connect() 以检查这些是否是瓶颈?

另外,我认为您应该在读取长度为 &gt;= 0 而不是严格意义上的 &gt; 0 时复制字节,因为 -1 表示流结束,而不是 0。

【讨论】:

    【解决方案2】:

    你试过了吗

    new SmbFile("username:password@server/")

    而不是使用 NTLM?这也可能是 DNS 问题,所以请尝试

    jcifs.Config.setProperty("resolveOrder", "DNS");

    如果两者都不起作用,您可能想尝试 BufferedOutputStream 和您的 SmbFileOutputStream

    【讨论】:

    • new SmbFile("username:password@server/") 提供帐户当前已禁用 jcifs.Config.setProperty("resolveOrder", "DNS"); 也没有解决如何将 BufferedOutputStream 与 SmbFileOutputStream 一起使用,因为它不能在此处应用 SmbFileOutputStream sfos = new BufferedOutputStream(sfile); 并且在此处不兼容 BufferedOutputStream sfos = new SmbFileOutputStream(sfile);跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多