1, tool jar package dependent
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
2, the code is as follows
Package com.zbb.util;
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 Org.springframework.web.multipart.MultipartFile;
Import jcifs.smb.SmbException;
Import Jcifs.smb.SmbFile;
Import Jcifs.smb.SmbFileOutputStream;
/**
* Send files to remote server via SMB protocol
*
*/
public class Smbutil {
Private String URL = "";
Private Smbfile smbfile = null;
Private Smbfileoutputstream smbout = null;
private static Smbutil SMB = NULL;
/**
* Get the SMB and connect method
* @param URL
* @return
*/
public static synchronized Smbutil getinstance (String URL) {
if (SMB = = null)
return new Smbutil (URL);
return SMB;
}
Private smbutil (String URL) {
This.url = URL;
This.init ();
}
public void init () {
try {
System.out.println ("Connecting...url:" + this.url);
Smbfile = new Smbfile (This.url);
if (!smbfile.exists ()) {
Smbfile.mkdirs ();
}
Smbfile.connect ();
System.out.println ("Connect successfu...url:" + this.url);
}catch (Malformedurlexception e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Upload files to server
* @param file
* @return
*/
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 transmission complete ...");
}catch (Smbexception e) {
E.printstacktrace ();
}catch (Malformedurlexception e) {
E.printstacktrace ();
}catch (Unknownhostexception e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}finally{
try {
if (null! = This.smbout)
This.smbOut.close ();
if (null! = BF)
Bf.close ();
}catch (Exception E2) {
E2.printstacktrace ();
}
}
return flag;
}
/**
* Upload files to server
* @param file
* @return
*/
public int UploadFile (multipartfile file,string fileName) {
int flag =-1;
Bufferedinputstream BF = null;
try{
This.smbout = new Smbfileoutputstream (This.url + "/" + FileName, false);
BF = new Bufferedinputstream (File.getinputstream ());
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 transmission complete ...");
}catch (Smbexception e) {
E.printstacktrace ();
}catch (Malformedurlexception e) {
E.printstacktrace ();
}catch (Unknownhostexception e) {
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}finally{
try {
if (null! = This.smbout)
This.smbOut.close ();
if (null! = BF)
Bf.close ();
}catch (Exception E2) {
E2.printstacktrace ();
}
}
return flag;
}
public static void Main (string[] args) {
Smbutil SMB = smbutil.getinstance ("smb://Username: password @ server ip/commodity");
File File = new file ("e://server letter dd. txt");
Smb.uploadfile (file);//Upload file
}
}
3. Configuration information
User_name=administrator
User_pwd=akjq55**+6
user_ip=23.234.5.146
Pan=d
Folder=yhyhimageaddress
Upload images locally to the tool class on the server