
问一下,你是想做ftp上传下载么?
首先你需要安装一个ftp服务端程序,启动起来,然后下载一个ftp客户端程序,测试能不能连接,首先这一块儿需要测试通过。
代码ftp上传下载
21 上传代码:
import javaioFile;
import javaioFileInputStream;
import orgapachecommonsnetftpFTPClient;
import orgapachecommonsnetftpFTPReply;
public class test {
private FTPClient ftp;
/
@param path 上传到ftp服务器哪个路径下
@param addr 地址
@param port 端口号
@param username 用户名
@param password 密码
@return
@throws Exception
/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftpconnect(addr,port);
ftplogin(username,password);
ftpsetFileType(FTPClientBINARY_FILE_TYPE);
reply = ftpgetReplyCode();
if (!FTPReplyisPositiveCompletion(reply)) {
ftpdisconnect();
return result;
}
ftpchangeWorkingDirectory(path);
result = true;
return result;
}
/
@param file 上传的文件或文件夹
@throws Exception
/
private void upload(File file) throws Exception{
if(fileisDirectory()){
ftpmakeDirectory(filegetName());
ftpchangeWorkingDirectory(filegetName());
String[] files = filelist();
for (int i = 0; i < fileslength; i++) {
File file1 = new File(filegetPath()+"\\"+files[i] );
if(file1isDirectory()){
upload(file1);
ftpchangeToParentDirectory();
}else{
File file2 = new File(filegetPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
}else{
File file2 = new File(filegetPath());
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
public static void main(String[] args) throws Exception{
test t = new test();
tconnect("", "localhost", 21, "yhh", "yhhazr");
File file = new File("e:\\uploadify");
tupload(file);
}
}
22 下载代码
这里没有用到filter,如果用filter就可以过滤想要的文件。
public class Ftp {
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
Ftp ftp = new Ftp();
String hostname = ">
public strData as string
'这段可以返回服务器中的第一级目录名,保存在strData中, 如有多个文件夹名每个文件夹名会以空格分开。读取时判段一下。
Inet1Execute Inet1URL, "dir """ & dirName & """"
Do
DoEvents
Loop While Inet1StillExecuting = True
msgbox strData
'这段是进入目录中,要想获得服务器中所有的文件夹名,就可以用递规的办法来获得。
Inet1Execute Inet1URL, "cd """ & dirName & """"
Do
DoEvents
Loop While Inet1StillExecuting = True
Private Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case 1
StatusBar1PanelsItem(1)Text = "该控件正在查询所指定的主机的 IP 地址"
Case 2
StatusBar1PanelsItem(1)Text = "该控件已成功地找到所指定的主机的 IP 地址"
Case 3
StatusBar1PanelsItem(1)Text = "该控件正在与主机连接"
Case 4
StatusBar1PanelsItem(1)Text = "该控件已与主机连接成功"
Case 5
StatusBar1PanelsItem(1)Text = "该控件正在向主机发送请求"
Case 6
StatusBar1PanelsItem(1)Text = "该控件发送请求已成功"
Case 7
StatusBar1PanelsItem(1)Text = "该控件正在接收主机的响应"
Case 8
StatusBar1PanelsItem(1)Text = "该控件已成功地接收到主机的响应"
Case 9
StatusBar1PanelsItem(1)Text = "该控件正在解除与主机的连接"
Case 10
StatusBar1PanelsItem(1)Text = "该控件已成功地与主机解除了连接"
Case 11
MsgBox "与主机通讯时出现了错误", vbInformation, "个人资料"
Case 12
Dim vtData As Variant
Dim bDone As Boolean
bDone = False
'取得第一个块。
vtData = Inet1GetChunk(1024, icString)
DoEvents
Do While Not bDone
strData = strData & vtData
'取得下一个块。
vtData = Inet1GetChunk(1024, icString)
DoEvents
If Len(vtData) = 0 Then
bDone = True
End If
Loop
StatusBar1PanelsItem(1)Text = "该请求已经完成,并且所有数据均已接收到"
Case Else
StatusBar1PanelsItem(1)Text = Inet1ResponseCode & ": " & Inet1ResponseInfo
End Select
End Sub
以上就是关于怎么获取ftp的路径全部的内容,包括:怎么获取ftp的路径、Linux下如何获取FTP文件名和大小、VB怎么获得FTP服务器的文件以及文件夹列表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)