引入依赖:
<!--ftp文件上传-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
package com.wuye.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
/**
* @author
* @date
*/
public class FileTool {
final static String url="127.0.0.1"; // FTP服务器hostname
final static int port=21; // FTP服务器端口
final static String username="ftptest"; // FTP登录账号
final static String password="123456"; // FTP登录密码
final static String path="images"; // FTP服务器保存目录
/**
* 上传图片
* @param file
* @return
* @throws Exception
*/
public static String uploadPic(MultipartFile file) throws Exception{
if(file.getSize()==0){
return null;
}
String filename = file.getOriginalFilename();
System.out.println("图片名是:"+filename);
if(filename.length() == 0){
return null;
}
//得到后缀名
int lastIndex = filename.lastIndexOf(".");
String suffix = filename.substring(lastIndex);
//将文件名字替换-->文件地址
filename = CommonUtil.getUUID()+suffix;
String picurl="http://47.92.103.143:8088/images/"+filename;
InputStream input = file.getInputStream();
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8");
try {
int reply;
ftp.connect(url, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.makeDirectory(path);
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return picurl;
}
/**
* 上传并且删除图片
* @param file
* @param oldFileName
* @return
* @throws Exception
*/
public static String uploadAndDeletePic(MultipartFile file,String oldFileName) throws Exception{
if(file.getSize()==0){
return null;
}
String filename = file.getOriginalFilename();
//得到后缀名
int lastIndex = filename.lastIndexOf(".");
String suffix = filename.substring(lastIndex);
//将文件名字替换-->文件地址
filename = CommonUtil.getUUID()+suffix;
String picurl="http://47.92.103.143:8088/images/"+filename;
InputStream input = file.getInputStream();
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8");
try {
int reply;
ftp.connect(url, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.makeDirectory(path);
ftp.changeWorkingDirectory(path);
if (StringUtils.isNotEmpty(oldFileName)){
ftp.deleteFile(oldFileName);
}
ftp.storeFile(filename, input);
input.close();
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return picurl;
}
}