FTP工具类
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import javax.sound.midi.Soundbank;
import java.io.*;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
public void downFile(String ftpPath,String localPath,String fileName) throws Exception {
try {
ftpClient = this.getFTPClient(ftpIp,ftpUserName,ftpPassword,ftpPort);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
} catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024]; int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public boolean uploadFile(String ftpPath,String fileName, InputStream input) {
boolean success = false;
try {
System.out.println("_____________________________________"+fileName);
int reply;
ftpClient = getFTPClient(ftpIp, ftpUserName, ftpPassword, ftpPort);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
if(ftpPath!=null && !"".equals(ftpPath)) {
CreateDirecroty(ftpPath);
}
ftpClient.changeWorkingDirectory(ftpPath);
ftpClient.storeFile(fileName, input);
input.close();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
public FTPClient getFTPClient(String ftpIp, String ftpUserName,String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpIp,ftpPort);
ftpClient.login(ftpUserName, ftpPassword);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
public static byte[] getInputStream(String ftpFileName,String ftpIp,String ftpUserName,String ftpPassword,String ftpProt) {
String basepath = "";
try {
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(5);
ftpClient.connect(ftpIp, Integer.valueOf(ftpProt));
ftpClient.login(ftpUserName, ftpPassword);
ftpClient.changeWorkingDirectory(basepath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if ("".equals(basepath)) {
basepath = "/";
}
String dir = new String(basepath.getBytes("GBK"), StandardCharsets.ISO_8859_1);
String fileName = new String(ftpFileName.getBytes("GBK"), StandardCharsets.ISO_8859_1);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
byte[] bytes = IOUtils.toByteArray(inputStream);
inputStream.close();
ftpClient.getReply();
ftpClient.logout();
return bytes;
} catch (Exception e) {
System.out.println( e.toString());
return null;
}
}
public static byte[] getInputStreamgfkh(String ftpFileName,String ftpIp,String ftpUserName,String ftpPassword,String ftpProt) {
String basepath = "";
try {
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(5);
ftpClient.connect(ftpIp, Integer.valueOf(ftpProt));
ftpClient.login(ftpUserName, ftpPassword);
ftpClient.changeWorkingDirectory(basepath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if ("".equals(basepath)) {
basepath = "/";
}
String dir = new String(basepath.getBytes("GBK"), StandardCharsets.ISO_8859_1);
String fileName = new String(ftpFileName.getBytes("GBK"), StandardCharsets.ISO_8859_1);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
byte[] bytes = IOUtils.toByteArray(inputStream);
inputStream.close();
ftpClient.getReply();
ftpClient.logout();
return bytes;
} catch (Exception e) {
System.out.println( e.toString());
return null;
}
}
public boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
System.out.println("进入文件夹" + directory + " 成功!");
} else {
System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
public boolean 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) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
if (end <= start) {
break;
}
}
}
return success;
}
public boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
public boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftpClient.makeDirectory(dir);
if (flag) {
System.out.println("创建文件夹" + dir + " 成功!");
} else {
System.out.println("创建文件夹" + dir + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public String getFtpIp() {return ftpIp;}
public void setFtpIp(String ftpIp) {this.ftpIp = ftpIp;}
public String getFtpUserName() {return ftpUserName;}
public void setFtpUserName(String ftpUserName) {this.ftpUserName = ftpUserName;}
public String getFtpPassword() {return ftpPassword;}
public void setFtpPassword(String ftpPassword) {this.ftpPassword = ftpPassword;}
public Integer getFtpPort() {return ftpPort;}
public void setFtpPort(Integer ftpPort) {this.ftpPort = ftpPort;}
public Integer getHttpPort() {return httpPort;}
public void setHttpPort(Integer httpPort) {this.httpPort = httpPort;}
public FTPClient getFtpClient() {return ftpClient;}
public void setFtpClient(FTPClient ftpClient) {this.ftpClient = ftpClient;}
public FtpUtil(String ftpIp, String ftpUserName, String ftpPassword, Integer ftpPort, Integer httpPort) {
this.ftpIp = ftpIp;
this.ftpUserName = ftpUserName;
this.ftpPassword = ftpPassword;
this.ftpPort = ftpPort;
this.httpPort = httpPort;
}
public FtpUtil() {
}
public FTPClient geta(){
return getFTPClient(ftpIp, ftpUserName, ftpPassword, ftpPort);
}
}