JAVA上传FTP文件服务器

IFileService的实现类:

public class FileServiceImpl implements IFileService {
    private static Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Override
    public String upload(MultipartFile file, String path) {
        // 原始名称
        String filename = file.getOriginalFilename();
        //扩展名
        String fileExtensionName = filename.substring(filename.lastIndexOf(".") + 1);
        //文件名
        String uploadFileName = UUID.randomUUID().toString() + "." + fileExtensionName;
        logger.info("开始上传文件,上传文件的文件名:{},上传的路径:{},新文件名:{}", filename, path, uploadFileName);

        File fileDir = new File(path);
        if (!fileDir.exists()) {
            fileDir.setWritable(true);
            fileDir.mkdirs();
        }
        // 新文件
        File targetFile = new File(path, uploadFileName);
        try {
            file.transferTo(targetFile);
            //上传文件到FTP服务器上
            FTPUtil.uploadFile(Lists.newArrayList(targetFile));
            //上传完成后,删除缓存中的文件。
            targetFile.delete();
        } catch (Exception e) {
            logger.error("图片上传失败",e);
            return null;
        }
        return targetFile.getName();
    }
}

FTPUtil工具类

public class FTPUtil {
    private static Logger logger = LoggerFactory.getLogger(FTPUtil.class);


    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");


    private String ip ;
    private int port;
    private String user;
    private String pwd;
    private FTPClient ftpClient;



    public FTPUtil(String ip, int port, String user, String pwd) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
    }

    public static boolean uploadFile(List fileList) throws IOException {
        FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass);
        logger.info("开始连接FTP服务器");
        //放在FTP服务器根目录
        boolean result = ftpUtil.uploadFile("/",fileList);
        logger.info("开始连接FTP服务器,结束上传,上传结果:{}",result);
        return  result;
    }

    private boolean uploadFile(String remotePath,List fileList) throws IOException {
        boolean uploaded = true;
        FileInputStream fis =null;
        //连接FTP服务器
        if(connectServer(this.ip,this.port,this.user,this.pwd)){
            try {
                ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for(File fileItem : fileList){
                    fis = new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(),fis);
                }

            } catch (IOException e) {
                e.printStackTrace();
                logger.error("上传文件异常",e);
                uploaded = false;
            } finally {
                fis.close();
                ftpClient.disconnect();
            }
        }
        return  uploaded;
    }

    private boolean connectServer(String ip,int port ,String user,String pwd){
        boolean isSuccess = false;
        ftpClient =new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user,pwd);
            logger.info("连接{}",isSuccess);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("连接FTP服务器异常",e);
        }
        return  isSuccess;
    }

    public static String getFtpIp() {
        return ftpIp;
    }

    public static void setFtpIp(String ftpIp) {
        FTPUtil.ftpIp = ftpIp;
    }

    public static String getFtpUser() {
        return ftpUser;
    }

    public static void setFtpUser(String ftpUser) {
        FTPUtil.ftpUser = ftpUser;
    }

    public static String getFtpPass() {
        return ftpPass;
    }

    public static void setFtpPass(String ftpPass) {
        FTPUtil.ftpPass = ftpPass;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}

FTP上传文件war包依赖

<dependency>
            <groupId>org.apache.camelgroupId>
            <artifactId>camel-ftpartifactId>
            <version>2.13.2version>
        dependency>

你可能感兴趣的:(JAVA应用层)