如何将文件从SD卡复制到smb?

时间:2014-03-18 15:07:46

标签: android android-sdcard smb

我使用以下代码将文件从smb复制到SD卡。

    SmbFile remoteFile;
try {
    remoteFile = new SmbFile("smb://172.25.0.1/Public-01/Documents/Welcome.pdf");
    OutputStream os = new FileOutputStream("sdcard/Download/Welcome.pdf");
    InputStream is = remoteFile.getInputStream();

    int bufferSize = 5096;

    byte[] b = new byte[bufferSize];
    int noOfBytes = 0;
    while( (noOfBytes = is.read(b)) != -1 )
    {
        os.write(b, 0, noOfBytes);
    }
    os.close();
    is.close();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我想反过来,我该怎么办呢?

1 个答案:

答案 0 :(得分:1)

这只是反转inputoutputstream的问题。像这样:

SmbFile remoteFile;
try {
    remoteFile = new SmbFile("smb://172.25.0.1/Public-01/Documents/Welcome.pdf");
    OutputStream os = remoteFile.getOutputStream();
    InputStream is = new FileInputStream("sdcard/Download/Welcome.pdf");

    int bufferSize = 5096;

    byte[] b = new byte[bufferSize];
    int noOfBytes = 0;
    while( (noOfBytes = is.read(b)) != -1 )
    {
        os.write(b, 0, noOfBytes);
    }
    os.close();
    is.close();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}