Knowledge Points:
Springboot Backend project, receive the front-end frame upload to the picture, upload the image to the FTP image server
Note: In the process of uploading may appear back, you can create a folder, but the picture cannot upload the problem;
Tried many ways on the web, and finally changed the filename to full path upload success
1. Introduction of dependent packages in Pom.xml
<!--add to upload file components--
<!--https://mvnrepository.com/artifact/commons-net/commons-net--
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
2.ftp Upload Tool class Ftpfileutil.java
Package com.hand.hand.util;
Import org.apache.commons.net.ftp.FTPClient;
Import org.apache.commons.net.ftp.FTPReply;
Import java.io.IOException;
Import Java.io.InputStream;
/**
* Created by Nishuai on 2018/1/12.
*/
public class Ftpfileutil {
FTP Server IP Address
private static final String ftp_address = "101.132.1.113";
Port number
private static final int ftp_port = 21;
User name
private static final String Ftp_username = "FTP";
Password
private static final String Ftp_password = "123456";
Picture path
private static final String Ftp_basepath = "/home/ftpadmin/health/images";
public static Boolean uploadfile (String originfilename,inputstream input) {
Boolean success = false;
ftpclient ftp = new FtpClient ();
Ftp.setcontrolencoding ("GBK");
try {
int reply;
Ftp.connect (ftp_address, ftp_port);//Connect FTP server
Ftp.login (Ftp_username, Ftp_password);//Login
Reply = Ftp.getreplycode ();
if (! Ftpreply.ispositivecompletion (Reply)) {
Ftp.disconnect ();
return success;
}
Ftp.setfiletype (Ftpclient.binary_file_type);
Ftp.makedirectory (Ftp_basepath);
Ftp.changeworkingdirectory (Ftp_basepath);
Ftp.storefile (Originfilename,input);
Input.close ();
Ftp.logout ();
Success = true;
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (ftp.isconnected ()) {
try {
Ftp.disconnect ();
} catch (IOException IoE) {
}
}
}
return success;
}
}
3.controller Layer Interface Ftpfileuploadcontroller.java
Package Com.hand.hand.controller;
Import Com.hand.hand.util.FtpFileUtil;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.*;
Import Org.springframework.web.multipart.MultipartFile;
Import Javax.servlet.http.HttpServletRequest;
Import java.io.IOException;
Import Java.io.InputStream;
/**
* Created by Nishuai on 2017/12/26.
*/
@CrossOrigin
@Controller
public class Ftpfileuploadcontroller {
FTP processing File Upload
@RequestMapping (value= "/ftpuploadimg", method = Requestmethod.post)
Public @ResponseBody String uploadimg (@RequestParam ("file") Multipartfile file,
HttpServletRequest request) throws IOException {
String fileName = File.getoriginalfilename ();
InputStream Inputstream=file.getinputstream ();
String Filepath=null;
Boolean Flag=ftpfileutil.uploadfile (Filename,inputstream);
if (flag==true) {
SYSTEM.OUT.PRINTLN ("FTP Upload succeeded! ");
Filepath=filename;
}
return filePath; The path picture name, the front end frame can be used Ngnix the specified path +filepath to access the picture in the Ngnix picture server
}
}
Springboot Integrated FTP upload image function