commons-net
commons-net
3.3
2.Ftp工具类
import com.turing.JavaBean.FtpInformation;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
*/
@Component //这就是我们刚才加入的依赖
public class FtpUtil {
/**
*/
private static final String FTP_ADDRESS = “39.103.203.27”;
/**
*/
private static final int FTP_PORT = 21;
/**
*/
private static String FTP_USERNAME;
/**
*/
private static String FTP_PASSWORD;
/**
*/
private static String FTP_BASEPATH = “/home/”;
/**
-
参数传过来了文件和文件的输入流
-
@param file
-
@return
-
@throws Exception
*/
public static String uploadFile(FtpInformation ftpInformation,MultipartFile file)throws Exception {
FTP_USERNAME = ftpInformation.getFTP_USERNAME();
FTP_PASSWORD = ftpInformation.getFTP_PASSWORD();
if ("/home/".equals(FTP_BASEPATH))
{
FTP_BASEPATH = FTP_BASEPATH+FTP_USERNAME+"/";
}
//获取上传的文件流
InputStream inputStream = file.getInputStream();
//获取上传的文件名
String filename = file.getOriginalFilename();
//获取文件后缀
String suffix = filename.substring(filename.lastIndexOf("."));
//使用UUID拼接后缀,定义一个不重复的文件名
String finalName = UUID.randomUUID() + suffix;
//这是最开始引入的依赖里的方法
FTPClient ftp = new FTPClient();
ftp.setControlEncoding(“utf-8”);
try {
int reply;
// 连接FTP服务器
ftp.connect(FTP_ADDRESS, FTP_PORT);
// 登录
ftp.login(FTP_USERNAME, FTP_PASSWORD);
//连接成功会的到一个返回状态码
reply = ftp.getReplyCode();
//可以输出看一下是否连接成功
System.out.println(reply);
//设置文件类型
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//修改操作空间
ftp.changeWorkingDirectory(FTP_BASEPATH);
//对了这里说明一下你所操作的文件夹必须要有可读权限,chomd 777 文件夹名//这里我就是用的我的home文件夹
//这里开始上传文件
ftp.storeFile(finalName, inputStream);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println(“连接失败”);
return null;
}
System.out.println(“连接成功!”);
inputStream.close();
ftp.logout();
return FTP_BASEPATH+finalName;
} catch (Exception e) {
e.printStackTrace();
System.out.println(“ERROR:”+e.getMessage());
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return null;
}
}
import lombok.AllArgsConstructor;
import lombok.Data;
/**
*/
@Data
@AllArgsConstructor
public class FtpInformation
{
private String FTP_USERNAME;
private String FTP_PASSWORD;
}
//记住添加请求头,否则无法接收文件
@PostMapping(value = “/uploadFile”, headers = “content-type=multipart/form-data”)
@ResponseBody
@ApiOperation("上传
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】浏览器打开:qq.cn.hn/FTf 免费领取
文件")
public ResponseBean uploadFile(FtpInformation ftpInformation, @RequestParam(“files”) MultipartFile[] files)
{
List list &#61; new ArrayList<>();
for (MultipartFile file : files) {
try {
//调用自定义的FTP工具类上传文件
String finalName &#61; FtpUtil.uploadFile(ftpInformation,file);
if (StringUtils.isEmpty(finalName))
{
throw new Exception(“Upload Error”);
}
list.add(finalName);
} catch (Exception e) {
log.error(“ERROR:{}”,e.getMessage());
return new ResponseBean(HttpStatusCode.INTERNAL_SERVER_ERROR,null);
}
}
return new ResponseBean(HttpStatusCode.Success,list);
}