ftp图片上传

springboot ftp图片上传实现

1.application.yml配置

ftp:
  host: 192.168.XX.XXX
  port: 21
  username: XXX
  password: XXX
  savePath: XXX
  downloadUrl: /XXX/
  readImgUrl: http://192.168.XX.XXX:8080/XXX/

2.pom.xml配置

<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.6</version>
</dependency>

3.FtpConfig代码

package cn.trawe.pay.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix = "ftp")
public class FtpConfig {

    private String host;
    private int port;
    private String username;
    private String password;
    private String savePath;
    private String downloadUrl;
    private String readImgUrl;
}

4.pom.xml配置

<dependency>
package cn.trawe.pay.utils;

import cn.trawe.pay.config.FtpConfig;
import cn.trawe.util.L;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class FtpUtils {
    /**
     * 保存文件到ftp
     * @param file
     * @param
     */
    public static String uploadFiles(MultipartFile file, FtpConfig config) {
        String url = "";
        //无上传的文件
        if(file == null || file.isEmpty()){
            log.info(L.b("FtpUtils").bt("uploadFiles").m("*****上传图片到FTP服务器失败,未发现图片*****").s());
            return "";
        }
        //获取文件名
        String originalName = file.getOriginalFilename();
        String outFileName = System.currentTimeMillis() + originalName;

        FTPClient ftp = new FTPClient();
        try{
            ftp.connect(config.getHost(),config.getPort());
            boolean loginSuccess = ftp.login(config.getUsername(),config.getPassword());
            if(loginSuccess){//登录成功
                int reply = ftp.getReplyCode();
                if(FTPReply.isPositiveCompletion(reply)){
                    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                    ftp.setRemoteVerificationEnabled(false);
                    ftp.changeWorkingDirectory(config.getSavePath());//保存路径

                    //上传文件
                    boolean success = ftp.storeFile(outFileName,file.getInputStream());
                    if(!success){
                        log.info(L.b("FtpUtils").bt("uploadFiles").m("*****上传图片到FTP服务器失败*****").s());
                        return "";
                    }
                    //url = config.getDownloadUrl() + outFileName;//下载路径
                    url = outFileName;//下载路径
                    log.info(L.b("FtpUtils").bt("uploadFiles").m("*****上传图片到FTP服务器成功").d("图片url:",url).s());
                }else{//没有应答
                    log.info(L.b("FtpUtils").bt("uploadFiles").m("*****FTP服务器无应答*****").s());
                }
            }else{//登录失败
                log.info(L.b("FtpUtils").bt("uploadFiles").m("*****FTP服务器登录失败*****").s());
            }
        }catch (Exception e){
            log.info(L.b("FtpUtils").bt("uploadFiles").m("*****上传图片到FTP服务器异常*****").s());
        }
//        finally {
//            try {
//                ftp.logout();
//                ftp.disconnect();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        return url;
    }

}

你可能感兴趣的:(Java)