package com.iflytek.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtils
{
public String hostname = "166.0.1.78";
public Integer port = Integer.valueOf(21);
public String username = "hz";
public String password = "hzftp";
public FTPClient ftpClient = null;
public void initFtpClient() { this.ftpClient = new FTPClient();
this.ftpClient.setControlEncoding("utf-8");
try {
this.ftpClient.connect(this.hostname, this.port.intValue());
this.ftpClient.login(this.username, this.password);
int replyCode = this.ftpClient.getReplyCode();
System.out.println(replyCode);
if (!FTPReply.isPositiveCompletion(replyCode))
System.out.println("连接ftp成功");
}
catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
public boolean uploadFile(String pathname, String fileName, String originfilename) throws FileNotFoundException {
boolean flag = false;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(originfilename));
initFtpClient();
this.ftpClient.setFileType(2);
CreateDirecroty(pathname);
this.ftpClient.makeDirectory(pathname);
this.ftpClient.changeWorkingDirectory(pathname);
this.ftpClient.storeFile(fileName, inputStream);
inputStream.close();
this.ftpClient.logout();
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
private void CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote + "/";
if ((directory.equalsIgnoreCase("/")) && (!changeWorkingDirectory(new String(directory)))) {
int start = 0;
int end = 0;
if (directory.startsWith("/"))
start = 1;
else {
start = 0;
}
end = directory.indexOf("/", start);
String path = "";
String paths = "";
while (true)
try {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
try {
if (!existFile(path)) {
if (makeDirectory(subDirectory))
changeWorkingDirectory(subDirectory);
else
changeWorkingDirectory(subDirectory);
}
else
changeWorkingDirectory(subDirectory);
}
catch (IOException e)
{
e.printStackTrace();
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
private boolean makeDirectory(String dir) throws IOException {
boolean flag = true;
flag = this.ftpClient.makeDirectory(dir);
if (flag)
System.out.println("创建文件成功");
else {
System.out.println("创建文件失败");
}
return flag;
}
private boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = this.ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
private boolean changeWorkingDirectory(String directory) throws IOException {
boolean flag = true;
flag = this.ftpClient.changeWorkingDirectory(directory);
if (flag)
System.out.println("进入文件夹成功");
else {
System.out.println("进入文件夹失败,开始创建文件夹");
}
return flag;
}
}