package com.ftp_copyFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
public class ftpMain
{
public static void main(String[] args)
{
int ftpPort = 0;
String ftpUserName = "";
String ftpPassword = "";
String ftpHost = "";
String ftpPath = "";
try
{
InputStream in = FTPUtil.class.getClassLoader().getResourceAsStream("ftp.properties");
if (in == null)
{
System.out.println("配置文件ftp.properties读取失败");
}
else
{
Properties properties = new Properties();
properties.load(in);
ftpUserName = properties.getProperty("ftpUserName");
ftpPassword = properties.getProperty("ftpPassword");
ftpHost = properties.getProperty("ftpHost");
ftpPort = Integer.valueOf(properties.getProperty("ftpPort")).intValue();
ftpPath = properties.getProperty("ftpPath");
ftpCopy(ftpUserName, ftpPassword, ftpPath, ftpHost, ftpPort,
"a1.jpg");
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e.toString());
}
}
public static boolean ftpCopy(String ftpUserName, String ftpPassword,
String ftpPath, String ftpHost, int ftpPort, String fileName)
throws IOException
{
InputStream in = null;
FTPClient ftpClient = null;
boolean copyFalg = false;
try
{
ftpClient = FTPUtil.getFTPClient(ftpHost, ftpPassword, ftpUserName, ftpPort);
// ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setDefaultTimeout(2*60*1000);
ftpClient.setSoTimeout(5*60*1000);
ftpClient.setBufferSize(1024);
ftpClient.changeWorkingDirectory(ftpPath);
//获取输入流
in = ftpClient.retrieveFileStream(fileName);
fileName = "new_" + fileName;
// 调用完之后必须调用completePendingCommand释放,否则FTP会断开连接
if (!ftpClient.completePendingCommand())
{
copyFalg = false;
return copyFalg;
}
// 如果读取的文件流不为空则复制文件
if (in != null)
{
copyFalg = ftpClient.storeFile(fileName, in);
// 关闭文件流
in.close();
if (!copyFalg)
{
return copyFalg;
}
}
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
}
return copyFalg;
}
}
package com.ftp_copyFile;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPUtil
{
/**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpPassword,
String ftpUserName, int ftpPort)
{
FTPClient ftpClient = null;
try
{
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
ftpClient.disconnect();
}
}
catch (IOException e)
{
e.printStackTrace();
}
return ftpClient;
}
}
ftp.properties
ftpUserName=root
ftpPassword=134
ftpHost=10.41.200.23
ftpPort=21
ftpPath=/home/123
writeTempFielPath=e\://
研究半天没找到解决方法,先记录下来。
有种思路是:分别获取新旧文件的输入输出流,然后进行读写操作,阻碍是:FTP服务器上大文件的输入流没法获取。
-----------------------------------------
改进实施:
ftpClient2 = FTPUtil.getFTPClient(ftpHost, ftpPassword, ftpUserName, ftpPort);
ftpClient2.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient2.enterLocalPassiveMode();
ftpClient2.setDefaultTimeout(2*60*1000);
ftpClient2.setSoTimeout(5*60*1000);
ftpClient2.setBufferSize(1024);
ftpClient2.changeWorkingDirectory(ftpPath);
out = ftpClient2.storeFileStream(fileName);
try
{
byte[] buffer = new byte[1024];
int temp = 0;
while (true)
{
temp = in.read(buffer, 0, buffer.length);
if (temp == -1)
{
break;
}
out.write(buffer, 0, temp);
}
}
catch(IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch(IOException e2)
{
System.out.println(e2.toString());
}
}
也可适用于不同的FTP服务器之间,大文件可以成功,项目中未实际使用。