当前位置: 技术问答>java相关
向高手请教ftp
来源: 互联网 发布时间:2015-03-07
本文导语: 下面是我一个小ftp代码,问题都在里面有注释,多谢!! import java.io.*; import java.net.*; public class Class1 { String hostName; //主机地址 String userName; //用户名 String pass; //密码 static final int PORT=21; Socket clientsocket; PrintWr...
下面是我一个小ftp代码,问题都在里面有注释,多谢!!
import java.io.*;
import java.net.*;
public class Class1
{
String hostName; //主机地址
String userName; //用户名
String pass; //密码
static final int PORT=21;
Socket clientsocket;
PrintWriter dataOut ;
BufferedReader dataIn;
String datareply; //从FTP服务器返回的字符串
boolean successful;
public Class1(String ip)
{
hostName=ip;
userName="tanhs";
pass="mykey";
}
public boolean login() throws IOException, InterruptedIOException
{
StringBuffer ftpReply=new StringBuffer();
clientsocket=new Socket(hostName,PORT);
clientsocket.setSoTimeout(5000);
dataOut=new PrintWriter(new OutputStreamWriter(clientsocket.getOutputStream()));
dataIn=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
ftpReply.append(readReply().trim());
if(ftpReply.toString().startsWith("220"))
if(comm("USER "+userName)) //键入用户名,无误
if(comm("PASS "+pass)) //键入密码,无误
successful=true;
comm("TYPE A"); //定义类型,无误----这个地方也是个问题,我感觉应该用"ASCII",但这个确实有效,用"ASCII"反而会出错
return successful;
}
public boolean getFile(String ftpfileName) throws IOException, InterruptedIOException
{
successful=false;
String fileName=ftpfileName;
if(comm("GET "+fileName+" d:\reply.txt"))
successful=true;
return successful;
}
protected boolean comm(String command)throws IOException
{
boolean success=false;
dataOut.println(command);
dataOut.flush();
if (command.startsWith("GET"))
{
datareply=getReply(); //此处得到服务器返回信息
success=datareply.startsWith("226")?true:false;
}
else
datareply=readReply();
if(command.startsWith("USER"))
{
success=datareply.startsWith("331")?true:false;
}
else if(command.startsWith("PASS"))
{
success=datareply.startsWith("230")?true:false;
}
else if(command.equals("TYPE A"))
{
success=datareply.startsWith("200")?true:false;
}
return success;
}
protected String readReply()throws IOException, InterruptedIOException
{
StringBuffer reply=new StringBuffer();
do {
reply.append(dataIn.readLine());
}
while(dataIn.ready());
datareply=reply.toString();
System.out.print(datareply);
return datareply;
}
protected String getReply()throws IOException, InterruptedIOException
{
StringBuffer getreply=new StringBuffer();
/*int i=0;
String datatemp;
while((datatemp=dataIn.readLine())!=null && i
import java.io.*;
import java.net.*;
public class Class1
{
String hostName; //主机地址
String userName; //用户名
String pass; //密码
static final int PORT=21;
Socket clientsocket;
PrintWriter dataOut ;
BufferedReader dataIn;
String datareply; //从FTP服务器返回的字符串
boolean successful;
public Class1(String ip)
{
hostName=ip;
userName="tanhs";
pass="mykey";
}
public boolean login() throws IOException, InterruptedIOException
{
StringBuffer ftpReply=new StringBuffer();
clientsocket=new Socket(hostName,PORT);
clientsocket.setSoTimeout(5000);
dataOut=new PrintWriter(new OutputStreamWriter(clientsocket.getOutputStream()));
dataIn=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
ftpReply.append(readReply().trim());
if(ftpReply.toString().startsWith("220"))
if(comm("USER "+userName)) //键入用户名,无误
if(comm("PASS "+pass)) //键入密码,无误
successful=true;
comm("TYPE A"); //定义类型,无误----这个地方也是个问题,我感觉应该用"ASCII",但这个确实有效,用"ASCII"反而会出错
return successful;
}
public boolean getFile(String ftpfileName) throws IOException, InterruptedIOException
{
successful=false;
String fileName=ftpfileName;
if(comm("GET "+fileName+" d:\reply.txt"))
successful=true;
return successful;
}
protected boolean comm(String command)throws IOException
{
boolean success=false;
dataOut.println(command);
dataOut.flush();
if (command.startsWith("GET"))
{
datareply=getReply(); //此处得到服务器返回信息
success=datareply.startsWith("226")?true:false;
}
else
datareply=readReply();
if(command.startsWith("USER"))
{
success=datareply.startsWith("331")?true:false;
}
else if(command.startsWith("PASS"))
{
success=datareply.startsWith("230")?true:false;
}
else if(command.equals("TYPE A"))
{
success=datareply.startsWith("200")?true:false;
}
return success;
}
protected String readReply()throws IOException, InterruptedIOException
{
StringBuffer reply=new StringBuffer();
do {
reply.append(dataIn.readLine());
}
while(dataIn.ready());
datareply=reply.toString();
System.out.print(datareply);
return datareply;
}
protected String getReply()throws IOException, InterruptedIOException
{
StringBuffer getreply=new StringBuffer();
/*int i=0;
String datatemp;
while((datatemp=dataIn.readLine())!=null && i