Java判断Ftp服务器目录是否存在,若不存在创建目录 .

 解决方法1:

 

package com.soft4j.log4j;

import java.io.IOException;

import sun.net.ftp.FtpClient;

public class FtpTest
{
 static String middle_ftpServer = "10.103.2.250";
 static String middle_user = "ora9iftp";
 static String middle_password = "ftp";
 static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";

 public static void main(String[] args)
 {
  FtpClient ftpClient = new FtpClient();
  try
  {
   ftpClient.openServer(middle_ftpServer);
   ftpClient.login(middle_user, middle_password);
   FtpTest ft =  new FtpTest();
   ft.isDirExist(ftpClient, middle_dir);
  } catch (IOException e)
  {
   e.printStackTrace();
  }

 }

 /** 判断Ftp目录是否存在,如果不存在则创建目录 */
 public void isDirExist(FtpClient ftpClient, String dir)
 {
  try
  {
   ftpClient.cd(dir);  //想不到什么好办法来判断目录是否存在,只能用异常了(比较笨).请知道的告诉我一声`
  } catch (IOException e1)
  {
   ftpClient.sendServer("MKD " + dir + "/r/n");
   try
   {
    ftpClient.readServerResponse();
   } catch (IOException e)
   {
    e.printStackTrace();
   }
  }
 }
}

 

解决方法2:

	public void md(String path) throws Exception {
		fc.sendServer("XMKD " + path+"\r\n");
	}

 

你可能感兴趣的:(java)