java判断ftp是否连接_校验FTP是否能正常连接的Java类

展开全部

先写一个异常类,如果FTP的用户名或者密码不正确e68a8462616964757a686964616f31333337623465就通过这个异常类抛出异常,代码如下:FTPException.java

public class FTPException extends Exception {

private int replyCode = -1;

public FTPException(String msg) {

super(msg);

}

public FTPException(String msg, String replyCode) {

super(msg);

try {

this.replyCode = Integer.parseInt(replyCode);

}

catch (NumberFormatException ex) {

this.replyCode = -1;

}

}

public int getReplyCode() {

return replyCode;

}

}

FtpUtil.java校验类代码如下:

import java.io.*;

import java.net.Socket;

public class FtpUtils {

static Logger logger = Logger.getLogger(FtpUtils.class);

public static boolean debugResponsesUtil = false;

public static PrintWriter printlog = null;

public static boolean getFtpLogin(String ip, String port, String username, String password){

boolean flag = false;   //默认FTP未连接

printlog = new PrintWriter(System.out);

debugResponsesUtil = true;

try {

Socket controlSock = new Socket(ip, Integer.parseInt(port));

if (controlSock == null) {

try{

throw new IllegalStateException("Failed to set timeout - no control socket");

} catch (IllegalStateException e) {

logger.error(e.getMessage());

e.printStackTrace();

return flag;

}

}

controlSock.setSoTimeout(0);

InputStream is = controlSock.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

// output stream

OutputStream os = controlSock.getOutputStream();

Writer writer = new OutputStreamWriter(os);

String replyStr1 = curReadReply(reader);        //第一次调用curReadReply函数

String replyCodeStr1 = replyStr1.substring(0, 3);

String replyText1 = replyStr1.substring(4);

if (replyCodeStr1.equals("220")) {

debugResponsesUtil = false;

} else {

// if unexpected reply, throw an exception

try {

throw new FTPException(replyText1, replyCodeStr1);

} catch (FTPException e) {

logger.error(e.getMessage());

e.printStackTrace();

return flag;

}

}

if (debugResponsesUtil) {

printlog.println("---> " + "USER " + username);

}

//send it

writer.write("USER " + username + "\r\n");

writer.flush();

String replyStr2 = curReadReply(reader);        //第二次调用curReadReply函数

String replyCodeStr2 = replyStr2.substring(0, 3);

String replyText2 = replyStr2.substring(4);

if (replyCodeStr2.equals("331")) {

debugResponsesUtil = false;

} else {

//if unexpected reply, throw an exception

try {

throw new FTPException(replyText2, replyCodeStr2);

} catch (FTPException e) {

logger.error(e.getMessage());

e.printStackTrace();

return flag;

}

}

if (debugResponsesUtil) {

printlog.println("---> " + "PASS " + password);

}

//send it

writer.write("PASS " + password + "\r\n");

writer.flush();

String replyStr3 = curReadReply(reader);        //第三次调用curReadReply函数

String replyCodeStr3 = replyStr3.substring(0, 3);

String replyText3 = replyStr3.substring(4);

if (replyCodeStr3.equals("230")) {

debugResponsesUtil = false;

} else {

//if unexpected reply, throw an exception

try {

throw new FTPException(replyText3, replyCodeStr3);

} catch (FTPException e) {

logger.error(e.getMessage());

e.printStackTrace();

return flag;

}

}

controlSock.setSoTimeout(1000);

flag = true;   //FTP连接成功

controlSock.setSoTimeout(10000);

} catch (IOException e) {

logger.error(e.getMessage());

e.printStackTrace();

}

return flag;

}

private static String curReadReply(BufferedReader reader){

String firstLine = null;

try {

firstLine = reader.readLine();

} catch (IOException e) {

logger.error(e.getMessage());

e.printStackTrace();

}

if (firstLine == null || firstLine.length() == 0) {

try{

throw new IOException("Unexpected null reply received");

} catch (IOException e) {

logger.error(e.getMessage());

e.printStackTrace();

}

}

StringBuffer reply = new StringBuffer(firstLine);

if (debugResponsesUtil) {

printlog.println(reply.toString());

}

String replyCode = reply.toString().substring(0, 3);

//check for multiline response and build up

//the reply

if (reply.charAt(3) == '-') {

boolean complete = false;

while (!complete) {

String line = null;

try {

line = reader.readLine();

} catch (IOException e) {

logger.error(e.getMessage());

e.printStackTrace();

}

if (line == null) {

try{

throw new IOException("Unexpected null reply received");

} catch (IOException e) {

logger.error(e.getMessage());

e.printStackTrace();

}

}

if (debugResponsesUtil) {

printlog.println(line);

}

if (line.length() > 3 && line.substring(0, 3).equals(replyCode) && line.charAt(3) == ' ') {

reply.append(line.substring(3));

complete = true;

} else {  //not the last line

reply.append(" ");

reply.append(line);

}

}  //end while

}  //end if

return reply.toString();

}

}

摘自:iteye博客地址如下:

http://shihuan830619.iteye.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值