package hcit.dajk.fyda.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;
/**
- ftp上传
- @author sjd
-
2018-6-1 下午3:54:42
*/
public class UpFileToFtp
{
public static final long serialVersionUID = 201806011553L;
private String ip = “”;
private String username = "";
private String password = "";
private int port = 2121;
private String localFileFullName = "";// 需要上传的目录
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public UpFileToFtp(String serverIP, int port, String username, String password) throws IOException
{
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
this.ftpClient = new FtpClient(this.ip, this.port);
}
public UpFileToFtp(String serverIP, String username, String password)
{
this.ip = serverIP;
this.username = username;
this.password = password;
}
/**
* 创建文件夹
*
* @param dir
* @param ftpClient
* @throws Exception
*/
private void createDir(String dir) throws Exception
{
this.ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
s.countTokens();
String pathName = "";
while (s.hasMoreElements())
{
pathName = pathName + "/" + (String) s.nextElement();
try
{
this.ftpClient.sendServer("XMKD " + pathName + "\r\n");
}
catch (Exception e)
{
e = null;
}
this.ftpClient.readServerResponse();
}
this.ftpClient.binary();
}
/**
* 检查文件夹是否存在
*
* @param dir
* @param ftpClient
* @return
*/
private Boolean isDirExist(String dir)
{
try
{
this.ftpClient.cd(dir);
}
catch (Exception e)
{
return false;
}
return true;
}
/**
* ftp上传
*
* @param localFileFullName
* 上传的源文件夹
* @param P_FileNewName
* 文件新名称
* @return
*/
public Boolean upload(String localFileFullName, String P_FileNewName, String P_FTPFilePath)
{
this.localFileFullName = localFileFullName;
try
{
String savefilename = new String(localFileFullName.getBytes("ISO-8859-1"), "GBK");
// 新建一个FTP客户端连接
ftpClient = new FtpClient();
ftpClient.openServer(this.ip, this.port);
ftpClient.login(this.username, this.password);
createDir(P_FTPFilePath.substring(0, P_FTPFilePath.lastIndexOf("/")));
// 打开本地待上传的文件
File file_in = new File(savefilename);
processFile(file_in, P_FileNewName, P_FTPFilePath);
if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if (ftpClient != null)
{
ftpClient.closeServer();
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
return false;
}
}
/**
* 上传文件
*
* @param source
* @param ftpClient
* @throws Exception
*/
private void processFile(File source, String P_FileNewName, String P_FTPFilePath) throws Exception
{
if (source.exists())
{
if (source.isDirectory())// 检查表示此抽象路径名的文件是否是一个目录
{
// 判断目录是否存在,不存在则创建目录
if (!isDirExist(source.getPath().substring(localFileFullName.length()).replace("//", "/")))
{
createDir(source.getPath().substring(localFileFullName.length()).replace("//", "/"));
}
File sourceFile[] = source.listFiles();
for (int i = 0; i < sourceFile.length; i++)
{
if (sourceFile[i].exists())
{
if (sourceFile[i].isDirectory())
{
this.processFile(sourceFile[i], P_FileNewName, P_FTPFilePath);
}
else
{
this.ftpClient.cd(cheangPath(sourceFile[i].getPath()));
this.ftpClient.binary();
os = ftpClient.put(sourceFile[i].getName());
byte[] bytes = new byte[1024];
is = new FileInputStream(sourceFile[i]);
// 开始复制
int c;
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
}
}
else
{
// ftpClient.cd(cheangPath(source.getPath()));//更改远程计算机上的目录
// System.out.println("cheangPath(source.getPath()):"+cheangPath("C:/FTP2/wj/"));
this.ftpClient.cd(P_FTPFilePath);// 更改远程计算机上的目录
this.ftpClient.binary();
os = ftpClient.put(P_FileNewName);
byte[] bytes = new byte[1024];
is = new FileInputStream(source);
// 开始复制
int c;
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
else
{
throw new Exception("此文件或文件夹[" + source.getName() + "]有误或不存在!");
}
}
/**
* 获取当前的FTP路径
*
* @param path
* @return
*/
private String cheangPath(String path)
{
path = path.substring(this.localFileFullName.length()).replace("//", "/");
if ("".equals(path))
{
path = "/";
}
else
{
path = path.substring(0, path.lastIndexOf("/") + 1);
}
return path;
}
public static void main(String args[]) throws Exception
{
// UpFileToFtp ftpup = new UpFileToFtp("172.16.3.21", 2121, "dell","Sjd1141613095!");
ftpup.upload("E:/bar/test");
}
}