java implements remote file sending to server based on ES1en.smb

  • 2020-12-21 18:03:45
  • OfStack

This article is an example of java to share the implementation of remote file to the server specific code for your reference, the specific content is as follows

1. Dependent related jar package jcifs- 1.3.14.1.jar
2. Create declarations for SMB


import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.UnknownHostException; 
 
import jcifs.smb.SmbException; 
import jcifs.smb.SmbFile; 
import jcifs.smb.SmbFileOutputStream; 
 
public class SmbUtil { 
  // 1.  Declaration attributes  
  private String url = "smb://userName:password@192.168.2.153/mars/"; 
  private SmbFile smbFile = null; 
  private SmbFileOutputStream smbOut = null; 
  private static SmbUtil smbUtil = null; //  Shared file protocol  
   
  private SmbUtil(String url) { 
    this.url = url; 
    this.init(); 
  } 
   
  // 2.  get SmbUtil And the method of connection  
  public static synchronized SmbUtil getInstance(String url) { 
    if (smbUtil == null) 
      return new SmbUtil(url); 
    return smbUtil; 
  } 
 
   
  // 3.smbFile The connection  
  public void init() { 
    try { 
      System.out.println(" Begin to connect ...url : " + this.url); 
      smbFile = new SmbFile(this.url); 
      smbFile.connect(); 
      System.out.println(" The connection is successful ...url : " + this.url); 
    } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      System.out.print(e); 
    } catch (IOException e) { 
      e.printStackTrace(); 
      System.out.print(e); 
    } 
  } 
   
  // 4. Upload the file to the server  
  public int uploadFile(File file) { 
    int flag = -1; 
    BufferedInputStream bf = null; 
    try { 
      this.smbOut = new SmbFileOutputStream(this.url + "/" 
          + file.getName(), false); 
      bf = new BufferedInputStream(new FileInputStream(file)); 
      byte[] bt = new 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(" End of file transfer ..."); 
    } catch (SmbException e) { 
      e.printStackTrace(); 
      System.out.println(e); 
    } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      System.out.println(e); 
    } catch (UnknownHostException e) { 
      e.printStackTrace(); 
      System.out.println(" Host cannot be found ...url : " + this.url); 
    } catch (IOException e) { 
      e.printStackTrace(); 
      System.out.println(e); 
    } finally { 
      try { 
        if (null != this.smbOut) 
          this.smbOut.close(); 
        if (null != bf) 
          bf.close(); 
      } catch (Exception e2) { 
        e2.printStackTrace(); 
      } 
    } 
 
    return flag; 
  } 
 
  // 5.  in main Method inside test  
  public static void main(String[] args) { 
    //  Address of server   Format �  smb:// Computer user name : Computer passwords @ The computer IP address /IP Shared folder  
    String remoteUrl = "smb://wangqinghua:wqh123@192.168.2.153/mars/"; 
    String localFile = "F:/ Switch production and sales enterprise directory .xls"; //  Files to upload locally  
    File file = new File(localFile); 
    SmbUtil smb = SmbUtil.getInstance(remoteUrl); 
    smb.uploadFile(file);//  Upload a file  
  } 
} 

Points to Note:

The above is LAN based, and the directory or folder of uploaded files must be set to share mode.


Related articles: