做本案例前,您需要去搭建ftp服务站点,不懂的同志可以去参考 百度教程
1、依赖导入
<dependencies> <!--FTP包--> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.5</version> </dependency> <!--日志包--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.9</version> </dependency> </dependencies>
2、FTPTools工具类编写
import org.apache.commons.net.ftp.FTPClient; import org.apache.log4j.Logger; import java.io.IOException; import java.io.InputStream; /** * 通过FTP上传文件 * * @Author boat * @Date 2021/2/11 */ public class FTPTools { //用于打印日志 private static final Logger log = Logger.getLogger(FTPTools.class); //设置私有不能实例化 private FTPTools() { } /** * 上传 * * @param hostname ip或域名地址 * @param port 端口 * @param username 用户名 * @param password 密码 * @param workingPath 服务器的工作目 * @param inputStream 要上传文件的输入流 * @param saveName 设置上传之后的文件名 * @return */ public static boolean upload(String hostname, int port, String username, String password, String workingPath, InputStream inputStream, String saveName) { boolean flag = false; FTPClient ftpClient = new FTPClient(); //1 测试连接 if (connect(ftpClient, hostname, port, username, password)) { try { //2 检查工作目录是否存在 if (ftpClient.changeWorkingDirectory(workingPath)) { // 3 检查是否上传成功 if (storeFile(ftpClient, saveName, inputStream)) { flag = true; disconnect(ftpClient); } } } catch (IOException e) { log.error("工作目录不存在"); e.printStackTrace(); disconnect(ftpClient); } } return flag; } /** * 断开连接 * * @param ftpClient * @throws Exception */ public static void disconnect(FTPClient ftpClient) { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); log.error("已关闭连接"); } catch (IOException e) { log.error("没有关闭连接"); e.printStackTrace(); } } } /** * 测试是否能连接 * * @param ftpClient * @param hostname ip或域名地址 * @param port 端口 * @param username 用户名 * @param password 密码 * @return 返回真则能连接 */ public static boolean connect(FTPClient ftpClient, String hostname, int port, String username, String password) { boolean flag = false; try { //ftp初始化的一些参数 ftpClient.connect(hostname, port); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setControlEncoding("UTF-8"); if (ftpClient.login(username, password)) { log.info("连接ftp成功"); flag = true; } else { log.error("连接ftp失败,可能用户名或密码错误"); try { disconnect(ftpClient); } catch (Exception e) { e.printStackTrace(); } } } catch (IOException e) { log.error("连接失败,可能ip或端口错误"); e.printStackTrace(); } return flag; } /** * 上传文件 * * @param ftpClient * @param saveName 全路径。如/home/public/a.txt * @param fileInputStream 要上传的文件流 * @return */ public static boolean storeFile(FTPClient ftpClient, String saveName, InputStream fileInputStream) { boolean flag = false; try { if (ftpClient.storeFile(saveName, fileInputStream)) { flag = true; log.error("上传成功"); disconnect(ftpClient); } } catch (IOException e) { log.error("上传失败"); disconnect(ftpClient); e.printStackTrace(); } return flag; } }
3、编写测试类
import com.boat.utils.FTPTools; import org.apache.commons.net.ftp.FTPClient; import java.io.*; /** * @Author boat */ public class MainTest { /** * 测试连接 * ftpClient.connect(hostname, port); * * 检查上传的工作目录是否存在 * ftpClient.changeWorkingDirectory(workingPath) * * 上传 * ftpClient.storeFile(saveName, fileInputStream) * * 断开连接 * ftpClient.disconnect() * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String hostname = "192.168.1.115"; int port = 21; String username = "ftpUser"; String password = "Admin123"; //存放的目录,比如d:/boat/jdsxqrspdf String workingPath = "jdsxqrspdf"; String str = "C:\Users\Administrator\Desktop\"; InputStream fileInputStream = new FileInputStream(new File(str)); String saveName = "Video.wmv"; System.out.println(FTPTools.upload( hostname, port, username, password, workingPath, fileInputStream, saveName)); } }
如何上传提示结果为ture表示成功