application.yml
ftp:
host: 自己服务器ip
userName: 服务器账号
password: 服务器密码
port: 22
rootPath: /usr/nginx/image
img:
url: http://ip:9090/ # ftp.img.url 可以不添加,这里只是为了上传文件成功后返回文件路径
file:
upload:
path: /usr/local/nginx/image/ # Linux下文件的上传路径
FtpUtil.java
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Properties;
@Component
public class FtpUtil {
private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);
/**
* ftp服务器ip地址
*/
private static String host;
@Value("${ftp.host}")
public void setHost(String val){
FtpUtil.host = val;
}
/**
* 端口
*/
private static int port;
@Value("${ftp.port}")
public void setPort(int val){
FtpUtil.port = val;
}
/**
* 用户名
*/
private static String userName;
@Value("${ftp.userName}")
public void setUserName(String val){
FtpUtil.userName = val;
}
/**
* 密码
*/
private static String password;
@Value("${ftp.password}")
public void setPassword(String val){
FtpUtil.password = val;
}
/**
* 存放图片的根目录
*/
private static String rootPath;
@Value("${ftp.rootPath}")
public void setRootPath(String val){
FtpUtil.rootPath = val;
}
/**
* 存放图片的路径
*/
private static String imgUrl;
@Value("${ftp.img.url}")
public void setImgUrl(String val){
FtpUtil.imgUrl = val;
}
/**
* 获取连接
*/
private static ChannelSftp getChannel() throws Exception{
JSch jsch = new JSch();
//->ssh root@host:port
Session sshSession = jsch.getSession(userName,host,port);
//密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
return (ChannelSftp) channel;
}
/**
* ftp上传图片
* @param inputStream 图片io流
* @param imagePath 路径,不存在就创建目录
* @param imagesName 图片名称
* @return urlStr 图片的存放路径
*/
public static String putImages(InputStream inputStream, String imagePath, String imagesName){
try {
ChannelSftp sftp = getChannel();
String path = rootPath + imagePath + "/";
createDir(path,sftp);
//上传文件
sftp.put(inputStream, path + imagesName);
logger.info("上传成功!");
sftp.quit();
sftp.exit();
//处理返回的路径
String resultFile;
resultFile = imgUrl + imagePath + imagesName;
return resultFile;
} catch (Exception e) {
logger.error("上传失败:" + e.getMessage());
}
return "";
}
/**
* 创建目录
*/
private static void createDir(String path,ChannelSftp sftp) throws SftpException {
String[] folders = path.split("/");
sftp.cd("/");
for ( String folder : folders ) {
if ( folder.length() > 0 ) {
try {
sftp.cd( folder );
}catch ( SftpException e ) {
sftp.mkdir( folder );
sftp.cd( folder );
}
}
}
}
/**
* 删除图片
*/
public static void delImages(String imagesName){
try {
ChannelSftp sftp = getChannel();
String path = rootPath + imagesName;
sftp.rm(path);
sftp.quit();
sftp.exit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
NginxService.java
import com.ecio.common.utils.DateTimeUtil;
import com.ecio.common.utils.FtpUtil;
import com.ecio.common.utils.IDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@Service
@Slf4j
public class NginxService {
public Object uploadPicture(MultipartFile uploadFile) {
//1、给上传的图片生成新的文件名
//1.1获取原始文件名
String oldName = uploadFile.getOriginalFilename();
//1.2使用IDUtils工具类生成新的文件名,新文件名 = newName + 文件后缀
String newName = IDUtils.genImageName();
assert oldName != null;
newName = newName + oldName.substring(oldName.lastIndexOf("."));
//1.3生成文件在服务器端存储的子目录
String filePath = DateTimeUtil.getFormatDate("/yyyyMMdd/");
// new DateTime().toString("/yyyyMMdd/");
// DateTimeUtil.getFormatDate("/yyyyMMdd/");
//2、把图片上传到图片服务器
//2.1获取上传的io流
InputStream input = null;
try {
input = uploadFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//2.2调用FtpUtil工具类进行上传
return FtpUtil.putImages(input, filePath, newName);
}
}