이 예제는 Java가 참조를 위해 서버에 원격으로 파일을 보내는 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
1. 관련 JAR 패키지 JCIFS-1.3.14.1.jar
2. SMB 선언을 만듭니다
import java.io.bufferedInputStream; import java.io.file; import java.io.fileInputStream; import java.io.ioexception; java.net.malformedurlexception import; import java.net.unknownhostexception; jcifs.smb.smbexception 가져 오기; JCIFS.SMB.SMBFILE 가져 오기; JCIFS.SMB.SMBFILEOUTPUTSTREAM 가져 오기; public class smbutil {// 1. 속성 프라이빗 문자열 url = "smb : // username : [email protected]/mars/"; 개인 smbfile smbfile = null; 개인 smbfileoutputStream smbout = null; 개인 정적 smbutil smbutil = null; // 공유 파일 프로토콜 private smbutil (String URL) {this.url = url; this.init (); } // 2. SMBUTIL 및 연결 방법 공개 정적 동기화 SMBUTIL GETINSTANCE (String URL) {if (smbutil == NULL) 새로운 SMBUTIL (URL)을 반환합니다. SMBUTIL을 반환하십시오. } // 3.smbfile 연결 public void init () {try {system.out.println ( "시작 연결 ... URL :" + this.url); smbfile = 새로운 smbfile (this.url); smbfile.connect (); System.out.println ( "연결 성공 ... URL :" + this.url); } catch (marlomedurlexception e) {e.printstacktrace (); System.out.print (e); } catch (ioexception e) {e.printstacktrace (); System.out.print (e); }} // 4. 파일을 서버에 업로드 public int uploadfile (파일 파일) {int flag = -1; bufferedInputStream bf = null; try {this.smbout = new smbfileoutputStream (this.url + "/" + file.getName (), false); bf = new bufferedInputStream (new FileInputStream (file)); 바이트 [] bt = 새로운 바이트 [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 ( "파일 전송 종료 ..."); } catch (smbexception e) {e.printstacktrace (); System.out.println (e); } catch (marlomedurlexception e) {e.printstacktrace (); System.out.println (e); } catch (unknownHostException e) {e.printstacktrace (); System.out.println (e); } catch (unknownHostException e) {e.printstacktrace (); System.out.println ( "호스트를 찾을 수 없음 ... url :" + this.url); } catch (ioexception e) {e.printstacktrace (); System.out.println (e); } 마침내 {try {if (null! = this.smbout) this.smbout.close (); if (null! = bf) bf.close (); } catch (예외 e2) {e2.printstacktrace (); }} 반환 플래그; } // 5. test public static void main (string [] args) {// 서버 주소 smb : // 컴퓨터 사용자 이름 : computer password@computer IP 주소/IP 공유 폴더 remoteUrl = "smb : // wangqinghua : [email protected]/mars/"; 문자열 localfile = "f :/스위치 프로덕션 및 판매 엔터프라이즈 디렉토리 .xls"; // 업로드 할 로컬 파일 파일 = 새 파일 (localFile); smbutil smb = smbutil.getinstance (remoteurl); smb.uploadfile (file); // 업로드 파일}}주목할만한 것 :
위는 LAN을 기반으로하며 파일을 업로드하는 디렉토리 또는 폴더는 공유 모드로 설정해야합니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.