将文件从 Samba 驱动器复制到 Android SD 卡目录

发布于 2024-12-16 16:50:53 字数 643 浏览 2 评论 0原文

我是 Android 和 Samba 新手。我正在尝试使用 JCIFS 副本。将文件从 Samba 目录复制到 Android 3.1 设备上 sdcard 下的“下载”目录的方法。以下是我的代码:

from = new SmbFile("smb://username:[email protected]/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);

我在“to”文件上收到 MalformedURLException。有没有办法使用 copyTo 方法来解决此问题,或者是否有其他方法可以使用 JCIFS 或任何其他方式将文件从 samba 文件夹复制到 sdcard 文件夹?谢谢。

I am new to Android and Samba. I am trying to use the JCIFS copy. To method to copy a file from a Samba directory to the 'Download' directory under sdcard on an Android 3.1 device. Following is my code:

from = new SmbFile("smb://username:[email protected]/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);

I am getting a MalformedURLException on the 'to' file. Is there a way to get around this problem using the copyTo method, or is there an alternate way to copy a file from the samba folder to the sdcard folder using JCIFS or any other way? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-12-23 16:50:53

SmbFile 的 copyTo() 方法允许您在网络之间复制文件。要在本地设备和网络之间复制文件,您需要使用流。例如:

try {
    SmbFile source = 
            new SmbFile("smb://username:[email protected]/sandbox/sambatosdcard.txt");

    File destination = 
            new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");

    InputStream in = source.getInputStream();
    OutputStream out = new FileOutputStream(destination);

    // Copy the bits from Instream to Outstream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // Maybe in.close();
    out.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

The SmbFile's copyTo() method lets you copy files from network to network. To copy files between your local device and the network you need to use streams. E.g.:

try {
    SmbFile source = 
            new SmbFile("smb://username:[email protected]/sandbox/sambatosdcard.txt");

    File destination = 
            new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");

    InputStream in = source.getInputStream();
    OutputStream out = new FileOutputStream(destination);

    // Copy the bits from Instream to Outstream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // Maybe in.close();
    out.close();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文