SpringBoot上传文件到FTP服务器
昨天公司遇到一个上传图片到ftp服务器的问题,其实也不麻烦,于是自己就想练下手
首先创建一个基本的SpringBoot工程
pom.xml文件如下
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.028888.springcloud</groupId> <artifactId>mainpom</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>net.028888.springcloudupload</groupId> <artifactId>springCloudUpload</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springCloudUpload</name> <url>https://www.bugkong.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
主要是引入了commons-net包
然后创建工程的目录结构如下:
然后,我直接上代码吧,时间来不及了,一会上班要迟到了。。。。
1,主程序Upload.java
@Controller @ComponentScan("org.springCloudUpload") @EnableAutoConfiguration public class Upload { @Autowired private UploadService uploadService; @RequestMapping("/upload") @ResponseBody String upload(String path) { uploadService.upLoadImg(path); return "index"; } public static void main(String[] args) { SpringApplication.run(Upload.class, args); } }
2,service实现层(借口层就不写了)
@Service public class UpLoadServiceImpl implements UploadService{ // ftp服务器ip地址 @Value("${ftpAddress}") private String ftpAddress; // 端口号 @Value("${ftpPort}") private int ftpPort; // 用户名 @Value("${ftpName}") private String ftpName; // 密码 @Value("${ftpPassWord}") private String ftpPassWord; // 图片路径 @Value("${ftpBasePath}") private String ftpBasePath; @Override public void upLoadImg(String path) { // TODO Auto-generated method stub InputStream inputStream = null; try { inputStream = new FileInputStream(path); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Boolean flag = FtpUtil.uploadFile(path, inputStream,ftpAddress,ftpPort,ftpName,ftpPassWord,ftpBasePath); if (flag == true) { System.out.println("ftp上传成功!"); } } }
3,工具类FtpUtil
public class FtpUtil { // ftp服务器ip地址 private static String FTP_ADDRESS; // 端口号 private static int FTP_PORT; // 用户名 private static String FTP_USERNAME; // 密码 private static String FTP_PASSWORD; // 图片路径 private static String FTP_BASEPATH; private static boolean uploadFile(String originFileName, InputStream input) { System.out.println(FTP_ADDRESS); boolean success = false; FTPClient ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); try { int reply; ftp.connect(FTP_ADDRESS, FTP_PORT);// 连接FTP服务器 ftp.login(FTP_USERNAME, FTP_PASSWORD);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.makeDirectory(FTP_BASEPATH); ftp.changeWorkingDirectory(FTP_BASEPATH); ftp.storeFile(originFileName, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; } public static Boolean uploadFile(String path, InputStream inputStream, String ftpAddress, int ftpPort, String ftpName, String ftpPassWord, String ftpBasePath) { FTP_ADDRESS = ftpAddress; FTP_PORT = ftpPort; FTP_USERNAME = ftpName; FTP_PASSWORD = ftpPassWord; FTP_BASEPATH = ftpBasePath; uploadFile(path,inputStream); return true; } }
4,application.properties文件,主要就是定义ftp参数
#配置ftp信息 ftpAddress= ftpPort= ftpName= ftpBasePath= ftpPassWord=
注意,地址,请直接写ip或域名,不要协议(http或ftp)
好了最后测试一下
http://localhost:8080/upload?path=/mydoc/3.txt
爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情