導航:首頁 > IDC知識 > ftp伺服器java

ftp伺服器java

發布時間:2020-12-17 23:17:31

1、求用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);

}
}}

2、如何測試java開發ftp伺服器

http的話就用httpclient。open後,可以返回一個InputStream。這個就是你要讀到文件流。原理的話,參考你版用瀏覽器打開這權個鏈接顯示的內容。這個返回的是一個HTML網頁,需要你解析出裡面的文字(一般來說取body中間的內容就行)其實對於這種文件一般用FTP來下載的。樓上寫的那個不對,哈哈。需要的話自己最好去查一下,怎麼用,我有代碼,不過告訴你的話也不太好?URLurl=newURL("http://你的地址");URLConnectionconnection=url.openConnection();InputStreamis=connection.getInputStream();BufferedReaderbr=newBufferedReader(newInputStreamReader(is,"gb2312"));下面就是解析這個字元串來,自己來吧

3、如何搭建ftp伺服器 java

可以用Socket ,歸根結底還是對IO流進行處理, 根據協議處理不同的命令,比如客戶端發送「獲取服務端目錄結構」 的命令,服務端接收後向客戶端寫出目錄結構數據,客戶端根據協議解析並顯示

4、java怎麼打開FTP伺服器上的文件

可以用文件流的方式直接寫到伺服器,也可以先在本地生成再調用ftp介面上傳到伺服器

5、java中怎麼實現ftp伺服器

我知道apache有個commons net包,其中的copyFTPClient類可以實現客戶端和服務之間的文件傳輸,但是我如果使用這種方式的話,就得將一台伺服器上的文件傳到我本地,再將這個文件傳到另一台伺服器上,感覺這中間多了一步操作;

6、如何用Java實現FTP伺服器

1.使用的FileZillaServer開源免費軟體,安裝過後建立的本地FTP伺服器。2.使用的apache上下載專FTP工具包,引用到工程目錄屬中。3.IDE,Eclipse,JDK6上傳和下載目錄的實現原理:對每一個層級的目錄進行判斷,是為目錄類型、還是文件類型。如果為目錄類型,採用遞歸調用方法,檢查到最底層的目錄為止結束。如果為文件類型,則調用上傳或者下載方法對文件進行上傳或者下載操作。貼出代碼:(其中有些沒有代碼,可以看看,還是很有用處的)!

7、請問有java上傳文件到ftp伺服器的demo嗎,感激不盡

/**
* 依賴commons-net-3.4.jar, commons-io-2.4.jar
*/
public class FtpUtils {
/**
* 上傳
* @param host FTP地址
* @param port 埠ftp默認22,sftp默認23
* @param user ftp用戶名
* @param pwd ftp密碼
* @param destPath FTP文件保存路徑
* @param fileName ftp保存文件名稱
* @param file 需要上傳的文件
*/
public static void upload(String host, int port,String user, String pwd, String destPath, String fileName, File file){
FTPClient ftp = null;
InputStream fis = null;
try {
//1.建立連接
ftp = new FTPClient();
ftp.connect(host, port);
//2.驗證連接地址
int reply = 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 = new FileInputStream(file);
ftp.storeFile(fileName, fis);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
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 (IOException e) {
e.printStackTrace();
}
}
}
}

8、JAVA怎麼實現刪除遠程FTP伺服器上的某一文件

一個JAVA 實現FTP功能的代碼,包括了伺服器的設置模塊,並包括有上傳文件至FTP的通用方法、下載文件的通用方法以及刪除文件、在ftp伺服器上傳文件夾、檢測文件夾是否存在等,裡面的有些代碼對編寫JAVA文件上傳或許有參考價值,

(1):Java FTP主文件代碼:

package ftpDemo;

import java.io.DataOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import sun.net.TelnetInputStream;

import sun.net.TelnetOutputStream;

import sun.net.ftp.FtpClient;

public class ftpUtil {

// 上傳文件至FTP通用方法

public static void upLoadFileFtp(KmConfig kmConfig,InputStream is, String fileName){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。

ftpClient.login(userName, passWord);// userName、passWord分別為FTP伺服器的登陸用戶名和密碼

ftpClient.binary();

ftpClient.cd(path);// path為FTP伺服器上保存上傳文件的路徑。

TelnetOutputStream telnetOut = ftpClient.put(fileName);// fileName為上傳的文件名

DataOutputStream dataOut = new DataOutputStream(telnetOut);

byte buffer[] = new byte[ * ];

int count = ;

while ((count = is.read(buffer)) != -) {

dataOut.write(buffer, , count);

}

telnetOut.close();

dataOut.close();

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("上傳文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");

}

}

// 刪除文件至FTP通用方法

public static void deleteFileFtp(KmConfig kmConfig,String fileName){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。

ftpClient.login(userName, passWord);// userName、passWord分別為FTP伺服器的登陸用戶名和密碼

ftpClient.binary();

ftpClient.cd(path);// path為FTP伺服器上保存上傳文件的路徑。

try {

ftpClient.sendServer("dele " + fileName + " ");

} catch (Exception e) {

System.out.println("刪除文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");

}

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("刪除文件失敗!");

}

}

// 下載ftp文件

public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。

ftpClient.login(userName, passWord);// userName、passWord分別為FTP伺服器的登陸用戶名和密碼

ftpClient.binary();

ftpClient.cd(path);// path為FTP伺服器上保存上傳文件的路徑。

try {

TelnetInputStream in = ftpClient.get(fileName);

byte[] bytes = new byte[];

int cnt=;

while ((cnt=in.read(bytes,,bytes.length)) != -) {

outputStream.write(bytes, , cnt);

}

outputStream.close();

in.close();

} catch (Exception e) {

ftpClient.closeServer();

e.printStackTrace();

}

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("下載文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");

}

}

//在ftp伺服器上傳件文件夾

public boolean createDir(String path,FtpClient ftpClient) throws Exception{

//進入到home文件夾下

ftpClient.cd("/home");

//創建遠程文件夾

//遠程命令包括

//USER  PORT  RETR  ALLO  DELE  SITE  XMKD  CDUP  FEAT<br>

// PASS  PASV  STOR  REST  CWD STAT  RMD XCUP  OPTS<br>

// ACCT  TYPE  APPE  RNFR  XCWD  HELP  XRMD  STOU  AUTH<br>

// REIN  STRU  SMNT  RNTO  LIST  NOOP  PWD SIZE  PBSZ<br>

// QUIT  MODE  SYST  ABOR  NLST  MKD XPWD  MDTM  PROT<br>

//  在伺服器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上/r/n<br>

//  ftpclient.sendServer("XMKD /test/bb/r/n"); //執行伺服器上的FTP命令<br>

//  ftpclient.readServerResponse一定要在sendServer後調用<br>

//  nameList("/test")獲取指目錄下的文件列表<br>

//  XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯<br>

//  XRMD刪除目錄<br>

//  DELE刪除文件<br>

//通過遠程命令 穿件一個files文件夾

ftpClient.sendServer("MKD "+ path + " ");

//這個方法必須在 這兩個方法中間調用 否則 命令不管用

ftpClient.binary();

ftpClient.readServerResponse();

return false;

}

/**

* 檢查文件夾是否存在

* @param dir

* @param ftpClient

* @return

*/

public boolean isDirExist(String dir, FtpClient ftpClient) {

try {

ftpClient.cd(dir);

} catch (Exception e) {

return false;

}

return true;

}

}

(2):KmConfig.java代碼如下:定義FTP伺服器參數,包括登錄的用戶名密碼之類的。

package ftpDemo;

public class KmConfig {

//主機ip

private String FtpHost = "";

//埠號

private int FtpPort;

//ftp用戶名

private String FtpUser = "";

//ftp密碼

private String FtpPassword = "";

//ftp中的目錄

private String FtpPath = "";

public String getFtpHost() {

return FtpHost;

}

public void setFtpHost(String ftpHost) {

FtpHost = ftpHost;

}

public int getFtpPort() {

return FtpPort;

}

public void setFtpPort(int ftpPort) {

FtpPort = ftpPort;

}

public String getFtpUser() {

return FtpUser;

}

public void setFtpUser(String ftpUser) {

FtpUser = ftpUser;

}

public String getFtpPassword() {

return FtpPassword;

}

public void setFtpPassword(String ftpPassword) {

FtpPassword = ftpPassword;

}

public String getFtpPath() {

return FtpPath;

}

public void setFtpPath(String ftpPath) {

FtpPath = ftpPath;

}

}

(3):下面是測試代碼:

9、如何用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){}
}

10、java如何實現將FTP文件轉移到另一個FTP伺服器上

你有FTPClient就比較好辦,假如你的兩台FTP伺服器分別為fs1和fs2

在本地開發代碼思路如下:

通過專FTPClient連接上fs1,然後下載屬(可以循環批量下載)到本地伺服器,保存到一個臨時目錄。

下載完成後,FTPClient斷開與fs1的連接,記得必須logout。

本地伺服器通過FileInputStream將剛下載到臨時目錄的文件讀進來,得到一個List<File>集合。

通過FTPClient連接上fs2,循環List<File>集合,將文件上傳至fs2的特定目錄,然後清空臨時目錄,上傳完畢後,斷開fs2的連接,同樣必須logout。

與ftp伺服器java相關的知識