package com.bcxin.autodownloadupload.common.utils;

import java.io.InputStream;
import java.nio.file.Paths;

/**
 * @Auther linqinglin
 * @date 2022/9/23 14:40
 */
public class FtpUtil {


    public static boolean bcxUploadFile(BcxinFtpClient ftpClient, String pathFile, InputStream input){
        try {
            String ftpPath = pathFile.substring(0, pathFile.lastIndexOf("/")+1);
            String fileName = pathFile.substring(pathFile.lastIndexOf("/") + 1);
            boolean isDir = ftpClient.isARemoteDirectory(ftpPath);
            if (!isDir) {
                boolean createDirectoryFlag = ftpClient.createDirectory(ftpPath);
                if (!createDirectoryFlag) {
                } else {
                }
            }
            String filePath = ftpPath + fileName;
            String fileDir = Paths.get(filePath).getParent().toString().replace("\\", "/");
            if (!ftpClient.isARemoteDirectory(fileDir)) {
                boolean createDirectoryFlag = ftpClient.createDirectory(fileDir);
                if (!createDirectoryFlag) {
                } else {
                }
            }

            filePath = filePath.replace("\\", "/");

            return ftpClient.uploadFile(input, filePath);
        }catch (Exception e){
            e.printStackTrace();
        }

        return false;

    }

}
