當前位置:首頁 » 文件傳輸 » ftpclient是啥
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

ftpclient是啥

發布時間: 2022-09-15 03:23:23

A. FTPClient 是什麼

FTP客戶端

B. filezilla ftp client和filezilla server有什麼區別分別有什麼用途

前者是客戶端,用來通過ftp或者sftp協議上傳下載文件的 ------ 有它,你能連別人的伺服器上傳和下載
後者是服務端,用來搭建ftp或者sftp服務的(以便為別人通過ftp客戶端訪問自己的文件提供服務) ----有它,別人能連你的伺服器上傳下載。
========================
什麼情況啊,一直提示完善回答,這個回答夠完善啦。

C. Java類庫中FtpClient的ftpClient用法是什麼

切換工作路徑啊,
ftpClient.changeWorkingDirectory("/admin/pic"); //工作路徑切換到/admin/pic ,ftpClient.setBufferSize(1024); //設置1M緩沖,
ftpClient.setControlEncoding("GBK"); //設置編碼為GBK,
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //文件類型為二進制文件,
ftpClient.storeFile("3.gif", fis)//在前面設置好路徑,緩沖,編碼,文件類型後,開始上傳3.gif。

D. ftpsclient 和ftpclient有什麼區別

FTP(FileTransferProtocol)是Internet上用來傳送文件的協議(文件傳輸協議)。它是為了我們能夠在Internet上互相傳送文件而制定的的文件傳送標准,規定了Internet上文件如何傳送。也就是說,通過FTP協議,我們就可以跟Internet上的FTP伺服器進行文件的上傳(Upload)或下載(Download)等動作。WWW的核心——HTTP協議眾所周知,Internet的基本協議是TCP/IP協議,目前廣泛採用的FTP、ArchieGopher等是建立在TCP/IP協議之上的應用層協議,不同的協議對應著不同的應用。WWW伺服器使用的主要協議是HTTP協議,即超文體傳輸協議。由於HTTP協議支持的服務不限於WWW,還可以是其它服務,因而HTTP協議允許用戶在統一的界面下,採用不同的協議訪問不同的服務,如FTP、Archie、SMTP、NNTP等。另外,HTTP協議還可用於名字伺服器和分布式對象管理。

E. FtpClient這個類的方法怎麼用

jdk1.7下其構造函數FtpClient()被定義為private類型,所以無法new了。 在jdk1.7,已經換成了 FtpClient.create(ip)方法.
同時,其他的一些方法也基本都改掉了,

如 ftpClient.openServer(server);
ftpClient.login(user, password);

就可以換成:ftpClient.login(user, null, password);

ftpClient.binary(); ---> ftpClient.setBinaryType();

ftpClient.put(remotefilename);--->ftpClient.putFileStream(remotefilename, true);

等。

F. FtpClient

當時我用SUN 的FtpClient.get()方法下載文件是有問題的,我推薦你用org.apache.commons.net.ftp.FTPClient下載文件,可以解決中文文件下載問題,你可以去我博客里看看哦:http://hi..com/renliangli/blog/item/6ccb6b3a049d95c9d46225a5.html,文章摘給你吧:
現在就來看下我解決的代碼吧,希望對遇到同樣問題的人有點幫助。

1)把ftp地址中的文件保存到本地的java類源碼:

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class Ftp {

/**
* Description: 從FTP伺服器下載文件
* @param ip FTP伺服器的ip地址
* @param port FTP伺服器埠,默認為:21
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param remotePath FTP伺服器上的相對路徑
* @param fileName 要下載的文件名
* @param localPath 下載後保存到本地的路徑
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip, port);
//下面三行代碼必須要,而且不能改變編碼格式,否則不能正確下載中文文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

//如果採用默認埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}

ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();

for(int i = 0; i < fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){

File localFile = new File(localPath+File.separator+ff.getName());
//
OutputStream is = new FileOutputStream(localFile);

//注意此處retrieveFile的第一個參數由GBK轉為ISO-8859-1編碼。否則下載後的文件內容為空。
//原因可能是由於aix系統默認的編碼為ISO-8859-1
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), is);
is.close();
}
}

ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Ftp.downFile("10.32.166.144", 21, "test", "test", "/flashfxp", "激活碼.txt", "C:");

}

}

2)將ftp資源以文件流的方式打開,由用戶決定保存在本地何處,程序運行後可以從IE跳出框中打開或者保存的Action代碼,利用Struts1寫的:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.mocha.test;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DownloadAction extends Action{

/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException{

OutputStream os=null;

try {

os = response.getOutputStream();

response.reset();

downFile("10.32.166.144", 21, "test", "test", "/flashfxp", "激活碼.txt",os,response);

} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
os.close();

} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return null;

}

/**
* Description: 從FTP伺服器下載文件
* @param ip FTP伺服器ip地址
* @param port FTP伺服器埠,默認為21
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param remotePath 附件在FTP伺服器上的絕對路徑
* @param fileName 要下載的文件名
* @param outputStream 輸出流
* @param response
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath
,String fileName,OutputStream outputStream,HttpServletResponse response) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip, port);
//下面三行代碼必須要,而且不能改變編碼格式
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

//如果採用默認埠,可以使用ftp.connect(url)的方式直接連接FTP伺服器
ftp.login(username, password);//登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}

ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();

for(int i = 0; i < fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){

String filename = fileName;
//這個就就是彈出下載對話框的關鍵代碼
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(filename, "utf-8"));
//將文件保存到輸出流outputStream中
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), outputStream);
outputStream.flush();
outputStream.close();
}
}

ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}

差點忘了利用ftpclient要用到的jar包了,呵呵,就這兩個了:commons-net-1.4.1.jar、jakarta-oro-2.0.8.jar

對了我用的jdk是1.4的。所以commons-net-1.4.1.jar用了這個版本比較老的。

G. FTPClient需要關閉嗎

這是FTP客戶端,一般在操作系統中默認開啟,不需要關閉

H. ftp client配置是什麼意思

FTP客戶端配置,設置要連接的FTP伺服器的IP,埠,用戶名,密碼,連接模式等

I. FtpClient.storeFile作用

作用是將流寫進伺服器

FTP就是文件傳輸協議。用於互聯網雙向傳輸,控制文件下載空間在伺服器復制文件從本地計算機或本地上傳文件復制到伺服器上的空間。

J. filezilla ftp client和filezilla server有什麼區別分別有什麼用途

前者是客戶端,用來通過ftp或者sftp協議上傳寫在文件的
------
有它,你能連別人的伺服器上傳和下載
後者是服務端,用來搭建ftp或者sftp服務的(以便為別人通過ftp客戶端訪問自己的文件提供服務)
----有他,別人能連你的伺服器上傳下載