<!--FTP包-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.5</version>
</dependency>
package com.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FtpUtil {
private Logger log = LoggerFactory.getLogger(this.getClass());
private String username;
private String password;
private String host;
private int port;
private static FTPClient ftp;
// 初始化
public FtpUtil(String host, int port, String username, String password) {
this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
// 登录
public boolean login() throws SocketException, IOException {
ftp = new FTPClient();
ftp.connect(host, port);
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
log.error("ftp登录失败");
ftp.disconnect();
return false;
}
else {
return true;
}
}
// 登出
public boolean logout() throws IOException {
return ftp.logout();
}
/**
* 上传
* @param path 服务器路径,如果文件夹不存在,则会一级级的创建
* @param fileName 文件名
* @param inputStream 输入流
* @throws IOException
*/
public void uploadFile(String path, String fileName, InputStream inputStream) throws IOException {
// 如果目录不存在就创建
if (!ftp.changeWorkingDirectory(path)) {
mkdir(path);
}
ftp.storeFile(path + "/" + fileName, inputStream);
}
/**
* 上传
* @param path 服务器路径,如果文件夹不存在,则会一级级的创建
* @param fileName 文件名
* @param str 文件内容
* @throws IOException
*/
public void uploadFile(String path, String fileName, String str) throws IOException {
uploadFile(path, fileName, new ByteArrayInputStream(str.getBytes()));
}
/**
* 下载
* @param 文件路径
* @throws IOException
*/
public byte[] downloadFile(String path) throws IOException {
return IOUtils.toByteArray(ftp.retrieveFileStream(path));
}
/**
* 创建多级文件夹
* @param path 路径,必须是绝对路径
* 根据/分割,一级级的创建
* @throws IOException
*/
public void mkdir(String path) throws IOException {
if (!path.startsWith("/")) {
throw new RuntimeException("创建文件夹必须是以/开头的绝对路径");
}
String tempPath = "";
String pathArr[] = path.split("/");
for (String s : pathArr) {
// 开头/和结尾/会分割出空字符串
if (!"".equals(s)) {
tempPath += "/" + s;
if (!ftp.changeWorkingDirectory(tempPath)) {
log.info("创建文件夹:" + tempPath);
ftp.mkd(tempPath);
}
}
}
}
public static void main(String[] args) throws SocketException, IOException {
// 初始化
// FtpUtil ftpUtil = new FtpUtil("127.0.0.1", 21, "ftp_user", "xxxxx");
// 登录
// System.out.println(ftpUtil.login());
// 上传文件
// ftpUtil.uploadFile("/home/ftp_user/ems_api/flow/a/b/c/d", "test.txt", new FileInputStream(new File("c:\\yingwenok.txt")));
// 上传文件
// ftpUtil.uploadFile("/home/ftp_user/ems_api/flow/a/b/c/d", "test.txt", "阿AV爱吃撒\r\nsadsd");
// 下载文件
// System.out.println(new String(ftpUtil.downloadFile("/home/ftp_user/ems_api/flow/a/b/c/d/test.txt")));
//登出
// System.out.println(ftpUtil.logout());
}
}