Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize File upload by integrating ftp with springboot

Shulou Source: shulou.com Published: 2022-06-01 12:07:26 09月28日 Update

This article mainly shows you "springboot how to upload files through the integration of ftp", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "springboot how to achieve file upload through integrated ftp" this article.

1 、 FileUtilpackage io.renren.modules.oss.utils;import org.apache.commons.net.ftp.FTPClient;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import java.io.IOException;import java.io.InputStream;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date @ Componentpublic class FileUtil {/ / ftp server ip address @ Value ("${custom.config.file-server.ip}") / related configuration is placed in application.properties String FTP_ADDRESS; / / port number @ Value ("${custom.config.file-server.port}") Integer FTP_PORT; / / user name @ Value ("${custom.config.file-ftp-user}") String FTP_USERNAME / / password @ Value ("${custom.config.file-ftp-password}") String FTP_PASSWORD; / / Picture path @ Value ("${custom.config.file-savepath}") String FTP_BASEPATH; / / generate a folder private static String getTimePath () {Date now = new Date (); DateFormat format = new SimpleDateFormat ("yyyy/MM/dd/"); return format.format (now) based on the current file } public String upload (InputStream inputStream, String originName) {StringBuilder url = new StringBuilder (); FTPClient ftp = new FTPClient (); ftp.setControlEncoding ("GBK"); try {int reply; ftp.connect (FTP_ADDRESS, FTP_PORT); / / Connect to FTP server ftp.login (FTP_USERNAME, FTP_PASSWORD); / / Log in reply = ftp.getReplyCode (); System.out.println ("reply:" + reply) Ftp.enterLocalPassiveMode (); / / turn on passive mode, otherwise file upload will not succeed and String timePath = getTimePath (); String saveDir = FTP_BASEPATH + timePath; url.append (saveDir); ftp.setFileType (FTPClient.BINARY_FILE_TYPE); createDir (ftp, saveDir); / / ftp.makeDirectory (saveDir); / / ftp.changeWorkingDirectory (FTP_BASEPATH) OriginName= System.currentTimeMillis () + originName.substring (originName.lastIndexOf ('.')); url.append (originName); ftp.storeFile (originName, inputStream); inputStream.close (); ftp.logout ();} catch (IOException e) {throw new RuntimeException ("File upload failed");} finally {if (ftp.isConnected ()) {try {ftp.disconnect () } catch (IOException ioe) {} return url.toString ();} / / create a folder and switch to this folder / / for example: hello/test / / will eventually switch to the test folder and return private void createDir (FTPClient client, String path) throws IOException {String [] dirs = path.split ("/") For (String dir: dirs) {if (StringUtils.isEmpty (dir)) {continue;} if (! client.changeWorkingDirectory (dir)) {client.makeDirectory (dir);} client.changeWorkingDirectory (dir);} 2, application.properties#//ftp server ip address custom.config.file-server.ip=111.229.65.208#String FTP_ADDRESS # / / Port number, custom.config.file-server.port=21#Integer FTP_PORT;#// user name, custom.config.file-ftp-user=root#String FTP_USERNAME;#// password, custom.config.file-ftp-password=123456#String FTP_PASSWORD;#// picture path, custom.config.file-savepath=/upload#String FTP_BASEPATH;3, UploadController

/ * upload file * / String fileUrl = fileUtil.upload (file.getInputStream (), file.getOriginalFilename ())

Get the path: / upload2020/11/23/1606122890137.jpg

4. Ftp to view all the contents of the article "how to upload files by integrating ftp with springboot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files folders content server articles paths services slogans pictures addresses passwords users user names switching learning help success easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Redmi MySQL Shulou Technology