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。