当前位置:首页 » 文件管理 » 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#.");
}
热点内容
javawebeclipse编译 发布:2025-05-14 11:35:24 浏览:935
可编程控制器试题 发布:2025-05-14 11:25:32 浏览:119
dsp混合编程 发布:2025-05-14 11:23:10 浏览:248
mysql添加存储过程 发布:2025-05-14 11:23:01 浏览:879
房车旅游自媒体有脚本吗 发布:2025-05-14 11:18:18 浏览:125
android输入法键盘 发布:2025-05-14 11:15:48 浏览:658
谷歌商店安卓手机在哪里 发布:2025-05-14 11:13:46 浏览:535
编程猫销售女 发布:2025-05-14 11:13:36 浏览:335
安卓卡无翼怎么出小黑屋 发布:2025-05-14 11:13:00 浏览:581
买商用笔记本电脑主要看哪些配置 发布:2025-05-14 11:12:15 浏览:950