John Snow
John Snow

Reputation: 21

How to copy file from sdcard to smb?

I use the below code to copy file from smb to sdcard.

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

and I want to make the inverse way, how can I do that please?

Upvotes: 0

Views: 803

Answers (1)

Budius
Budius

Reputation: 39836

It's just a matter of inverting the input and outputstream. Like this:

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

Upvotes: 1

Related Questions