java ftp可以直接转绝对路径么 D:\\FT\GM 请问一下,java中有没直接判断ftp上文件夹下是否存在某文...

JAVA\u7f16\u5199FTP\u8fde\u63a5\u62a5\u9519java.net.ConnectException: Connection refused: connect FTP

\u4f60\u7528\u7684FTPClient\u5f15\u5165\u4e0d\u5bf9\u5427\uff0c\u6211\u4eec\u9879\u76ee\u4e0a\u90fd\u662f\u7528\u7684
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;
\u4e0b\u9762\u662f\u6211\u4eec\u9879\u76ee\u4e0a\u7528\u5230\u7684FTP\u7684\u5b9e\u73b0\u4ee3\u7801\uff08FTP\u9700\u8981\u5148\u8fde\u63a5\uff0c\u518d\u767b\u5f55\uff0c\u4e4b\u540e\u5c31\u662f\u6821\u9a8c\u767b\u5f55\u662f\u5426\u6210\u529f\uff09\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a
/** * \u83b7\u53d6FTPClient\u5bf9\u8c61 * * @param ftpHost FTP\u4e3b\u673a\u670d\u52a1\u5668 * @param ftpPassword FTP \u767b\u5f55\u5bc6\u7801 * @param ftpUserName FTP\u767b\u5f55\u7528\u6237\u540d * @param ftpPort FTP\u7aef\u53e3 \u9ed8\u8ba4\u4e3a21 * @return FTPClient * @throws Exception */ public static FTPClient getFTPClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort) throws Exception { try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpHost, ftpPort);// \u8fde\u63a5FTP\u670d\u52a1\u5668 ftpClient.login(ftpUserName, ftpPassword);// \u767b\u9646FTP\u670d\u52a1\u5668 if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { logger.error("\u672a\u8fde\u63a5\u5230FTP\uff0c\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef!"); ftpClient.disconnect(); return null; } else { logger.info("FTP\u8fde\u63a5\u6210\u529f!"); return ftpClient; } } catch (SocketException socketException) { logger.error("FTP\u7684IP\u5730\u5740\u53ef\u80fd\u9519\u8bef\uff0c\u8bf7\u6b63\u786e\u914d\u7f6e!"); throw socketException; } catch (IOException ioException) { logger.error("FTP\u7684\u7aef\u53e3\u9519\u8bef,\u8bf7\u6b63\u786e\u914d\u7f6e!"); throw ioException; } }

\u7b2c\u4e00\u4e2a\u79cd\u65b9\u6cd5 \uff1a
org.apache.commons.net.ftp.* \u770b\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u662f\u5426\u6709\u4f60\u8981\u7684\u65b9\u6cd5
\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a


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();
}

}

/** \u5224\u65adFtp\u76ee\u5f55\u662f\u5426\u5b58\u5728,\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u76ee\u5f55 */
public void isDirExist(FtpClient ftpClient, String dir)
{
try
{
ftpClient.cd(dir); //\u60f3\u4e0d\u5230\u4ec0\u4e48\u597d\u529e\u6cd5\u6765\u5224\u65ad\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c\u53ea\u80fd\u7528\u5f02\u5e38\u4e86(\u6bd4\u8f83\u7b28).\u8bf7\u77e5\u9053\u7684\u544a\u8bc9\u6211\u4e00\u58f0`
} catch (IOException e1)
{
ftpClient.sendServer("MKD " + dir + "\r\n");
try
{
ftpClient.readServerResponse();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}

ftp服务器的目录不是你来设置的,你只相当于是一个访问ftp的客户端,所以你这里连接上ftp服务器后到了ftp的根目录,你就是要切换也只能写/XXXX,不可以写绝对路径。

扩展阅读:免费的服务器地址 ... java python ... www.sony.com.cn ... java sm4解密 ... java入门 ... java实现ftp文件上传 ... java sftp ... filezilla server ... 免费ftp服务器软件哪个好 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网