你好,这个你解决了吗?C语言如何用FtpPutFile()函数上传文件到Ftp服务器!下载用FtpGetFile()可以!
先后使用InternetOpen和InternetConnect打开连接。使用CreateFile函数打开本地文件。使用FtpOpenFile函数打开远程文件。分别使用InternetReadFile和ReadFile函数读取
FTP
或本地文件。分别使用InternetWriteFile和WriteFile函数写入
FTP
或本地文件。使用CloseHandle函数关闭本地文件句柄。使用InternetCloseHandle函数关闭
FTP
文件句柄。
下载类:
import javaioFile;
import javaioFileOutputStream;
import javaioIOException;
import sunnetTelnetInputStream;
import sunnetftpFtpClient;
/
@======================================================================================================================================
@类说明:文件下载
@创建者:
@创建日期 2009-6-18
@======================================================================================================================================
/
public class FtpDownload {
/
@----------------------------------------------------------------------------------------------------------------------------------
@作者:
@版本号:
@函数说明:从ftp服务器上下载一个文件
@参数:path 文件路径和名称
@返回:
@创建日期:2009-06-19
@----------------------------------------------------------------------------------------------------------------------------------
/
public String downloadFile(String host, String name, String password, String hostPath, String outPath) {
String res = "-1";
long current = SystemcurrentTimeMillis();
try {
FtpClient ftpClient = new FtpClient(); // 创建FtpClient对象
ftpClientopenServer(host); // 连接FTP服务器
ftpClientlogin(name, password); // 登录FTP服务器
if (hostPathlength() != 0)
ftpClientcd(hostPath);
ftpClientbinary();
TelnetInputStream is = ftpClientget("01rmvb");
File file_out = new File("d:\\22rmvb");
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
Systemoutprintln(">>>>>>>>>>downloadFile>>>>>>>>>>>>>>>");
while ((c = isread(bytes)) != -1) {
oswrite(bytes, 0, c);
}
Long time = (SystemcurrentTimeMillis() - current);
res = timetoString();
isclose();
osclose();
ftpClientcloseServer();
} catch (IOException ex) {
exprintStackTrace();
res = "-1";
return res;
}
return res;
}
/
@----------------------------------------------------------------------------------------------------------------------------------
@作者:
@版本号:
@函数说明:测试用main函数
@参数:path 文件路径和名称
@返回:
@创建日期:2009-06-19
@----------------------------------------------------------------------------------------------------------------------------------
/
public static void main(String argv[]) {
FtpDownload f = new FtpDownload();
FtpUpload up = new FtpUpload();
FtpFileList fileList = new FtpFileList();
String path = new String("/");
String outPath = new String();
fileListdownloadFileList("10020161155","name001", "name001",path);
fileListdownloadFileList("10020161155","name001", "name001",path+"folder");
fileListdownloadFileList("10020161155","name001", "name001",path+"folder/2");
fdownloadFile("10020161155","name001", "name001",path, outPath);
upuploadFile("10020161155","name001", "name001",path,"D:/111txt","555txt");
}
}
上传类:
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import sunnetTelnetOutputStream;
import sunnetftpFtpClient;
/
@======================================================================================================================================
@类说明:文件上传
@创建者:
@创建日期 2009-6-18
@======================================================================================================================================
/
public class FtpUpload {
/
@----------------------------------------------------------------------------------------------------------------------------------
@作者:
@版本号:
@函数说明:给ftp服务器上传一个文件
@参数:host ftp服务器IP,name 用户名,password 密码,path 文件路径和名称,inFileName 上传文件路径和名称,outFileName 上传文件在服务器端名称
@返回:
@创建日期:2009-06-19
@----------------------------------------------------------------------------------------------------------------------------------
/
public String uploadFile(String host, String name, String password, String path, String inFileName, String outFileName) {
String res = "-1";
long current = SystemcurrentTimeMillis();
try {
FtpClient ftpClient = new FtpClient(); // 创建FtpClient对象
ftpClientopenServer(host); // 连接FTP服务器
ftpClientlogin(name, password); // 登录FTP服务器
if (pathlength() != 0)
ftpClientcd(path);
ftpClientbinary();
TelnetOutputStream os = ftpClientput(outFileName);
File file_in = new File(inFileName);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
Systemoutprintln(">>>>>>>>>>uploadFile>>>>>>>>>>>>>>>");
while ((c = isread(bytes)) != -1) {
oswrite(bytes, 0, c);
}
Long time = (SystemcurrentTimeMillis() - current);
res = timetoString();
isclose();
osclose();
ftpClientcloseServer();
} catch (IOException ex) {
exprintStackTrace();
res="-1";
return res;
}
return res;
}
}
看服务器目录类:
import javaioIOException;
import javautilList;
import sunnetTelnetInputStream;
import sunnetftpFtpClient;
public class FtpFileList {
/
@----------------------------------------------------------------------------------------------------------------------------------
@作者:
@版本号:
@函数说明:ftp服务器的默认目录及文件名下载
@参数:file 文件路径和名称
@返回:
@创建日期:2009-06-18
@----------------------------------------------------------------------------------------------------------------------------------
/
public String downloadFileList(String host, String name, String password, String path) {
String res = "-1";
long current = SystemcurrentTimeMillis();
try {
FtpClient ftpClient = new FtpClient(); // 创建FtpClient对象
ftpClientopenServer(host); // 连接FTP服务器
ftpClientlogin(name, password); // 登录FTP服务器
if (pathlength() != 0)
ftpClientcd(path);
TelnetInputStream is = ftpClientnameList(path);
//TelnetInputStream is = ftpClientlist();
int c;
Systemoutprintln(">>>>>>>>>>fileList>>>>>>>>>>>>>>>");
while ((c = isread()) != -1) {
Systemoutprint((char) c);
}
Long time = (SystemcurrentTimeMillis() - current);
res = timetoString();
isclose();
ftpClientcloseServer();// 退出FTP服务器
} catch (IOException ex) {
exprintStackTrace();
res = "-1";
return res;
}
return res;
}
}
--追寻 为您解忧 希望采纳
网站模板库 » 你好,这个你解决了吗?C语言如何用FtpPutFile()函数上传文件到Ftp服务器!下载用FtpGetFile()可以!
0条评论