1 ftp服务器需要先下载ftp服务器,


2 然后安装完ftp服务器后,创建域(会有设置服务器的ip等)


3 创完域后,创建用户(会有账号、密码、设置根目录、权限)


4 可以通过浏览器访问:http://服务器ip 


5 在浏览器中输入ftp路径后,出现登陆界面,输入账号和密码。点击登陆


java后台操作:




6 连接ftp服务器。先创建FTPClient对象,然后创建FTPClientConfig对象,然后连接服务器、登陆账户、设置文件类型为二进制类型。
如:

ftpClient=new FTPClient();//创建FTPClient对象 



 FTPClientConfig ftpClientConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);//创建FTPClientConfig,设置操作系统 

 ftpClientConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);//设置字符集 



 ftpClient.configure(ftpClientConfig); 



 ftpClient.connect(server,port);//连接服务器 

 connect_flag=ftpClient.login(username, password);//登陆 

 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置文件类型为二进制类型




7 断开连接ftp服务器,通过ftpClient的disconnnect()方法。
如:i

f(ftpClient!=null&&ftpClient.isConnected()){ 

ftpClient.disconnect();//断开服务器连接 

connect_flag=false; 

 }




8 上传文件到ftp服务器。上传有三个步骤:设置目的、创建上传服务、上传
如:

ftpClient.changeWorkingDirectory("/");//设置上传到的目录为根目录 

ftpClient.remoteStore(ftpFileName);//创造远程传输服务器 

flag=ftpClient.storeFile(ftpFileName, inputStream);//传输文件



9 从ftp服务器上下载文件,下载有1步,检索下载文件。
如:

InputStream inputStream=null; 

 inputStream=ftpClient.retrieveFileStream(ftpFileName);//检索ftp服务器上的文件,并返回文件输入流




10 带路径的文件上传到ftp服务器上:如上传到/aa/bb/cc.txt 。aa和bb是文件夹,如果没有该文件夹需要创建,FTPClient有两个函数:changeWorkingDirectory方法和makeDirectory方法。前者是改变工作目录,后者是创建工作目录。
如:

String path=ftpFileName.substring(0,ftpFileName.lastIndexOf("/")); 

 createAndChangeWorkingFtpDir(path); //创造文件夹路径。如果没有则创建, 

 String fileName=ftpFileName.substring(ftpFileName.lastIndexOf("/")+1); 

 ftpClient.remoteStore(fileName);//设置上传服务 

 ftpClient.storeFile(fileName, inputStream);//上传文件




完整的例子:

package com.util; 



 import java.io.BufferedReader; 

 import java.io.BufferedWriter; 

 import java.io.File; 

 import java.io.FileInputStream; 

 import java.io.FileNotFoundException; 

 import java.io.FileOutputStream; 

 import java.io.IOException; 

 import java.io.InputStream; 

 import java.io.InputStreamReader; 

 import java.io.OutputStream; 

 import java.net.SocketException; 



 import org.apache.commons.net.ftp.FTP; 

 import org.apache.commons.net.ftp.FTPClient; 

 import org.apache.commons.net.ftp.FTPClientConfig; 



 public class FTPUtils { 



private FTPClient ftpClient; 

private boolean connect_flag=false; 

public FTPUtils() { 

super(); 

// TODO Auto-generated constructor stub 

} 

 

public boolean isConnect(){ 

return this.connect_flag; 

} 

 

/**连接FTP服务器,并登陆 

* @param server 

* @param port 

* @param username 

* @param password 

*/ 

public void connectServer(String server,int port,String username,String password){ 

//设置FTPClientConfig FTP客户端的配置 :操作系统、字符集 

FTPClientConfig ftpClientConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX); 

ftpClientConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING); 

 

ftpClient=new FTPClient(); 

ftpClient.configure(ftpClientConfig); 

 

try { 

ftpClient.connect(server,port);//连接服务器 

connect_flag=ftpClient.login(username, password);//登陆 

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置文件类型为二进制类型 

} catch (SocketException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

connect_flag=false; 

} catch(IOException e){ 

e.printStackTrace(); 

} 

System.out.println("connect_flag:"+this.connect_flag); 

 

} 

 

/**断开FTP的登陆 

* @throws IOException 

*/ 

public void disConnect() throws IOException{ 

if(ftpClient!=null&&ftpClient.isConnected()){ 

ftpClient.disconnect(); 

connect_flag=false; 

} 

} 

 

/**上传文件到ftp服务器汇总 

* @param inputStream待上传的文件流 

* @param ftpFileName准备放到ftp服务器上的文件名 

* @return 

*/ 

public boolean uploadFile(InputStream inputStream,String ftpFileName){ 

boolean flag=false; 

try { 

ftpClient.changeWorkingDirectory("/");//设置上传到的目录为根目录 

ftpClient.remoteStore(ftpFileName);//创造远程传输服务器 

flag=ftpClient.storeFile(ftpFileName, inputStream);//传输文件 

System.out.println("upload:"+flag); 

} catch (IOException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

flag=false; 

}//设置上传的目的地的目录 

return flag; 

} 

 

/**上传文件,可以带路径 

* @param inputStream 

* @param ftpFileName 

* @return 

* @throws IOException 

*/ 

public boolean uploadFileForPath(InputStream inputStream,String ftpFileName) throws IOException{ 

String path=ftpFileName.substring(0,ftpFileName.lastIndexOf("/")); 

System.out.println(path); 

 

createAndChangeWorkingFtpDir(path); 

 

String fileName=ftpFileName.substring(ftpFileName.lastIndexOf("/")+1); 

System.out.println("file:"+fileName); 

ftpClient.remoteStore(fileName); 

return ftpClient.storeFile(fileName, inputStream); 

//createFtpDir() 

} 

 

/**创建ftp文件夹 

* @param path 

* @throws IOException 

*/ 

public void createAndChangeWorkingFtpDir(String path) throws IOException{ 

String root="/"; 

ftpClient.changeWorkingDirectory(root); 

String path_array[]=path.split("/"); 

for(int i=1;i<path_array.length;i++){ 

String path_=path_array[i]; 

root=root+"/"+path_; 

boolean flag=ftpClient.changeWorkingDirectory(root);//改变工作目录 

if(!flag){ 

ftpClient.makeDirectory(path_);//创建目录 

ftpClient.changeWorkingDirectory(root); 

} 

//ftpClient.m 

} 

} 

 

/**从ftp服务器上下载文件 

* @param ftpFileName ftp服务器上的文件名称 从根目录开始,根目录为/ 

* @return 输出流 

*/ 

public InputStream downloadFile(String ftpFileName){ 

InputStream inputStream=null; 

try { 

//ftpClient.retrieveFile(ftpFileName,fileOutputStream); 

inputStream=ftpClient.retrieveFileStream(ftpFileName);//检索ftp服务器上的文件,并返回文件输入流 

} catch (IOException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

return inputStream; 

 

} 

 

public static void main(String[] args) { 

FTPUtils ftpUtils=new FTPUtils(); 

if(!ftpUtils.isConnect()){ 

ftpUtils.connectServer("127.0.0.1", 21, "test", "123456"); 

} 

 

/*try { 

ftpUtils.uploadFile(new FileInputStream(new File("C:/aa.txt")), "ckcore_ftp111.txt"); 

} catch (FileNotFoundException e1) { 

// TODO Auto-generated catch block 

e1.printStackTrace(); 

System.out.println("file not found"); 

}*/ 

 

 

 

InputStream fileInputStream=ftpUtils.downloadFile("/aa/bb.txt"); 

BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream)); 

String neirong=null; 

try { 

while((neirong=bufferedReader.readLine())!=null){ 

System.out.println(neirong); 

} 

} catch (IOException e1) { 

// TODO Auto-generated catch block 

e1.printStackTrace(); 

}/**/ 

 

/*try { 

ftpUtils.uploadFileForPath(new FileInputStream(new File("C:/aa.txt")), "/bb/cc/dd/bb112.txt"); 

} catch (FileNotFoundException e1) { 

// TODO Auto-generated catch block 

e1.printStackTrace(); 

} catch (IOException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

}*/ 

 

//String path="/aa/bb"; 

//System.out.println(path.split("/")); 

//System.out.println("ddd"); 

try { 

ftpUtils.disConnect(); 

} catch (IOException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

//if(!ftpUtils.isConnect()){ 

//ftpUtils.connectServer("127.0.0.1", 21, "test", "123456"); 

//} 

} 

 

 }