Skip to content

Instantly share code, notes, and snippets.

@mancvso
Created May 27, 2021 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mancvso/8381c92c27c6e817db47cf66375f3892 to your computer and use it in GitHub Desktop.
Save mancvso/8381c92c27c6e817db47cf66375f3892 to your computer and use it in GitHub Desktop.
// build.gradle
// implementation 'org.apache.commons:commons-vfs2:2.2'
// implementation 'net.idauto.oss.jcifs:vfs-jcifs-ng:1.0.1'
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.apache.poi.ss.usermodel.Cell;
import jcifs.CIFSContext;
import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import net.idauto.oss.jcifsng.vfs2.provider.SmbFileSystemConfigBuilder;
try {
// Configure authenticator
BufferedInputStream in = null;
InputStream fin = null;
FileSystemOptions fileSystemOptions = new FileSystemOptions();
StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(domain, user, pass);
// jcifs configuration
Properties jcifsProperties = new Properties();
// these settings are needed for 2.0.x to use anything but SMB1, 2.1.x enables by default and will ignore
jcifsProperties.setProperty("jcifs.smb.client.enableSMB2", "true");
jcifsProperties.setProperty("jcifs.smb.client.useSMB2Negotiation", "true");
CIFSContext jcifsContext = new BaseContext(new PropertyConfiguration(jcifsProperties));
SmbFileSystemConfigBuilder.getInstance().setCIFSContext(fileSystemOptions, jcifsContext);
DefaultFileSystemConfigBuilder.getInstance()
.setUserAuthenticator(fileSystemOptions, userAuthenticator);
FileSystemManager fsManager = VFS.getManager();
FileObject f = fsManager.resolveFile(path, fileSystemOptions);
if (f.exists()) {
fin = f.getContent().getInputStream();
in = new BufferedInputStream(fin);
// ...
f.close();
fsManager.closeFileSystem(f.getFileSystem());
}
} catch (SmbAuthException aut) {
this.lastError = "Error en las credenciales";
aut.printStackTrace();
} catch(SmbException s){
Log.d("SMB", "Code " + s.getNtStatus());
String code = SmbException.getMessageByCode(s.getNtStatus());
if(code.equals("0xC0000054")) {
this.lastError = "Debe cerrar el archivo antes de proceder";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment