文件管理類java連接ftp
㈠ java通過ftp 連接ftp伺服器成功,怎麼list文件
1.使用的FileZillaServer開源免費軟體,安裝過後建立的本地FTP伺服器。2.使用的apache上下載FTP工具包,引用到工程目錄中。3.IDE,Eclipse,JDK6上傳和下載目錄的實現原理:對每一個層級的目錄進行判斷,是為目錄類型、還是文件類型。如果為目錄類型,採用遞歸調用方法,檢查到最底層的目錄為止結束。如果為文件類型,則調用上傳或者下載方法對文件進行上傳或者下載操作。貼出代碼:(其中有些沒有代碼,可以看看,還是很有用處的)!
㈡ java 連接 ftp 需要的 知識
這個網上有很多例子程序的,去開源中國看看吧。 http://www.oschina.net/code/search?q=java+ftp
㈢ 關於JAVA FTP連接後文件列表中的中文是亂碼
需要設置文件傳輸的格式,有2中格式 1:asicc 。2:binary格式 也就是二進制格式,並且ftpClient提供了相應的方法,asicc(),barry(),你要在連接ftp的時候加上此方法,ftpClient.binary();
㈣ Java FTPClient 連接FTP,上傳文件,不能以中文保存
在連接之前設置ftpClient.setControlEncoding("GBK");連接之後再設置是沒有作用的。
㈤ java 連接ftp是主動模式還是被動模式
一.FTP的PORT(主動模式)和PASV(被動模式)
 1. PORT(主動模式)
   PORT中文稱為主動模式,工作的原理: FTP客戶端連接到FTP伺服器的21埠,發送用戶名和密碼登錄,登錄成功後要list列表或者讀取數據時,客戶端隨機開放一個埠(1024以上),發送 PORT命令到FTP伺服器,告訴伺服器客戶端採用主動模式並開放埠;FTP伺服器收到PORT主動模式命令和埠號後,通過伺服器的20埠和客戶端開放的埠連接,發送數據.
2. PASV(被動模式)
   PASV是Passive的縮寫,中文成為被動模式,工作原理:FTP客戶端連接到FTP伺服器的21埠,發送用戶名和密碼登錄,登錄成功後要list列表或者讀取數據時,發送PASV命令到FTP伺服器, 伺服器在本地隨機開放一個埠(1024以上),然後把開放的埠告訴客戶端, 客戶端再連接到伺服器開放的埠進行數據傳輸。 
二.兩種模式的比較
    從上面的運行原來看到,主動模式和被動模式的不同簡單概述為: 主動模式傳送數據時是「伺服器」連接到「客戶端」的埠;被動模式傳送數據是「客戶端」連接到「伺服器」的埠。
    主動模式需要客戶端必須開放埠給伺服器,很多客戶端都是在防火牆內,開放埠給FTP伺服器訪問比較困難。
    被動模式只需要伺服器端開放埠給客戶端連接就行了。
三.不同工作模式的網路設置
    實際項目中碰到的問題是,FTP的客戶端和伺服器分別在不同網路,兩個網路之間有至少4層的防火牆,伺服器端只開放了21埠, 客戶端機器沒開放任何埠。FTP客戶端連接採用的被動模式,結果客戶端能登錄成功,但是無法LIST列表和讀取數據。很明顯,是因為伺服器端沒開放被動模式下的隨機埠導致。
    由於被動模式下,伺服器端開放的埠隨機,但是防火牆要不能全部開放,解決的方案是,在ftp伺服器配置被動模式下開放隨機埠在 50000-60000之間(范圍在ftp伺服器軟體設置,可以設置任意1024上的埠段),然後在防火牆設置規則,開放伺服器端50000-60000之間的埠端。
    主動模式下,客戶端的FTP軟體設置主動模式開放的埠段,在客戶端的防火牆開放對應的埠段。
四.如何設置 工作模式
   實時上FTP伺服器一般都支持主動和被動模式,連接採用何種模式是有FTP客戶端軟體決定。
㈥ JAVA 如何實現FTP遠程路徑
package nc.ui.doc.doc_007;  
 
import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 
import nc.itf.doc.DocDelegator;  
 import nc.vo.doc.doc_007.DirVO;  
 import nc.vo.pub.BusinessException;  
 import nc.vo.pub.SuperVO;  
 
import org.apache.commons.net.ftp.FTPClient;  
 import org.apache.commons.net.ftp.FTPFile;  
 import org.apache.commons.net.ftp.FTPReply;  
 import org.apache.commons.net.ftp.FTP;  
public class FtpTool {  
private FTPClient ftp;  
 
private String romateDir = "";  
 
private String userName = "";  
 
private String password = "";  
 
private String host = "";  
 
private String port = "21";  
 
public FtpTool(String url) throws IOException {  
 //String url="ftp://user:password@ip:port/ftptest/psd";  
 int len = url.indexOf("//");  
 String strTemp = url.substring(len + 2);  
 len = strTemp.indexOf(":");  
 userName = strTemp.substring(0, len);  
 strTemp = strTemp.substring(len + 1);  
 
len = strTemp.indexOf("@");  
 password = strTemp.substring(0, len);  
 strTemp = strTemp.substring(len + 1);  
 host = "";  
 len = strTemp.indexOf(":");  
 if (len < 0)//沒有設置埠  
 {  
 port = "21";  
 len = strTemp.indexOf("/");  
 if (len > -1) {  
 host = strTemp.substring(0, len);  
 strTemp = strTemp.substring(len + 1);  
 } else {  
 strTemp = "";  
 }  
 } else {  
 host = strTemp.substring(0, len);  
 strTemp = strTemp.substring(len + 1);  
 len = strTemp.indexOf("/");  
 if (len > -1) {  
 port = strTemp.substring(0, len);  
 strTemp = strTemp.substring(len + 1);  
 } else {  
 port = "21";  
 strTemp = "";  
 }  
 }  
 romateDir = strTemp;  
 ftp = new FTPClient();  
 ftp.connect(host, FormatStringToInt(port));  
 
}  
 
public FtpTool(String host, int port) throws IOException {  
 ftp = new FTPClient();  
 ftp.connect(host, port);  
 }  
 
public String login(String username, String password) throws IOException {  
 this.ftp.login(username, password);  
 return this.ftp.getReplyString();  
 }  
 
public String login() throws IOException {  
 this.ftp.login(userName, password);  
 System.out.println("ftp用戶: " + userName);  
 System.out.println("ftp密碼: " + password);  
 if (!romateDir.equals(""))  
 System.out.println("cd " + romateDir);  
 ftp.changeWorkingDirectory(romateDir);  
 return this.ftp.getReplyString();  
 }  
 
public boolean upload(String pathname, String filename) throws IOException, BusinessException {  
 
int reply;  
 int j;  
 String m_sfilename = null;  
 filename = CheckNullString(filename);  
 if (filename.equals(""))  
 return false;  
 reply = ftp.getReplyCode();  
 if (!FTPReply.isPositiveCompletion(reply)) {  
 ftp.disconnect();  
 System.out.println("FTP server refused connection.");  
 System.exit(1);  
 }  
 FileInputStream is = null;  
 try {  
 File file_in;  
 if (pathname.endsWith(File.separator)) {  
 file_in = new File(pathname + filename);  
 } else {  
 file_in = new File(pathname + File.separator + filename);  
 }  
 if (file_in.length() == 0) {  
 System.out.println("上傳文件為空!");  
 return false;  
 }   
   //產生隨機數最大到99   
   j = (int)(Math.random()*100);  
   m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名  
 is = new FileInputStream(file_in);  
 ftp.setFileType(FTP.BINARY_FILE_TYPE);  
 ftp.storeFile(m_sfilename, is);  
 ftp.logout();  
} finally {  
 if (is != null) {  
 is.close();  
 }  
 }  
 System.out.println("上傳文件成功!");  
 return true;  
 }  
public boolean delete(String filename) throws IOException {  
 
FileInputStream is = null;  
 boolean retValue = false;  
 try {  
 retValue = ftp.deleteFile(filename);  
 ftp.logout();  
 } finally {  
 if (is != null) {  
 is.close();  
 }  
 }  
 return retValue;  
 
}  
 
public void close() {  
 if (ftp.isConnected()) {  
 try {  
 ftp.disconnect();  
 } catch (IOException e) {  
 e.printStackTrace();  
 }  
 }  
 }  
 
public static int FormatStringToInt(String p_String) {  
 int intRe = 0;  
 if (p_String != null) {  
 if (!p_String.trim().equals("")) {  
 try {  
 intRe = Integer.parseInt(p_String);  
 } catch (Exception ex) {  
 
}  
 }  
 }  
 return intRe;  
 }  
 
public static String CheckNullString(String p_String) {  
 if (p_String == null)  
 return "";  
 else  
 return p_String;  
 }  
 
public boolean downfile(String pathname, String filename) {  
 
String outputFileName = null;  
 boolean retValue = false;  
 try {  
 FTPFile files[] = ftp.listFiles();  
 int reply = ftp.getReplyCode();  
 
////////////////////////////////////////////////  
 if (!FTPReply.isPositiveCompletion(reply)) {  
 try {  
 throw new Exception("Unable to get list of files to dowload.");  
 } catch (Exception e) {  
 // TODO Auto-generated catch block  
 e.printStackTrace();  
 }  
 }  
 
/////////////////////////////////////////////////////  
 if (files.length == 0) {  
 System.out.println("No files are available for download.");  
 }else {  
 for (int i=0; i <files.length; i++) {  
 System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());  
 outputFileName = pathname + filename + ".pdf";  
 //return outputFileName;  
 File f = new File(outputFileName);  
 
//////////////////////////////////////////////////////  
 retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));  
 
if (!retValue) {  
 try {  
 throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");  
 } catch (Exception e) {  
 // TODO Auto-generated catch block  
 e.printStackTrace();  
 }  
 }  
 
}  
 }  
 
/////////////////////////////////////////////////////////////  
 } catch (IOException e) {  
 // TODO Auto-generated catch block  
 e.printStackTrace();  
 }  
 return retValue;  
 }  
 
}
㈦ 求用java寫一個ftp伺服器客戶端程序。
import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
 String initDir;
 initDir = "D:/Ftp";
 ServerSocket server;
 Socket socket;
 String s;
 String user;
 String password;
 user = "root";
 password = "123456";
 try{
 System.out.println("MYFTP伺服器啟動....");
 System.out.println("正在等待連接....");
 //監聽21號埠
 server = new ServerSocket(21);
 socket = server.accept();
 System.out.println("連接成功");
 System.out.println("**********************************");
 System.out.println("");
 
 InputStream in =socket.getInputStream();
 OutputStream out = socket.getOutputStream();
 
 DataInputStream din = new DataInputStream(in);
 DataOutputStream dout=new DataOutputStream(out);
 System.out.println("請等待驗證客戶信息....");
 
 while(true){
  s = din.readUTF();
  if(s.trim().equals("LOGIN "+user)){
   s = "請輸入密碼:";
   dout.writeUTF(s);
   s = din.readUTF();
   if(s.trim().equals(password)){
    s = "連接成功。";
    dout.writeUTF(s);
    break;
   }
   else{s ="密碼錯誤,請重新輸入用戶名:";<br>   dout.writeUTF(s);<br>   <br>   }
   }
  else{
   s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";
   dout.writeUTF(s);
   }
 }
 System.out.println("驗證客戶信息完畢...."); while(true){
  System.out.println("");
  System.out.println("");
  s = din.readUTF();
  if(s.trim().equals("DIR")){
   String output = "";  
   File file = new File(initDir);
   String[] dirStructure = new String[10];
   dirStructure= file.list();
   for(int i=0;i<dirStructure.length;i++){
    output +=dirStructure[i]+"\n";
   }
   s=output;
   dout.writeUTF(s);
  }
  else if(s.startsWith("GET")){
   s = s.substring(3);
   s = s.trim();
   File file = new File(initDir);
   String[] dirStructure = new String[10];
   dirStructure= file.list();
   String e= s;
   int i=0;
   s ="不存在";
   while(true){
    if(e.equals(dirStructure[i])){
      s="存在";
      dout.writeUTF(s);
      RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
      byte byteBuffer[]= new byte[1024];
      int amount;
      while((amount = outFile.read(byteBuffer)) != -1){
      dout.write(byteBuffer, 0, amount);break;
      }break;
      
     }
    else if(i<dirStructure.length-1){
     i++;
     }
     else{
      dout.writeUTF(s);
     break;
     }
     }
   }
  else if(s.startsWith("PUT")){
   s = s.substring(3);
   s = s.trim();
   RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
   byte byteBuffer[] = new byte[1024];
   int amount;
   while((amount =din.read(byteBuffer) )!= -1){
   inFile.write(byteBuffer, 0, amount);break;
  }
  }
  else if(s.trim().equals("BYE"))break;
  else{
   s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";
   dout.writeUTF(s);
 }
 } 
  
 din.close();
 dout.close();
 in.close();
 out.close();
 socket.close();
}
catch(Exception e){
 System.out.println("MYFTP關閉!"+e);
 
}
}}
㈧ 如何用JAVA實現FTP訪問
在主函數中,完成伺服器埠的偵聽和服務線程的創建。我們利用一個靜態字元串變數initDir 來保存伺服器線程運行時所在的工作目錄。伺服器的初始工作目錄是由程序運行時用戶輸入的,預設為C盤的根目錄。 
具體的代碼如下: 
public class ftpServer extends Thread{
private Socket socketClient;
private int counter;
private static String initDir;
public static void main(String[] args){
if(args.length != 0) {
initDir = args[0];
}else{ initDir = "c:";}
int i = 1;
try{
System.out.println("ftp server started!");
//監聽21號埠
ServerSocket s = new ServerSocket(21);
for(;;){
//接受客戶端請求
Socket incoming = s.accept();
//創建服務線程
new ftpServer(incoming,i).start();
i++;
}
}catch(Exception e){}
}
㈨ 怎麼用java實現ftp的登陸
/**
*依賴commons-net-3.4.jar,commons-io-2.4.jar
*/
publicclassFtpUtils{
/**
*上傳
*@paramhostFTP地址
*@paramport埠ftp默認22,sftp默認23
*@paramuserftp用戶名
*@parampwdftp密碼
*@paramdestPathFTP文件保存路徑
*@paramfileNameftp保存文件名稱
*@paramfile需要上傳的文件
*/
publicstaticvoipload(Stringhost,intport,Stringuser,Stringpwd,StringdestPath,StringfileName,Filefile){
FTPClientftp=null;
InputStreamfis=null;
try{
//1.建立連接
ftp=newFTPClient();
ftp.connect(host,port);
//2.驗證連接地址
intreply=ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
return;
}
//3.登錄
ftp.login(user,pwd);
//設置上傳路徑、緩存、字元集、文件類型等
ftp.changeWorkingDirectory(destPath);
ftp.setBufferSize(1024);
ftp.setControlEncoding("UTF-8");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//4.上傳
fis=newFileInputStream(file);
ftp.storeFile(fileName,fis);
}catch(SocketExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
IOUtils.closeQuietly(fis);
try{
if(ftp.isAvailable()){
ftp.logout();
}
if(ftp.isConnected()){
ftp.disconnect();
}
//刪除上傳臨時文件
if(null!=file&&file.exists()){
file.delete();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
