求用java写一个ftp服务器客户端程序。

求用java写一个ftp服务器客户端程序。,第1张

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=0i<dirStructure.lengthi++){

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)

}

}}

给你一个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:

charm_szFtpIp[20] //Ftp服务器IP地址

UINTm_uFtpPort //Ftp服务器端口

charm_szFtpUser[20] //Ftp用户名

charm_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]

GetModuleFileName(NULL, filepath, MAX_PATH)

strDir.Format("%s", filepath)

strPath = strDir.Left(strDir.ReverseFind('\\')) + strDate + "\\*.txt"

return strPath

}

//保存成两个文件,然后添加到你的工程中就可以调用了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12560266.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存