展开全部
先写一个异常类,如果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/