Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize File upload by integrating ftp with springboot

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report