public static String uploadFtpFile(String ftpIp, String ftpPort, String ftpUser, String ftpPassword,
String ftpFilePath, String saveFileName, MultipartFile uploadFile){
//创建客户端对象
FTPClient ftp = new FTPClient();
InputStream inputStream = null;
String path = "";
try {
//连接ftp服务器
ftp.connect(ftpIp, Integer.valueOf(ftpPort));
//登录
ftp.login(ftpUser, ftpPassword);
//设置上传路径
path = "/" + ftpFilePath;
//检查上传路径是否存在 如果不存在返回false
boolean flag = ftp.changeWorkingDirectory(path);
if (!flag) {
//创建上传的路径 该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
ftp.makeDirectory(path);
/* //保证可以创建多层目录
StringTokenizer s = new StringTokenizer(path, File.separator);
s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftp.mkd(pathName);
} catch (Exception e) {
System.out.println("ftp文件夹创建失败");
}
}*/
}
//指定上传路径
ftp.changeWorkingDirectory(path);
//指定上传文件的类型 二进制文件
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//读取本地上传文件
inputStream = uploadFile.getInputStream();
//第一个参数是文件名
ftp.storeFile(saveFileName, inputStream);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭文件流
inputStream.close();
//退出
ftp.logout();
//断开连接
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
04-09
1191
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-08
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)