温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot如何通过集成ftp实现文件上传

发布时间:2022-02-25 15:15:10 来源:亿速云 阅读:542 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“springboot如何通过集成ftp实现文件上传”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“springboot如何通过集成ftp实现文件上传”这篇文章吧。

1、FileUtil

package 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;

@Component
public class FileUtil {

  //ftp服务器ip地址
  @Value("${custom.config.file-server.ip}") // 相关配置放在application.properties 中
    String FTP_ADDRESS;
  //端口号
  @Value("${custom.config.file-server.port}")
  Integer FTP_PORT;
  //用户名
  @Value("${custom.config.file-ftp-user}")
  String FTP_USERNAME;
  //密码
  @Value("${custom.config.file-ftp-password}")
  String FTP_PASSWORD;
  //图片路径
  @Value("${custom.config.file-savepath}")
  String FTP_BASEPATH;

  //根据当前文件生成 文件夹
  private static String getTimePath() {
    Date now = new Date();

    DateFormat format = new SimpleDateFormat("yyyy/MM/dd/");
    return format.format(now);
  }


  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);// 连接FTP服务器
      ftp.login(FTP_USERNAME, FTP_PASSWORD);// 登录
      reply = ftp.getReplyCode();
      System.out.println("reply:" + reply);

      ftp.enterLocalPassiveMode();//开启被动模式,否则文件上传不成功,也不报错

      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("文件上传失败");
    } finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        } catch (IOException ioe) {
        }
      }
    }

    return url.toString();
  }

  // 创建文件夹,并切换到该文件夹
  // 比如: hello/test
  //最终会切换到test 文件夹返回
  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服务器ip地址
custom.config.file-server.ip=111.229.65.208
#String FTP_ADDRESS;
#//端口号
custom.config.file-server.port=21
#Integer FTP_PORT;
#//用户名
custom.config.file-ftp-user=root
#String FTP_USERNAME;
#//密码
custom.config.file-ftp-password=123456
#String FTP_PASSWORD;
#//图片路径
custom.config.file-savepath=/upload
#String FTP_BASEPATH;

3、UploadController

 /**
  * 上传文件
  */
String fileUrl = fileUtil.upload(file.getInputStream(), file.getOriginalFilename());

得到路径:/upload2020/11/23/1606122890137.jpg

4、ftp查看

以上是“springboot如何通过集成ftp实现文件上传”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI