1、給出一個C++或Java編寫的ftp伺服器程序
給你一個MFC寫的:
// FtpClient.h: interface for the CFtpServer class.
//
#if !defined(_FTPCLIENT_H)
#define _FTPCLIENT_H
#include <afxinet.h>
class CFtpClient
{
//構造/析構函數
public:
CFtpClient(const char *pszFtpIp, const char *pszFtpPort, const char *pszFtpUser, const char *pszFtpPassWord);
//功能:構造函數
//參數:pszFtpIp---------Ftp伺服器IP地址
// pszFtpPort-------Ftp伺服器埠
// pszFtpUser-------Ftp用戶名
// pszFtpPassWord---Ftp用戶密碼
//返回值:無
virtual ~CFtpClient();
//功能:析構函數
//參數:無
//返回值:無
//公有成員函數
public:
BOOL ConnectFtpServer();
//功能:連接FTP伺服器
//參數:無
//返回值:TRUE--連接成功,FALSE--連接失敗
void DisConnectFtpServer();
//功能:斷開與FTP伺服器的連接
//參數:無
//返回值:無
BOOL PutFileToFtpServer(const char *pszFilePath);
//功能:上傳指定的文件到FTP伺服器
//參數:要上傳的文件名
//返回值:TRUE--連接成功,FALSE--連接失敗
BOOL GetFileFromFtpServer(const char *pszFilePath);
//功能:從FTP伺服器下載指定文件
//參數:要下載的文件名
//返回值:TRUE--連接成功,FALSE--連接失敗
CString GetUpLoadFilePath();
//功能:得到上傳文件名
//參數:無
//返回值:得到的當前路徑
//私有成員變數
private:
char m_szFtpIp[20]; //Ftp伺服器IP地址
UINT m_uFtpPort; //Ftp伺服器埠
char m_szFtpUser[20]; //Ftp用戶名
char m_szFtpPassWord[20]; //Ftp用戶密碼
CInternetSession *m_pInetSession; //Internet會話對象指針
CFtpConnection *m_pFtpConnection; //FTP服務連接對象指針
};
#endif
// FtpClient.cpp: implementation of the CFtpServer class.
//
#include "FtpClient.h"
CFtpClient::CFtpClient(const char *pszFtpIp, const char *pszFtpPort, const char *pszFtpUser,const char *pszFtpPassWord)
{
strcpy(m_szFtpIp, pszFtpIp);
m_uFtpPort = atoi(pszFtpPort);
strcpy(m_szFtpUser, pszFtpUser);
strcpy(m_szFtpPassWord, pszFtpPassWord);
m_pInetSession = NULL;
m_pFtpConnection = NULL;
}
CFtpClient::~CFtpClient()
{
}
BOOL CFtpClient::ConnectFtpServer()
{
//創建Internet會話
m_pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS);
try
{
//連接Ftp伺服器
m_pFtpConnection = m_pInetSession->GetFtpConnection(m_szFtpIp, m_szFtpUser, m_szFtpPassWord, m_uFtpPort);
}
catch(CInternetException *pEx)
{
char szError[1024];
pEx->GetErrorMessage(szError, 1024);
pEx->Delete();
CLog Log;
Log.RecordLog("連接Ftp伺服器", CString("9999"), CString(szError));
return FALSE;
}
return TRUE;
}
void CFtpClient::DisConnectFtpServer()
{
if(m_pFtpConnection != NULL)
{
m_pFtpConnection->Close();
delete m_pFtpConnection;
m_pFtpConnection = NULL;
}
if(m_pInetSession !=NULL)
{
m_pInetSession->Close();
delete m_pInetSession;
m_pInetSession = NULL;
}
}
BOOL CFtpClient::PutFileToFtpServer(const char *pszFilePath)
{
//1. 判斷伺服器是否連接
if(m_pFtpConnection == NULL)
{
return FALSE;
}
char szLocalFile[200] = {0};
char szRemoteFile[200] = {0};
//2. 本地文件名
sprintf(szLocalFile, "%s", pszFilePath);
//3. 遠程文件名
char szDrive[10] = {0};
char szDir[210] = {0};
char szFileName[70] = {0};
char szExt[10] = {0};
_splitpath(pszFilePath, szDrive, szDir, szFileName, szExt);
sprintf(szRemoteFile, "%s\\%s.%s", g_strEnterpriseID, szFileName, szExt);
//4. 文件上傳
if(!m_pFtpConnection->PutFile(szLocalFile, szRemoteFile))
{
return FALSE;
}
return TRUE;
}
BOOL CFtpClient::GetFileFromFtpServer(const char *pszFilePath)
{
//1. 判斷伺服器是否連接
if(m_pFtpConnection == NULL)
{
return FALSE;
}
char szLocalFile[200] = {0};
char szRemoteFile[200] = {0};
//2. 本地文件名
sprintf(szLocalFile,"%s",pszFilePath);
//3. 遠程文件名
char szDrive[10] = {0};
char szDir[210] = {0};
char szFileName[70] = {0};
char szExt[10] = {0};
_splitpath(pszFilePath, szDrive, szDir, szFileName, szExt);
sprintf(szRemoteFile, "%s\\%s.%s", g_strEnterpriseID, szFileName, szExt);
//4. 文件下載
if(!m_pFtpConnection->GetFile(szRemoteFile, szLocalFile))
{
return FALSE;
}
return TRUE;
}
CString CFtpClient::GetUpLoadFilePath()
{
CString strPath = "", strDir = "";
//得到當前日期
CTime time = CTime::GetCurrentTime();
CString strDate = time.Format("%Y%m%d");
//得到上傳文件路徑
char filepath[MAX_PATH];
GetMoleFileName(NULL, filepath, MAX_PATH);
strDir.Format("%s", filepath);
strPath = strDir.Left(strDir.ReverseFind('\\')) + strDate + "\\*.txt";
return strPath;
}
//保存成兩個文件,然後添加到你的工程中就可以調用了。
2、可否幫我寫個用java編寫的ftp伺服器呢?謝謝啦~~
有現成的啊!apache提供了FtpServer
3、java中怎麼實現ftp伺服器
我知道apache有個commons net包,其中的copyFTPClient類可以實現客戶端和服務之間的文件傳輸,但是我如果使用這種方式的話,就得將一台伺服器上的文件傳到我本地,再將這個文件傳到另一台伺服器上,感覺這中間多了一步操作;
4、Java實現ftp伺服器源代碼
/**
* 創建日期:Dec 23, 2008
* 類名:Ftp.java
* 類路徑:org
* 修改日誌:
*/
package org;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* @author 南山一根蔥
* @Description ftp操作
*/
public class Ftp {
/**
* 獲取Ftp目錄下的列表
*/
public void getftpList() {
String server = "";// 輸入的FTP伺服器的IP地址
String user = "";// 登錄FTP伺服器的用戶名
String password = "";// 登錄FTP伺服器的用戶名的口令
String path = "";// FTP伺服器上的路徑
try {
FtpClient ftpClient = new FtpClient();// 創建FtpClient對象
ftpClient.openServer(server);// 連接FTP伺服器
ftpClient.login(user, password);// 登錄FTP伺服器
if (path.length() != 0) {
ftpClient.cd(path);
}
TelnetInputStream is = ftpClient.list();
int c;
while ((c = is.read()) != -1) {
System.out.print((char) c);
}
is.close();
ftpClient.closeServer();// 退出FTP伺服器
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 下載FTP上的文件
*/
public void getFtpFile() {
String server = "";// 輸入的FTP伺服器的IP地址
String user = "";// 登錄FTP伺服器的用戶名
String password = "";// 登錄FTP伺服器的用戶名的口令
String path = "";// FTP伺服器上的路徑
String filename = "";// 下載的文件名
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 上傳文件到FTP
*/
public void putFtpFile() {
String server = "";// 輸入的FTP伺服器的IP地址
String user = "";// 登錄FTP伺服器的用戶名
String password = "";// 登錄FTP伺服器的用戶名的口令
String path = "";// FTP伺服器上的路徑
String filename = "";// 上傳的文件名
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(filename);
File file_in = new File(filename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
5、《急!!!》用java實現FTP伺服器上文件下載,上傳。《急!!!》
package com.icss.oa.impdoc.file.quartz;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;
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.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Ftp客戶端工具,提供ftp上傳、下載、查看文件列表功能
*/
public class FtpClientUtil {
private FTPClient ftpClient;
public FtpClientUtil(String host, String user, String password, int port) {
= new FTPClient();
try {
ftpClient.connect(host, port);
ftpClient.login(user, password);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}
public FtpClientUtil() {
// TODO Auto-generated constructor stub
}
/**
* 驗證是否登錄成功
*
* @return
*/
public boolean isAuthenticated() {
return ftpClient.isAuthenticated();
}
/**
* 下載文件
* @param resFilePath 下載的文件原來所在的路徑
* @param targetFilePath 下載的文件准備存放的目的路徑
*/
public InputStream download(String resFilePath, File file) {
InputStream in = null;
try {
if(isAuthenticated()){
ftpClient.setType(FTPClient.TYPE_BINARY);
ftpClient.download(resFilePath, file);
}
} catch (Exception e) {
System.err.println("從FTP下載文件失敗:" + e.getMessage());
} finally {
disconnect();
}
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}
/**
* 獲取文件列表
* path 代表查詢該路徑下的文件或目錄
* @return
*/
public FTPFile[] getFileList(String path) {
FTPFile[] list = null;
FTPFile[] rtulist = null; //要返回的文件的列表
int filenum = 0; //記錄文件的個數
try {
if(isAuthenticated()){
list = ftpClient.list(path);
if(list==null || list.length==0)
return null;
//首先確定要找的內容的個數
for(int i=0;i<list.length;i++){
if(list[i].getType()==0)
filenum++;
}
rtulist = new FTPFile[filenum];
int j=0;
for(int i=0;i<list.length;i++){
if(list[i].getType()==0){
rtulist[j]=list[i];
j++;
}
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} catch (FTPDataTransferException e) {
e.printStackTrace();
} catch (FTPAbortedException e) {
e.printStackTrace();
} catch (FTPListParseException e) {
e.printStackTrace();
}finally{
disconnect();
}
return rtulist;
}
/**
* 獲取當前文件夾下的文件夾列表
* path 代表查詢該路徑下的文件或目錄
* @return
*/
public FTPFile[] getDirectoryList(String path) {
FTPFile[] list = null;
FTPFile[] rtulist = null; //要返回的文件夾的列表
int filenum = 0; //記錄文件夾的個數
try {
if(isAuthenticated()){
list = ftpClient.list(path);
if(list==null || list.length==0)
return new FTPFile[0] ;
//首先確定要找的內容的個數
for(int i=0;i<list.length;i++){
if(list[i].getType()==1)
if(!".".equals(list[i].getName()))
if(!"..".equals(list[i].getName()))
filenum++;
}
rtulist = new FTPFile[filenum];
int j=0;
for(int i=0;i<list.length;i++){
if(list[i].getType()==1)
if(!".".equals(list[i].getName()))
if(!"..".equals(list[i].getName())){
rtulist[j]=list[i];
j++;
}
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} catch (FTPDataTransferException e) {
e.printStackTrace();
} catch (FTPAbortedException e) {
e.printStackTrace();
} catch (FTPListParseException e) {
e.printStackTrace();
}finally{
disconnect();
}
return rtulist;
}
public String getCurrentDirectory() {
try {
return ftpClient.currentDirectory();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
return null;
}
public void disconnect(){
try {
ftpClient.disconnect(true);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}
/**
* 刪除文件
*
* @param fileName
*/
public void delete(String fileName) {
try {
ftpClient.deleteFile(fileName);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} finally {
disconnect();
}
}
public static void main(String[] args) {
DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy HH:mm");
System.out.println(DATE_FORMAT.format(new Date()));
try {
System.out.println(DATE_FORMAT.parse("09/01/10 03:28 PM"));
} catch (ParseException e) {
e.printStackTrace();
}
FtpClientUtil ftpClientUtil = new FtpClientUtil("127.0.0.1", "verycd", "verycd", 21);
System.out.println(ftpClientUtil.isAuthenticated());
System.out.println(ftpClientUtil.getCurrentDirectory());
System.out.println("文件查詢:");
System.out.println("=======================================");
FTPFile[] files = ftpClientUtil.getFileList("/");
if(files!=null){
for (int i = 0; i < files.length; i++) {
FTPFile ftpFile = files[i];
ftpClientUtil = new FtpClientUtil("127.0.0.1", "verycd", "verycd", 21);
ftpClientUtil.download("/" + ftpFile.getName(), new File("d:\\catalogRoot\\test.zip"));
System.out.println(ftpFile.getName() + ":" + ftpFile.getType()+":"+ftpFile.getSize()+":"+ftpFile.getModifiedDate());
}
}else{
System.out.println("該目錄下的文件為空!");
}
}
}
6、如何用Java實現FTP伺服器
1.使用的FileZillaServer開源免費軟體,安裝過後建立的本地FTP伺服器。2.使用的apache上下載專FTP工具包,引用到工程目錄屬中。3.IDE,Eclipse,JDK6上傳和下載目錄的實現原理:對每一個層級的目錄進行判斷,是為目錄類型、還是文件類型。如果為目錄類型,採用遞歸調用方法,檢查到最底層的目錄為止結束。如果為文件類型,則調用上傳或者下載方法對文件進行上傳或者下載操作。貼出代碼:(其中有些沒有代碼,可以看看,還是很有用處的)!
7、如何用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){}
}
8、求java實現的FTP客戶端伺服器源碼!
網上多的是,別懶
9、求用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);
}
}}
10、java如何實現將FTP文件轉移到另一個FTP伺服器上
你有FTPClient就比較好辦,假如你的兩台FTP伺服器分別為fs1和fs2
在本地開發代碼思路如下:
通過專FTPClient連接上fs1,然後下載屬(可以循環批量下載)到本地伺服器,保存到一個臨時目錄。
下載完成後,FTPClient斷開與fs1的連接,記得必須logout。
本地伺服器通過FileInputStream將剛下載到臨時目錄的文件讀進來,得到一個List<File>集合。
通過FTPClient連接上fs2,循環List<File>集合,將文件上傳至fs2的特定目錄,然後清空臨時目錄,上傳完畢後,斷開fs2的連接,同樣必須logout。