當前位置:首頁 » 文件管理 » javaftp拷貝文件內容

javaftp拷貝文件內容

發布時間: 2023-04-17 03:49:45

java如何實現將ftp文件轉移到另一個FTP伺服器上

你有FTPClient就比較好辦,假如你的兩台FTP伺服器分別為fs1和fs2

在本地開發代碼思路如下:

  1. 通過FTPClient連接上fs1,然後下載(可以循環批量下載)到本地伺服器,保存到一個臨時目錄。

  2. 下載完成後,FTPClient斷開與fs1的連接,記得必須logout。

  3. 本地伺服器通過FileInputStream將剛下載到臨時目錄的文件讀進來,得到一個List<File>集合。

  4. 通過FTPClient連接上fs2,循環List<File>集合,將文件上傳至fs2的特定目錄,然後清空臨時目錄,上傳完畢後,斷開fs2的連接,同樣必須logout。

② java如何拷貝文件到另一個目錄下

下面列舉出4種方式:

1、使用FileStreams復制

這是最經典的方式將一個文件的內容復制到另一慎搜個文件中。 使用FileInputStream讀取文件A的位元組,使山頃用FileOutputStream寫入到文件B。正如你所看到的我們執行幾個讀和寫操作try的數據,所以這應該是一個低效率的,下一個方法我們將看到新的方式。 這是第一個方法的代碼:

③ java中怎麼實現ftp文件傳輸

packagecom.quantongfu.ftp.ftp;

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.util.List;

importorg.apache.commons.net.ftp.FTPReply;
importorg.apache.log4j.Logger;
importorg.apache.log4j.net.SocketServer;

importcom.quantongfu.conf.FtpConf;

/**
*@項目名稱:telinSyslog
*@文件名稱:Ftp.java
*@創建日期:2015年9月14日下午3:22:08
*@功能描述:ftp實體類,用於連接,上傳
*@修訂記錄:
*/
publicclassFtp{
privatestaticLoggerlogger=Logger.getLogger(Ftp.class);
privateFTPClientftp;

/**
*
*@parampath
*上傳到ftp伺服器哪個路徑下
*@paramaddr
*地址
*@paramport
*埠號
*@paramusername
*用戶名
*@parampassword
*密碼
*@return
*@throwsException
*/
publicbooleanconnect()throwsException{
booleanresult=false;
ftp=newFTPClient();
intreply;
ftp.connect(FtpConf.FTP_HOST,FtpConf.FTP_PORT);
ftp.login(FtpConf.FTP_USER_NAME,FtpConf.FTP_PASSWORD);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.setDataTimeout(60000);
reply=ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
returnresult;
}
if(FtpConf.IS_FTP_DIRECTORY){
ftp.changeWorkingDirectory(FtpConf.FTP_DIRECTORY);
}
result=true;
returnresult;
}

/**
*
*@paramfiles
*上傳的文件
*@throwsException
*/
publicbooleanupload(Filefile)throwsIOException{
FileInputStreaminput=null;
try{
input=newFileInputStream(file);
booleanb=ftp.storeFile(file.getName()+".tmp",input);
if(b){
b=ftp.rename(file.getName()+".tmp",file.getName());
}
returnb;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}finally{
if(input!=null){
input.close();
}
}
}

/**
*
*@paramfiles
*上傳的文件
*@throwsException
*/
publicbooleanupload(ServerSocketserver,Filefile)throwsException{
FileInputStreaminput=null;
try{
if(!file.exists()){
returntrue;
}
input=newFileInputStream(file);
booleanb=ftp.storeFile(server,file.getName()+".tmp",input);
if(b){
b=ftp.rename(file.getName()+".tmp",file.getName());
if(b){
file.delete();
}
}
returnb;
}catch(Exceptione){
logger.error("ftperror"+e.getMessage());
returnfalse;
}finally{
if(input!=null){
try{
input.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
/*斷開連接*/
publicvoiddisConnect(){
try{
if(ftp!=null){
ftp.disconnect();
}
}catch(IOExceptione){
e.printStackTrace();
}

}
/*獲取連接*/
publicstaticFtpgetFtp(){
Ftpftp=newFtp();
try{
ftp.connect();
}catch(Exceptione){
logger.error("FTP連接異常"+e.getMessage());
e.printStackTrace();
}
returnftp;
}
/*重連*/
publicFtpreconnect(){
disConnect();
returngetFtp();
}
}

使用Apache FtpClient jar包,獲取jar : http://commons.apache.org/net/

④ java如何拷貝文件到另一個目錄下

/**
*
復制單個文件
*
@param
oldPath
String
原文件路徑
如:c:/fqf.txt
*
@param
newPath
String
復制後路徑
如:f:/fqf.txt
*
@return
boolean
*/
public
void
File(String
oldPath,
String
newPath)
{
try
{
int
bytesum
=
0;
int
byteread
=
0;
File
oldfile
=
new
File(oldPath);
if
(oldfile.exists())
{
//文件存在時
InputStream
inStream
=
new
FileInputStream(oldPath);
//讀入原文件
FileOutputStream
fs
=
new
FileOutputStream(newPath);
byte[]
buffer
=
new
byte[1444];
int
length;
while
(
(byteread
=
inStream.read(buffer))
!=
-1)
{
bytesum
+=
byteread;
//位元組數
文件大小
System.out.println(bytesum);
fs.write(buffer,
0,
byteread);
}
inStream.close();
}
}
catch
(Exception
e)
{
System.out.println("復制單個文件操作出錯");
e.printStackTrace();
}
}
/**
*
復制整個文件夾內容
*
@param
oldPath
String
原文件路徑
如:c:/fqf
*
@param
newPath
String
復制後路徑
如:f:/fqf/ff
*
@return
boolean
*/
public
void
Folder(String
oldPath,
String
newPath)
{
try
{
(new
File(newPath)).mkdirs();
//如果文件夾不存在
則建立新文件夾
File
a=new
File(oldPath);
String[]
file=a.list();
File
temp=null;
for
(int
i
=
0;
i
<
file.length;
i++)
{
if(oldPath.endsWith(File.separator)){
temp=new
File(oldPath+file[i]);
}
else{
temp=new
File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream
input
=
new
FileInputStream(temp);
FileOutputStream
output
=
new
FileOutputStream(newPath
+
"/"
+
(temp.getName()).toString());
byte[]
b
=
new
byte[1024
*
5];
int
len;
while
(
(len
=
input.read(b))
!=
-1)
{
output.write(b,
0,
len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夾
Folder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch
(Exception
e)
{
System.out.println("復制整個文件夾內容操作出錯");
e.printStackTrace();
}
}

⑤ 用java怎麼獲取ftp上的文件

public class FtpClientUtil {
FtpClient ftpClient;
private String server;
private int port;
private String userName;
private String userPassword;

public FtpClientUtil(String server,int port,String userName,String userPassword)
{
this.server=server;
this.port=port;
this.userName=userName;
this.userPassword=userPassword;
}
/**
* 鏈接到伺服器
* @return
*/
public boolean open()
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
return true;
try
{
ftpClient= new FtpClient();
ftpClient.openServer(server,port);
ftpClient.login(userName, userPassword);
ftpClient.binary();
return true;
}
catch(Exception e)
{
e.printStackTrace();
ftpClient=null;
return false;
}
}

public boolean cd(String dir){
boolean f = false;
try {
ftpClient.cd(dir);
} catch (IOException e) {
Logs.error(e.toString());
return f;
}
return true;
}

/**
* 上傳文件到FTP伺服器
* @param localPathAndFileName 本地文件目錄和文件名
* @param ftpFileName 上傳後的文件名
* @param ftpDirectory FTP目錄如:/path1/pathb2/,如果目錄不存在回自動創建目錄
* @throws Exception
*/
public boolean upload(String localDirectoryAndFileName,String ftpFileName,String ftpDirectory)throws Exception {
if(!open())
return false;
FileInputStream is=null;
TelnetOutputStream os=null;
try
{
char ch = ' ';
if (ftpDirectory.length() > 0)
ch = ftpDirectory.charAt(ftpDirectory.length() - 1);
for (; ch == '/' || ch == '\\'; ch = ftpDirectory.charAt(ftpDirectory.length() - 1))
ftpDirectory = ftpDirectory.substring(0, ftpDirectory.length() - 1);

int slashIndex = ftpDirectory.indexOf(47);
int backslashIndex = ftpDirectory.indexOf(92);
int index = slashIndex;
String dirall = ftpDirectory;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
String directory = "";
while (index != -1) {
if (index > 0) {
String dir = dirall.substring(0, index);
directory = directory + "/" + dir;
ftpClient.sendServer("XMKD " + directory + "\r\n");
ftpClient.readServerResponse();
}
dirall = dirall.substring(index + 1);
slashIndex = dirall.indexOf(47);
backslashIndex = dirall.indexOf(92);
index = slashIndex;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
}
ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n");
ftpClient.readServerResponse();

os = ftpClient.put(ftpDirectory + "/"
+ ftpFileName);
File file_in = new File(localDirectoryAndFileName);
is = new FileInputStream(file_in);
byte bytes[] = new byte[1024];
int i;
while ((i = is.read(bytes)) != -1)
os.write(bytes, 0, i);
//清理垃圾

return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
}
}
/**
* 從FTP伺服器上下載文件並返回下載文件長度
* @param ftpDirectoryAndFileName
* @param localDirectoryAndFileName
* @return
* @throws Exception
*/
public long download(String ftpDirectoryAndFileName,String localDirectoryAndFileName)throws Exception
{
long result = 0;
if(!open())
return result;
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
is = ftpClient.get(ftpDirectoryAndFileName);
java.io.File outfile = new java.io.File(localDirectoryAndFileName);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
result = result + c;
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();

}
return result;
}
/**
* 返回FTP目錄下的文件列表
* @param ftpDirectory
* @return
*/
public List<String> getFileNameList(String ftpDirectory)
{
List<String> list = new ArrayList<String>();
if(!open())
return list;
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(ftpDirectory));
String filename = "";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 刪除FTP上的文件
* @param ftpDirAndFileName
*/
public boolean deleteFile(String ftpDirAndFileName)
{
if(!open())
return false;
ftpClient.sendServer("DELE "+ftpDirAndFileName+"\r\n");
return true;
}
/**
* 刪除FTP目錄
* @param ftpDirectory
*/
public boolean deleteDirectory(String ftpDirectory)
{
if(!open())
return false;
ftpClient.sendServer("XRMD "+ftpDirectory+"\r\n");
return true;
}
/**
* 關閉鏈接
*/
public void close()
{
try
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
ftpClient.closeServer();
}catch(Exception e)
{

}
}
}望採納,謝謝。

⑥ Java怎麼實現文件拷貝

工具/原料

一台配置了java環境的電腦

一款適合自己的開發集成環境,這里用的是eclipse Kepler


文件拷貝DEMO

1.首先,理清思路,然後我們再動手操作。

拷貝,有源文件,和目的文件。

如果原文件不存在,提示,報錯。

如果目的文件不存在,創建空文件並被覆蓋。

如果目的地址,也即目的路徑不存在,創建路徑。

拷貝,輸入流,輸出流,關閉流。

拷貝前輸出文件大小,計算拷貝大小,比較並核實。輸出。

⑦ 我想登錄一個ftp然後把某個目錄的所有文件考到另一個ftp的目錄的某個文件夾下用java代碼實現

用的commons-net包中的FTPClient
ftp1為拷貝目錄,ftp2為被拷貝目錄
你先登錄ftp2調用ftp1,
ftpClient1.changeWorkingDirectory(path);
InputStream inputStream = ftpClient1.retrieveFileStream(file.getName());
用這個代碼應該可以從ftp1中獲得一個inputStream ,在ftp2中可以做上傳操作
目錄的話ftp2還要做遞歸存放到list中,ftp2遍歷上傳. 其實我也沒做這個,希望思路有點幫助,應該可以實現.good luck!~~~

⑧ ftp 上的文件 如何復制下來

FTP 是 TCP/IP 協議組中的協議之一,是英文File Transfer Protocol的縮寫。該協議是Internet文件傳送的基礎,它由一系列規格說明文檔組成,目標是提高文件的共享性,提供非直接使用遠程計算機,使存儲介質對用戶透明和可靠高效地傳送數據。簡單的說,FTP就是完成兩台計算機之間的拷貝,從遠程計算機拷貝文件至自己的計算機上,稱之為「下載(download)」文件。若將文件從自己計算機中拷貝至遠程計算機上,則稱之為「上載(upload)」文件。在TCP/IP協議中,FTP標准命令TCP埠號為21,Port方式數據埠為20。FTP協議的任務是從一台計算機將文件傳送到另一台計算機,它與這兩台計算機所處的位置、聯接的方式、甚至是是否使用相同的操作系統無關。假設兩台計算機通過ftp協議對話,並且能訪問Internet, 你可以用ftp命令來傳輸文件。每種操作系統使用上有某一些細微差別,但是每種協議基本的命令結構是相同的。 FTP的傳輸有兩種方式:ASCII傳輸模式和二進制數據傳輸模式。 1.ASCII傳輸方式:假定用戶正在拷貝的文件包含的簡單ASCII碼文本,如果在遠程機器上運行的不是UNIX,當文件傳輸時ftp通常會自動地調整文件的內容以便於把文件解釋成另外那台計算機存儲文本文件的格式。 但是常常有這樣的情況,用戶正在傳輸的文件包含的不是文本文件,它們可能是程序,資料庫,字處理文件或者壓縮文件(盡管字處理文件包含的大部分是文本,其中也包含有指示頁尺寸,字型檔等信息的非列印字元)。在拷貝任何非文本文件之前,用binary 命令告訴ftp逐字拷貝,不要對這些文件進行處理,這也是下面要講的二進制傳輸。 2.二進制傳輸模式:在二進制傳輸中,保存文件的位序,以便原始和拷貝的是逐位一一對應的。即使目的地機器上包含位序列的文件是沒意義的。例如,macintosh以二進制方式傳送可執行文件到Windows系統,在對方系統上,此文件不能執行。 如果你在ASCII方式下傳輸二進制文件,即使不需要也仍會轉譯。這會使傳輸稍微變慢 ,也會損壞數據,使文件變得不能用。(在大多數計算機上,ASCII方式一般假設每一字元的第一有效位無意義,因為ASCII字元組合不使用它。如果你傳輸二進制文件,所有的位都是重要的。)如果你知道這兩台機器是同樣的,則二進制方式對文本文件和數據文件都是有效的。 5. FTP的工作方式 FTP支持兩種模式,一種方式叫做Standard (也就是 PORT方式,主動方式),一種是 Passive (也就是PASV,被動方式)。 Standard模式 FTP的客戶端發送 PORT 命令到FTP伺服器。Passive模式FTP的客戶端發送 PASV命令到 FTP Server。 下面介紹一個這兩種方式的工作原理: Port模式FTP 客戶端首先和FTP伺服器的TCP 21埠建立連接,通過這個通道發送命令,客戶端需要接收數據的時候在這個通道上發送PORT命令。 PORT命令包含了客戶端用什麼埠接收數據。在傳送數據的時候,伺服器端通過自己的TCP 20埠連接至客戶端的指定埠發送數據。 FTP server必須和客戶端建立一個新的連接用來傳送數據。 Passive模式在建立控制通道的時候和Standard模式類似,但建立連接後發送的不是Port命令,而是Pasv命令。FTP伺服器收到Pasv命令後,隨機打開一個高端埠(埠號大於1024)並且通知客戶端在這個埠上傳送數據的請求,客戶端連接FTP伺服器此埠,然後FTP伺服器將通過這個埠進行數據的傳送,這個時候FTP server不再需要建立一個新的和客戶端之間的連接。 很多防火牆在設置的時候都是不允許接受外部發起的連接的,所以許多位於防火牆後或內網的FTP伺服器不支持PASV模式,因為客戶端無法穿過防火牆打開FTP伺服器的高端埠;而許多內網的客戶端不能用PORT模式登陸FTP伺服器,因為從伺服器的TCP 20無法和內部網路的客戶端建立一個新的連接,造成無法工作。

⑨ 用java有一個ftp路徑地址,需要把傳送一些數據到ftp中怎樣實現

1.使用的FileZillaServer開源,安裝過後建立的本地FTP伺服器。2.使用的apache上FTP工具包,引用到工程目錄中。3.IDE,Eclipse,JDK6上傳和目錄的實現原理:對每一個層級的目錄進行判斷,是為目錄類型、還是文件類型。如果為目錄類型,採用遞歸調用方法,檢查到最底層的目錄為止結束。如果為文件類型,則調用上傳或者方法對文件進行上傳或者操作。貼出代碼:(其中有些沒有代碼,可以看看,還是很有用處的)!

⑩ java怎麼在ftp上取到文件夾中文件再錄入txt文本

前段時間正好看了這個。

http://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example

這個文檔非常好。有什麼看不懂的再問吧。

主要是使用org.apache.commons.net.ftp.FTPClient 和org.apache.commons.net.ftp.FTP 類。

核心代碼:

ftpClient.connect(server,port);
ftpClient.login(user,pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

//APPROACH#1:usingretrieveFile(String,OutputStream)
StringremoteFile1="/test/video.mp4";
FiledownloadFile1=newFile("D:/Downloads/video.mp4");
OutputStreamoutputStream1=newBufferedOutputStream(newFileOutputStream(downloadFile1));
booleansuccess=ftpClient.retrieveFile(remoteFile1,outputStream1);
outputStream1.close();

if(success){
System.out.println("File#.");
}
熱點內容
電腦怎麼選擇配置 發布:2025-05-14 10:46:12 瀏覽:325
電腦怎麼不顯示手機連接伺服器失敗 發布:2025-05-14 10:42:28 瀏覽:9
安卓如何下載lv手游 發布:2025-05-14 10:35:45 瀏覽:383
pythondict添加key 發布:2025-05-14 10:33:59 瀏覽:382
柱子箍筋加密區長度 發布:2025-05-14 10:18:29 瀏覽:352
雲伺服器和內網穿透哪個好 發布:2025-05-14 10:16:41 瀏覽:627
安徽新能源網路配置是什麼 發布:2025-05-14 10:06:24 瀏覽:631
pinode搭建伺服器 發布:2025-05-14 10:04:23 瀏覽:4
電腦伺服器ip名稱 發布:2025-05-14 10:01:09 瀏覽:749
connectorpython 發布:2025-05-14 09:48:50 瀏覽:763