fix:1.编写ftp检查目录、删除文件、写入文件功能2.修改邮箱数据表字段
This commit is contained in:
parent
f0d2d5f108
commit
c618e6957f
|
@ -2,6 +2,9 @@ package org.jeecg.common.util;
|
|||
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.saxon.trans.SymbolicName;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
@ -10,8 +13,7 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
@ -48,9 +50,11 @@ public class FTPUtil {
|
|||
// 切换为本地被动模式,可以解决FTP上传后文件为空的问题,但需要服务器将FTP服务添加至防火墙白名单
|
||||
ftp.enterLocalPassiveMode();
|
||||
//连接
|
||||
ftp.connect(host, port);
|
||||
ftp.connect("182.92.183.230", 21);
|
||||
// ftp.connect(host, port);
|
||||
//登录
|
||||
ftp.login(userName, password);
|
||||
ftp.login("xiao", "123456");
|
||||
// ftp.login(userName, password);
|
||||
//判断是否连接成功
|
||||
int reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
|
@ -129,4 +133,132 @@ public class FTPUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查目录是否存在,不存在则创建,支持递归创建
|
||||
* @param path 目录路径
|
||||
* @return 返回值true/false
|
||||
*/
|
||||
public boolean checkDirectory(String path) {
|
||||
final FTPClient ftpClient = this.LoginFTP();
|
||||
try {
|
||||
final boolean flag = this.checkDirectory(ftpClient, path);
|
||||
return flag;
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查目录是否存在,不存在则创建,支持递归创建
|
||||
* @param ftpClient FTP客户端
|
||||
* @param path 目录路径
|
||||
* @return 返回值true/false
|
||||
* @throws IOException
|
||||
*/
|
||||
private boolean checkDirectory(FTPClient ftpClient,String path) throws IOException {
|
||||
final String rootPath = ftpClient.printWorkingDirectory();
|
||||
final boolean changeFlag = ftpClient.changeWorkingDirectory(rootPath);
|
||||
if(!changeFlag){
|
||||
log.error("{},根目录切换失败",rootPath);
|
||||
return false;
|
||||
}
|
||||
String[] directories = path.split("/");
|
||||
for(String directory : directories){
|
||||
if(StringUtils.isEmpty(directory)){
|
||||
continue;
|
||||
}
|
||||
if(!ftpClient.changeWorkingDirectory(directory)){
|
||||
if(!ftpClient.makeDirectory(directory)){
|
||||
log.error("{},目录创建失败",directory);
|
||||
return false;
|
||||
}
|
||||
if(!ftpClient.changeWorkingDirectory(directory)){
|
||||
log.error("{},目录切换失败",directory);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文件,若文件或文件目录不存在则自行创建
|
||||
* @param filePath 文件路径
|
||||
* @param fileName 文件名称
|
||||
* @param inputStream 文件输入流
|
||||
* @return 返回值true/false
|
||||
*/
|
||||
public boolean saveFile(String filePath,String fileName,InputStream inputStream){
|
||||
final FTPClient ftpClient = this.LoginFTP();
|
||||
try{
|
||||
final boolean flag = this.checkDirectory(ftpClient,filePath);
|
||||
if(flag){
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
String encodedName = new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
|
||||
final boolean result = ftpClient.storeFile(encodedName, inputStream);
|
||||
return result;
|
||||
}
|
||||
}catch (IOException e){
|
||||
log.error("{}文件创建失败,原因为:{}",filePath+"/"+fileName,e.getMessage());
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param filePath 文件路径
|
||||
* @param fileName 文件名称
|
||||
* @return 返回值true/false
|
||||
*/
|
||||
public boolean removeFile(String filePath,String fileName){
|
||||
final FTPClient ftpClient = this.LoginFTP();
|
||||
try {
|
||||
final String rootPath = ftpClient.printWorkingDirectory();
|
||||
final boolean changeFlag = ftpClient.changeWorkingDirectory(rootPath);
|
||||
if(!changeFlag){
|
||||
log.error("{},根目录切换失败",rootPath);
|
||||
return false;
|
||||
}
|
||||
String[] directories = filePath.split("/");
|
||||
for(String directory : directories){
|
||||
if(StringUtils.isEmpty(directory)){
|
||||
continue;
|
||||
}
|
||||
if(!ftpClient.changeWorkingDirectory(directory)){
|
||||
log.error("此文件目录不存在:{}",filePath);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
boolean result = ftpClient.deleteFile(new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
|
||||
if(!result){
|
||||
log.error("此文件不存在:{}",filePath+"/"+fileName);
|
||||
}
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
log.error("{}文件删除失败,原因为:{}",filePath,e.getMessage());
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}finally {
|
||||
try {
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,9 @@ import org.springframework.format.annotation.DateTimeFormat;
|
|||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 邮件管理数据表
|
||||
*/
|
||||
@Data
|
||||
@TableName(value = "sys_email")
|
||||
public class SysEmail implements Serializable {
|
||||
|
@ -18,37 +21,73 @@ public class SysEmail implements Serializable {
|
|||
@TableId(value = "id", type = IdType.ASSIGN_ID)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 邮箱名称
|
||||
*/
|
||||
@TableField(value = "name")
|
||||
private String name;
|
||||
|
||||
@TableField(value = "status")
|
||||
private Integer status;
|
||||
/**
|
||||
* 邮箱类型(1-收件地址,2-发件地址)
|
||||
*/
|
||||
@TableField(value = "emil_type")
|
||||
private Integer emilType;
|
||||
|
||||
/**
|
||||
* email地址
|
||||
*/
|
||||
@TableField(value = "username")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 邮箱登录密码
|
||||
*/
|
||||
@TableField(value = "password")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
@TableField(value = "port")
|
||||
private String port;
|
||||
|
||||
/**
|
||||
* 是否启用邮箱(0-不启用,1-启用)
|
||||
*/
|
||||
@TableField(value = "enabled")
|
||||
private Integer enabled;
|
||||
|
||||
/**
|
||||
* 监测周期(秒)
|
||||
*/
|
||||
@TableField(value = "monitoring_cycle")
|
||||
private Integer monitoringCycle;
|
||||
|
||||
/**
|
||||
* 创建日期
|
||||
*/
|
||||
@TableField(value = "create_time")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 创建人员
|
||||
*/
|
||||
@TableField(value = "create_by")
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 修改日期
|
||||
*/
|
||||
@TableField(value = "update_time")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 修改人员
|
||||
*/
|
||||
@TableField(value = "update_by")
|
||||
private String updateBy;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user