将文件从Samba驱动器复制到Android sdcard目录 [英] Copying file from a Samba drive to an Android sdcard directory

查看:257
本文介绍了将文件从Samba驱动器复制到Android sdcard目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android和Samba的新手。我试图使用JCIFS副本。将文件从Samba目录复制到Android 3.1设备上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: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);

我在到文件中遇到MalformedURLException。有没有办法使用 copyTo 方法来解决这个问题,或者有一个备用的方法将文件从samba文件夹复制到sdcard文件夹使用JCIFS或任何其他办法?谢谢。

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.

推荐答案

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

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

这篇关于将文件从Samba驱动器复制到Android sdcard目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆