上一篇我们讲完了go-fastdfs上传文件,这一篇我们来讲一讲另一种上传文件方式,基于ftp形式的上传文件。
这一篇我们没有存入数据库 ,有需要的可以参考go-fastdfs存入数据库的方法,这篇主要说的还是上传文件到ftp
前提准备
yml配置文件
ftp:
ip: xxxx
username: zhangwen
password: 123456
port: 21
1.上传文件工具类
package com.zyw.showsyears.util;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
@Configuration
public class FileUtil {
@Value("${ftp.ip}")
private String ip;
@Value("${ftp.username}")
private String userName;
@Value("${ftp.password}")
private String password;
@Value("${ftp.port}")
private int port;
public HashMap<String,Object> uploadFileToFtp(MultipartFile uploadFile, String dir) {
HashMap<String,Object> resultmMap = new HashMap<>();
try {
String oldName = uploadFile.getOriginalFilename(); // 获取旧的名字
String newName = new Date().getTime() + ""; // 生成一个文件名
newName = newName + oldName.substring(oldName.lastIndexOf(".")); //新名字
String imagePath = dir; //上传的路径
boolean result = uploadFile(ip, port, userName, password, "/", imagePath,
newName, uploadFile.getInputStream()); //调用方法,上传文件
//判断是否上传成功
if (!result) {
resultmMap.put("error", 1);
resultmMap.put("message", "上传失败");
return resultmMap;
}
resultmMap.put("error", 0);
resultmMap.put("fileName", oldName);
resultmMap.put("url", imagePath + "/" + newName);
//其实在这里可以存入文件数据库,我们这没写
return resultmMap;
} catch (IOException e) {
resultmMap.put("error", 1);
resultmMap.put("message", "上传发生异常");
return resultmMap;
}
}
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切换到上传目录
if (!ftp.changeWorkingDirectory(basePath + filePath)) {
//如果目录不存在创建目录
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
//设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
}
2.调用工具类
package com.zyw.showsyears.controller;
import com.zyw.showsyears.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
@Controller
@RequestMapping("/file")
public class FileTest {
@Autowired
FileUtil fileDemo;
@RequestMapping("/fileOne")
public void show(MultipartFile file) {
System.out.println(file);
HashMap<String,Object> map=fileDemo.uploadFileToFtp(file,"/");
//在这里我们其实也可以在写一个存入 业务和表关联的表 方便后续通过文件路径来查看图片,这里我们省略没写
System.out.println(map);
}
@RequestMapping("/fileTwo")
public void show(MultipartFile[] file) {
System.out.println(file);
for (int i = 0; i < file.length; i++) {
HashMap<String,Object> map=fileDemo.uploadFileToFtp(file[i],"");
System.out.println(map);
}
}
}
大家在测试时候可以用FTPServer.exe 这个软件进行本地ftp环境
至此ftp上传文件就可以了