This example shares the specific code for Java to remotely send files to the server for your reference. The specific content is as follows
1. Related jar packages jcifs-1.3.14.1.jar
2. Create a declaration of 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. Declare the property private String url = "smb://userName:[email protected]/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 connection method public static synchronized SmbUtil getInstance(String url) { if (smbUtil == null) return new SmbUtil(url); return 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("Connection 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("File transfer end..."); } 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(e); } catch (UnknownHostException e) { e.printStackTrace(); System.out.println("Host not 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. Test public static void main(String[] args) { // Server address format smb://computer username: computer password@computer IP address/IP shared folder String remoteUrl = "smb://wangqinghua:[email protected]/mars/"; String localFile = "F:/Switch production and sales enterprise directory.xls"; // Local file to be uploaded File file = new File(localFile); SmbUtil smb = SmbUtil.getInstance(remoteUrl); smb.uploadFile(file);// Upload file} }Things to note:
The above is based on the LAN, and the directory or folder that uploads files must be set to share mode.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.