Este exemplo compartilha o código específico de Java usando o SMB para ler arquivos remotos para sua referência. O conteúdo específico é o seguinte
pacote com.ys.test.fileReadWriter; importar java.io.bufferInputStream; importar java.io.bufferedOutputStream; importar java.io.file; importar java.io.fileInputStream; importar java.io.fileOutputStream; importar java.io.ioException; importar java.io.inputStream; importar java.io.OutputStream; importar java.net.MalformEdUrlexception; importar jcifs.smb.smbfile; importar jcifs.smb.smbfileInputStream; importar jcifs.smb.smbfileOutputStream; classe pública RemoteAccessData {/** * @param args * @throws ioexception */public static void main (string [] args) lança ioexception {smbget1 ("sMb: //192.168.75.204/test/new text document.txt"); smbget ("sMB: //192.168.75.204/test/new text document.txt", "e:/"); }/** * Método 1: * * @param remoteurl * caminho remoto SMB: //192.168.75.204/test/new text document.txt * @throws ioexception */public static void smbget1 (stringUrl) lança a remesceção {sbrbfile sml; int length = smbfile.getContentLength (); // Obtenha o buffer de tamanho de tamanho do arquivo [] = novo byte [comprimento]; SMBFileInputStream in = new SMBFileInputStream (SMBFILE); // Crie o fluxo de entrada do arquivo SMB enquanto (in.read (buffer))! = -1) {System.out.write (buffer); System.out.println (buffer.length); } in.close (); } // Download file from the shared directory/** * Method 2: * Path format: smb://192.168.75.204/test/ Create a new text document.txt * smb://username:[email protected]/test * @param remoteUrl * Remote path * @param localDir * Local path to write*/ public static void smbGet(String remoteurl, string localDir) {inputStream em = null; OutputStream out = null; tente {SMBFILE REMOTEFILE = new SMBFILE (Remoteurl); if (remotefile == null) {System.out.println ("arquivo compartilhado não existe"); retornar; } String filename = remotefile.getName (); Arquivo localfile = novo arquivo (localDir + file.separator + nome do arquivo); in = new bufferInputStream (new SMBFileInputStream (RemoteFile)); out = new BufferedOutputStream (new FileOutputStream (LocalFile)); byte [] buffer = novo byte [1024]; while (in.read (buffer)! = -1) {out.write (buffer); buffer = novo byte [1024]; }} catch (Exceção e) {e.printStackTrace (); } finalmente {tente {out.close (); in.Close (); } catch (ioexception e) {e.printStackTrace (); }}} // Faça o upload do arquivo public static void sMbput (string remoteurl, string localFilePath) {inputStream in = null; OutputStream out = null; tente {file LocalFile = new File (LocalFilePath); String filename = localfile.getName (); SMBFILE REMOTEFILE = NOVO SMBFILE (Remoteurl + "/" + FileName); in = new bufferInputStream (new FileInputStream (LocalFile)); out = new BufferedOutputStream (new SMBFileOutputStream (RemoteFile)); byte [] buffer = novo byte [1024]; while (in.read (buffer)! = -1) {out.write (buffer); buffer = novo byte [1024]; }} catch (Exceção e) {e.printStackTrace (); } finalmente {tente {out.close (); in.Close (); } catch (ioexception e) {e.printStackTrace (); }}} // URL remoto SMB: //192.168.0.77/test // Se você precisar de um nome de usuário e senha, assim: // SMB: // Nome de usuário: [email protected]/test}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.