问题
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);
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.
回答1:
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();
}
来源:https://stackoverflow.com/questions/8169593/copying-file-from-a-samba-drive-to-an-android-sdcard-directory