windows下FTP服务器可以使用3CDaemon,下载后点击FTP服务器设置进行相关设置:
使用jar包坐标如下:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
使用java连接服务器并进行操作,登录FTP后默认在FTP根目录下,如果操作文件需要切换到待操作目录。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.btzh.exception.BusinessException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.util.StringUtils;
/**
* FTP工具类
*
* @author faith
*/
public class FtpUtil {
public final static String HOSTNAME = "127.0.0.1";
public final static Integer PORT = 21;
public final static String USERNAME = "admin";
public final static String PASSWORD = "666666";
public static FTPClient FTP_CLIENT;
public static void main(String[] args) {
FtpUtil.uploadFileByPath("/upload/img/", "test.jpg", "D:\\timg.jpg");
FtpUtil.downloadFile("/upload/img/", "test.jpg", "D:\\ftp");
FtpUtil.deleteFile("upload/img", "test.jpg");
}
/**
* 上传文件by文件名
* @param pathName
* @param fileName
* @param originFileName
*/
public static void uploadFileByPath(String pathName, String fileName, String originFileName) {
InputStream inputStream = null;
FTP_CLIENT = getFtpClient();
try {
FTP_CLIENT.setFileType(FTP.BINARY_FILE_TYPE);
createIfNotExsit(pathName);
FTP_CLIENT.storeFile(fileName, inputStream = new FileInputStream(new File(originFileName)));
} catch (Exception e) {
throw new RuntimeException("上传文件失败: " + e);
} finally {
closeIOResource(FTP_CLIENT, inputStream, null);
}
}
/**
* 上传文件by流
* @param pathname
* @param fileName
* @param inputStream
*/
public static void uploadFileByStream(String pathname, String fileName, InputStream inputStream) {
FTP_CLIENT = getFtpClient();
try {
FTP_CLIENT.setFileType(FTP.BINARY_FILE_TYPE);
createIfNotExsit(pathname);
FTP_CLIENT.storeFile(fileName, inputStream);
} catch (Exception e) {
throw new RuntimeException("上传文件失败");
} finally {
closeIOResource(FTP_CLIENT, inputStream, null);
}
}
/**
* 下载文件
* @param pathname
* @param filename
* @param localPath
* @return
*/
public static void downloadFile(String pathname, String filename, String localPath) {
OutputStream outputStream = null;
FTP_CLIENT = getFtpClient();
try {
FTP_CLIENT.changeWorkingDirectory(pathname);
FTPFile[] ftpFiles = FTP_CLIENT.listFiles();
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.getName().equalsIgnoreCase(filename)) {
FTP_CLIENT.retrieveFile(ftpFile.getName(), outputStream = new FileOutputStream(new File(localPath + "/" + ftpFile.getName())));
return;
}
}
throw new BusinessException("未找到文件:" + pathname + "/" + filename);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeIOResource(FTP_CLIENT, null, outputStream);
}
}
/**
* 删除文件
* @param pathName
* @param fileName
*/
public static void deleteFile(String pathName, String fileName) {
FTP_CLIENT = getFtpClient();
try {
FTP_CLIENT.changeWorkingDirectory(pathName);
FTP_CLIENT.dele(fileName);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeIOResource(FTP_CLIENT, null, null);
}
}
/**
* 改变工作目录
* @param directory
* @return
*/
public static boolean changeWorkingDirectory(String directory) {
try {
return FTP_CLIENT.changeWorkingDirectory(directory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 创建目录
* @param directory
*/
public static void createIfNotExsit(String directory) {
if (directory.equalsIgnoreCase("/")) {
return;
}
while (directory.startsWith("/") || directory.endsWith("/")) {
directory = directory.substring(directory.startsWith("/") ? 1 : 0, directory.endsWith("/") ? directory.length() - 1 : directory.length());
if (directory.equalsIgnoreCase("/")) {
return;
}
}
String[] filePaths = StringUtils.delimitedListToStringArray(directory, "/");
for (String filePath : filePaths) {
if (!existFilePath(filePath)) {
makeDirectory(filePath);
}
changeWorkingDirectory(filePath);
}
}
/**
* 判断文件是否存在
* @param filePath
* @return
*/
public static boolean existFilePath(String filePath) {
try {
return FTP_CLIENT.listFiles(filePath).length > 0;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 创建文件目录
* @param directory
* @return
*/
public static boolean makeDirectory(String directory) {
try {
return FTP_CLIENT.makeDirectory(directory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 连接FTP服务器
*/
public static FTPClient getFtpClient() {
FTP_CLIENT = new FTPClient();
FTP_CLIENT.setControlEncoding("UTF-8");
try {
FTP_CLIENT.connect(HOSTNAME, PORT);
if (!FTP_CLIENT.login(USERNAME, PASSWORD)) {
throw new RuntimeException("ftp登录失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return FTP_CLIENT;
}
/**
* 关闭使用的资源
* @param ftpClient
* @param inputStream
*/
private static void closeIOResource(FTPClient ftpClient, InputStream inputStream, OutputStream outputStream) {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
}
}
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
}