Dieses Beispiel teilt den spezifischen Code für Java mit, um Dateien für Ihre Referenz aus der Ferne an den Server zu senden. Der spezifische Inhalt ist wie folgt
1. Verwandte JAR-Pakete jcifs-1.3.14.1.jar
2. Erstellen Sie eine SMB -Erklärung
import Java.io.BufferedInputStream; Import Java.io.file; import Java.io.FileInputStream; importieren java.io.ioException; importieren java.net.malformeDurlexception; Importieren Sie java.net.unnownhostException; importieren jcifs.smb.smbexception; importieren jcifs.smb.smbfile; importieren jcifs.smb.smbFileOutputStream; öffentliche Klasse SMBUTIL {// 1. Deklarieren Sie die Eigenschaft private String url = "smb: // username: [email protected]/mars/"; private smbFile smbFile = null; private SmbFileOutputStream smbout = null; privates statisches Smbutil Smbutil = NULL; // Shared Datei Protocol Private Smbutil (String url) {this.url = url; this.init (); } // 2. Get SMBUTIL und Verbindungsmethode öffentliche statische synchronisierte Smbutil getInstance (String url) {if (smbutil == NULL) RECHTE NEUE SMBUTIL (URL); Rückkehr Smbutil; } // 3.smbFile Connection public void init () {try {System.out.println ("Start Connection ... url:" + this.url); SmbFile = new SmbFile (this.url); smbFile.connect (); System.out.println ("Verbindung erfolgreich ... URL:" + this.url); } catch (fehl Formaledurlexception e) {e.printstacktrace (); System.out.print (e); } catch (ioException e) {e.printstacktrace (); System.out.print (e); }} // 4. Laden Sie die Datei auf den Server öffentlich int uploadFile (Dateidatei) {int flag = -1; BufERDInputStream BF = NULL; try {this.smbout = new SmbFileOutputStream (this.url + "/" + file.getName (), false); bf = new buferedInputStream (neuer FileInputStream (Datei)); byte [] bt = neues byte [8192]; int n = bf.read (bt); while (n! = -1) {this.smbout.write (bt, 0, n); this.smbout.flush (); n = bf.read (bt); } Flag = 0; System.out.println ("Dateitransfer End ..."); } catch (smbexception e) {e.printstacktrace (); System.out.println (e); } catch (fehl Formaledurlexception e) {e.printstacktrace (); System.out.println (e); } catch (unbekannteHostException e) {e.printstacktrace (); System.out.println (e); } catch (unbekannteHostException e) {e.printstacktrace (); System.out.println ("Host nicht gefunden ... url:" + this.url); } catch (ioException e) {e.printstacktrace (); System.out.println (e); } endlich {try {if (null! = this.smbout) this.smbout.close (); if (null! = bf) bf.close (); } catch (Ausnahme e2) {e2.printstacktrace (); }} Rückkehrflag; } // 5. Test public static void main (String [] args) {// Serveradressformat SMB: // Computer Benutzername: Computer Passwort@Computer IP -Adresse/IP Shared Ordner String remoteurl = "smb: // wangqinghua: [email protected]/Mars/"; String localFile = "F:/Switch Production and Sales Enterprise Directory.xls"; // Lokale Datei, die hochgeladen werden soll, file Datei = neue Datei (localFile); Smbutil smb = smbutil.getInstance (remoteurl); smb.UploadFile (Datei); // Datei hochladen}}Dinge zu beachten:
Das obige basiert auf dem LAN, und das Verzeichnis oder den Ordner, das Dateien hochladen, muss auf den Freigabemodus gesetzt werden.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.