• JAVA


    FTP上传文件工具类

    package org.jeecg.modules.system.util;
    
    
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    import java.util.Vector;
    
    import com.jcraft.jsch.*;
    
    
    public class FtpUtils {
        /**
         * 链接sftp
         * @param host  主机
         * @param port    端口
         * @param username   用户名
         * @param password  密码
         * @return
         */
        public  static ChannelSftp connect(String host, int port, String username,String password) throws JSchException {
    
            ChannelSftp sftp = null;
                JSch jsch = new JSch();
                jsch.getSession(username, host, port);
                Session sshSession = jsch.getSession(username, host, port);
                System.out.println("Session创建成功");
                sshSession.setPassword(password);
                System.out.println("密码输入成功");
                Properties sshConfig = new Properties();
                sshConfig.put("StrictHostKeyChecking", "no");
                System.out.println("链接参数设置成功");
                sshSession.setConfig(sshConfig);
                sshSession.connect();
                System.out.println("Session已连接");
                Channel channel = sshSession.openChannel("sftp");
                channel.connect();
                sftp = (ChannelSftp) channel;
                System.out.println("连接到主机" + host + ".");
            return sftp;
        }
    
        /**
         * 文件重命名
         * @param directory 目录
         * @param oldname 旧文件名
         * @param newname 新文件名
         * @param sftp
         */
        public  static  void renameFile(String directory, String oldname, String newname,
                                        ChannelSftp sftp) {
            try {
                sftp.cd(directory);
                sftp.rename(oldname, newname);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 文件上传
         * @param directory 目录
         * @param uploadFile 要上传的文件名
         * @param sftp
         */
        public  static  void upload(String directory, String uploadFile, ChannelSftp sftp) throws SftpException, FileNotFoundException {
                //url路径上传
    //            URL url = new URL(uploadFile);
    //            URLConnection connection = url.openConnection();
    //            String sss = url.getFile();
                sftp.cd(directory);
                File file = new File(uploadFile);
                Date dNow = new Date();
                SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
                sftp.put(new FileInputStream(file), ft.format(dNow) + file.getName());
    //            sftp.put(connection.getInputStream(), file.getName());
        }
    
    
        public  static  void upload(String directory,String sftpFileName,InputStream input, ChannelSftp sftp) throws SftpException{
            try {
                sftp.cd(directory);
            } catch (Exception e) {
            }
            sftp.put(input, sftpFileName);
        }
    
        /**
         * 文件下载
         * @param directory 目录
         * @param downloadFile 要下载文件名
         * @param saveFile 保持的文件名
         * @param sftp
         */
        public  static void download(String directory, String downloadFile,
                                     String saveFile, ChannelSftp sftp) {
            try {
                sftp.cd(directory);
                File file = new File(saveFile);
                sftp.get(downloadFile, new FileOutputStream(file));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 文件删除
         * @param directory 目录
         * @param deleteFile 要删除的文件名
         * @param sftp
         */
        public  static  void delete(String directory, String deleteFile, ChannelSftp sftp) {
            try {
                sftp.cd(directory);
                sftp.rm(deleteFile);
                System.out.println("删除成功");
            } catch (Exception e) {
                System.out.println("删除失败");
                e.printStackTrace();
            }
        }
    
        /**
         * 列出目录下的文件
         * @param directory 目录
         * @param sftp
         * @return
         * @throws SftpException
         */
        public  static  Vector listFiles(String directory, ChannelSftp sftp)
                throws SftpException {
            return sftp.ls(directory);
        }
    
    
        //批量删除文件
        public  static void delete(String directory, String[] fileNames, ChannelSftp aa) {
            for (String fileName : fileNames) {
                delete(directory, fileName, aa);
            }
        }
    
        /**
         * 创建目录文件夹
         * @param directory  要创建文件夹的位置路径
         * @param fileName  要创建文件夹的名称
         * @param sftp   sftp连接
         */
        public  static  void mkdir(String directory,String fileName,ChannelSftp sftp){
            try {
                sftp.cd(directory);
                sftp.mkdir(fileName);
                System.out.println("文件夹创建成功");
            } catch (Exception e) {
                System.out.println("文件夹创建失败");
                e.printStackTrace();
            }
        }
    
    
        public  static  void  main(String[] args) throws  Exception{
            Date dNow = new Date();
            SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
            String str = ft.format(dNow);
            System.out.println(str);
    //        ChannelSftp  cftp=FtpUtils.connect("101.32.253.149",22, "root", "Happywork666");
    //        FtpUtils.upload("/usr", "https://cdn.starmini.jp/mini-program-starpay-cloud-s3/2021/06/17/1165ddd7-6f06-401b-b747-d4c0e8389b1d.zip", cftp);
    
        }
    }

    参数实体

    package org.jeecg.modules.system.entity;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableLogic;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.fasterxml.jackson.annotation.JsonFormat;
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    import org.jeecgframework.poi.excel.annotation.Excel;
    import org.springframework.format.annotation.DateTimeFormat;
    
    import java.io.Serializable;
    import java.util.Date;
    import java.util.List;
    
    /**
     * @Description: 邮件通知配置
     * @Author: jeecg-boot
     * @Date:   2021-03-10
     * @Version: V1.0
     */
    @Data
    @Accessors(chain = true)
    @EqualsAndHashCode(callSuper = false)
    @ApiModel(value="gaia_ftp_config对象", description="ftp通知配置")
    public class GaiaFtpConfig implements Serializable {
        private static final long serialVersionUID = 1L;
        /**FTP服务器地址*/
        @Excel(name = "FTP服务器地址", width = 15)
        @ApiModelProperty(value = "FTP服务器地址")
        private String host;
        /**FTP服务器端口*/
        @Excel(name = "FTP服务器端口", width = 15)
        @ApiModelProperty(value = "FTP服务器端口")
        private Integer port;
        /**ftp用户名*/
        @Excel(name = "ftp用户名", width = 15)
        @ApiModelProperty(value = "ftp用户名")
        private String username;
        /**ftp密码*/
        @Excel(name = "ftp密码", width = 15)
        @ApiModelProperty(value = "ftp密码")
        private String password;
        /**存储目录*/
        @Excel(name = "存储目录", width = 15)
        @ApiModelProperty(value = "存储目录")
        private String directory;
        /**文件路径*/
        @Excel(name = "文件路径", width = 15)
        @ApiModelProperty(value = "文件路径")
        private List<String> attachment;
    }
  • 相关阅读:
    /usr/bin/ld: 找不到 /usr/lib64/libatomic.so.1.0.0
    linux Version mismatch error. This is libtool 2.4.6 Debian-2.4.6-2
    java播放语音文件
    java冒泡排序
    java递归求和
    常见芯片标号说明
    keil5 新建 stm32项目 步骤
    st-link 升级固件时报错“is not in the DFU mode”
    数码管 段选位选
    C51 定时器
  • 原文地址:https://www.cnblogs.com/ljknlb/p/14926840.html
Copyright © 2020-2023  润新知