【问题标题】:Copying file from a Samba drive to an Android sdcard directory将文件从 Samba 驱动器复制到 Android sdcard 目录
【发布时间】:2011-11-17 15:19:54
【问题描述】:

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

from = new SmbFile("smb://username:password@a.b.c.d/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 文件夹?谢谢。

【问题讨论】:

标签: android file copy sd-card samba


【解决方案1】:

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

try {
    SmbFile source = 
            new SmbFile("smb://username:password@a.b.c.d/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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2014-01-30
    • 1970-01-01
    相关资源
    最近更新 更多