Este ejemplo comparte el código específico de Java usando SMB para leer archivos remotos para su referencia. El contenido específico es el siguiente
paquete com.yss.test.filreadwriter; import java.io.bufferedInputStream; import java.io.bufferedOutputStream; import java.io.file; import java.io.fileInputStream; import java.io.fileOutputStream; import java.io.ioException; import java.io.inputstream; import java.io.outputstream; import java.net.malFormedUrexception; import jcifs.smb.smbfile; import jcifs.smb.smbFileInputStream; import jcifs.smb.smbFileOutputStream; public class remoteAccessData {/** * @param args * @throws ioexception */public static void main (string [] args) lanza ioexception {smbget1 ("smb: //192.168.75.204/test/new de texto documento.txt"); smbget ("smb: //192.168.75.204/test/new text document.txt", "e:/"); }/** * Método 1: * * @param RemoteUrl * Remote Path smb: //192.168.75.204/test/new text document.txt * @throws ioException */public static void smbget1 (string remotelUrl) lanza ioexception {smbfile smbfile = new smbfile (remoto); int longitud = smbfile.getContentLength (); // Obtener el tamaño de byte de tamaño de archivo [] = new Byte [longitud]; SmbFileInputStream in = new SMBFileInputStream (SMBFile); // Crear flujo de entrada de archivo SMB while ((in.read (buffer))! = -1) {system.out.write (buffer); System.out.println (buffer.length); } in.close (); } // Descargar archivo desde el directorio compartido/** * Método 2: * Formato de ruta: smb: //192.168.75.204/test/ crea un nuevo documento de texto.txt * smb: // nombre de usuario: contraseñ[email protected]/test * @param remoteurl * ruta remota * @param localdir * local ruta a escritura */pública vhatic static ssoid smbget (stret streturl, streturl, streturl, stretret localDir) {inputStream in = null; OutputStream out = null; intente {SMBFile RemoteFile = new SMBFile (RemoteUrl); if (remotaFile == null) {system.out.println ("el archivo compartido no existe"); devolver; } String filename = remoteFile.getName (); Archivo localfile = nuevo archivo (localdir + archivo.separator + nombre de archivo); in = new BufferedInputStream (nuevo SMBFileInputStream (RemoteFile)); out = new BufferedOutputStream (new FileOutputStream (LocalFile)); byte [] buffer = new Byte [1024]; while (in.read (buffer)! = -1) {out.write (buffer); buffer = nuevo byte [1024]; }} catch (Exception e) {E.PrintStackTrace (); } finalmente {try {out.close (); cercar(); } catch (ioException e) {E.PrintStackTrace (); }}} // Cargar archivo public static void smbput (string rlowUrl, string localFilePath) {inputStream in = null; OutputStream out = null; intente {archivo localfile = nuevo archivo (localFilePath); Cadena filename = localFile.getName (); SMBFILE ROBAYFILE = new SMBFile (RemoteUrl + "/" + FileName); in = new BufferedInputStream (new FileInputStream (LocalFile)); out = new BufferedOutputStream (nuevo SMBFileOutputStream (RemoteFile)); byte [] buffer = new Byte [1024]; while (in.read (buffer)! = -1) {out.write (buffer); buffer = nuevo byte [1024]; }} catch (Exception e) {E.PrintStackTrace (); } finalmente {try {out.close (); cercar(); } catch (ioException e) {E.PrintStackTrace (); }}} // URL remota SMB: //192.168.0.77/test // Si necesita un nombre de usuario y contraseña, solo así: // smb: // username: contraseñ[email protected]/test}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.