Upload images locally to the tool class on the server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.