k94314517
2024-03-17 947688a33cd6b76917eedd3765e88bd05fdeb20d
server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java
@@ -29,6 +29,10 @@
@Slf4j
public class FtpUtil {
   public FTPClient ftpClient = new FTPClient();
   public static String hostname;
   public static int port;
   public static String username;
   public static  String password;
   public FtpUtil() {
      // 设置将过程中使用到的命令输出到控制台
@@ -39,26 +43,22 @@
   public FtpUtil(String hostname, int port, String username, String password)
         throws IOException {
      // 设置将过程中使用到的命令输出到控制台
      connect(hostname, port, username, password);
      FtpUtil.hostname =hostname;
      FtpUtil.port =port;
      FtpUtil.username =username;
      FtpUtil.password =password;
      connect();
   }
   /** */
   /**
    * 连接到FTP服务器
    *
    * @param hostname
    *            主机名
    * @param port
    *            端口
    * @param username
    *            用户名
    * @param password
    *            密码
    * @return 是否连接成功
    * @throws IOException
    */
   public boolean connect(String hostname, int port, String username,
         String password) throws IOException {
   public boolean connect() throws IOException {
      ftpClient.connect(hostname, port);
      ftpClient.setControlEncoding("GBK");
      if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
@@ -243,8 +243,8 @@
         ftpClient.setControlEncoding("GBK");
         // 对远程目录的处理
         String remoteFileName = remote;
         if (remote.contains(File.separator)) {
            remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1);
         if (remote.contains("/")) {
            remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);
            // 创建服务器远程目录结构,创建失败直接返回
            if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) {
               log.error("创建ftp目录失败======================="+remote);
@@ -298,8 +298,8 @@
      String result;
      // 对远程目录的处理
      String remoteFileName = remote ;
       if (remote.contains(File.separator)) {
         remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1);
       if (remote.contains("/")) {
         remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);
         // 创建服务器远程目录结构,创建失败直接返回
         if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) {
            return "2";
@@ -372,20 +372,20 @@
         throws IOException {
      String status = "1";
      // UploadStatus status = UploadStatus.Create_Directory_Success;
      String directory = remote.substring(0, remote.lastIndexOf(File.separator) + 1);
      if (!directory.equalsIgnoreCase(File.separator)
      String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
      if (!directory.equalsIgnoreCase("/")
            && !ftpClient.changeWorkingDirectory(new String(directory
            .getBytes("GBK"), "iso-8859-1"))) {
         // 如果远程目录不存在,则递归创建远程服务器目录
         int start = 0;
         int end = 0;
         if (directory.startsWith(File.separator)) {
         if (directory.startsWith("/")) {
            start = 1;
         } else {
            start = 0;
         }
         end = directory
               .indexOf(File.separator, start);
               .indexOf("/", start);
         while (true) {
            String subDirectory = new String(remote.substring(start, end)
                  .getBytes("GBK"), "iso-8859-1");
@@ -400,7 +400,7 @@
            }
            start = end + 1;
            end = directory.indexOf(File.separator,
            end = directory.indexOf("/",
                  start);
            // 检查所有目录是否创建完毕
@@ -541,7 +541,7 @@
   public static void main(String[] args) throws Exception {
      try {
         FtpUtil ftpUtil = new FtpUtil("175.27.187.84", 21, "ftpuser","doumee168" );
         ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+File.separator, UUID.randomUUID().toString()+"test.jpg");
         ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+"/", UUID.randomUUID().toString()+"test.jpg");
         /*FtpUtil myFtp = new FtpUtil("106.15.54.228", 21, "ftptlg",
               "tlg168.com");
         System.out.println(myFtp.upload("D:\\devices.sql", "/1111/devices.sql"));
@@ -631,7 +631,7 @@
         String remoteDirectoryPath) throws Exception {
      File src = new File(localDirectory);
      try {
         remoteDirectoryPath =  remoteDirectoryPath  + File.separator;
         remoteDirectoryPath =  remoteDirectoryPath  + "/";
         boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath);
      }catch (IOException e) {
         e.printStackTrace();
@@ -645,7 +645,7 @@
         String tPath = remoteDirectoryPath+f.getName();
         if (!f.isDirectory()) {
            int dNum = getNumFromStr(tPath,File.separator.toCharArray()[0]);
            int dNum = getNumFromStr(tPath,"/".toCharArray()[0]);
            uploadFile(f, tPath);
            if(dNum -2>=0){
               for (int i = 0; i < dNum-1; i++) {
@@ -654,7 +654,7 @@
            }
         }else{
            uploadDirectory(srcName,
                  tPath+File.separator);
                  tPath+"/");
         }
      }
      return true;
@@ -666,7 +666,7 @@
    * */
   public boolean delDirectory(String pathName ) throws Exception {
      try {
         this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf(File.separator)) );
         this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf("/")) );
         ftpClient.removeDirectory(pathName);
      }catch (IOException e) {
         e.printStackTrace();