parent
6cb148ff3e
commit
5736eb5a6a
@ -0,0 +1,37 @@
|
|||||||
|
package com.leaper.web.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.leaper.web.service.sftp.SftpFactory;
|
||||||
|
import com.leaper.web.service.sftp.SftpHelper;
|
||||||
|
import com.leaper.web.service.sftp.SftpPool;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
// ftp配置
|
||||||
|
@Configuration
|
||||||
|
public class SftpConfig {
|
||||||
|
@Resource
|
||||||
|
ConfigProperties properties;
|
||||||
|
|
||||||
|
// 工厂
|
||||||
|
@Bean
|
||||||
|
public SftpFactory sftpFactory(ConfigProperties properties) {
|
||||||
|
return new SftpFactory(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接池
|
||||||
|
@Bean
|
||||||
|
public SftpPool sftpPool(SftpFactory sftpFactory) {
|
||||||
|
return new SftpPool(sftpFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助类
|
||||||
|
@Bean
|
||||||
|
public SftpHelper sftpHelper(SftpPool sftpPool) {
|
||||||
|
return new SftpHelper(sftpPool);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.leaper.web.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PicData {
|
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.leaper.web.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.leaper.web.entity.LightSource;
|
||||||
|
import com.leaper.web.entity.OrderLive;
|
||||||
|
|
||||||
|
public interface OrderLiveMapper extends BaseMapper<OrderLive> {
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.leaper.web.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.leaper.web.entity.PicData;
|
||||||
|
|
||||||
|
|
||||||
|
public interface PicDataMapper extends BaseMapper<PicData> {
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
package com.leaper.web.service;
|
||||||
|
|
||||||
|
import com.sun.management.OperatingSystemMXBean;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author layman
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
getDiskInfo();
|
||||||
|
getMemoryInfo();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取系统各个硬盘的总容量、已经使用的容量、剩余容量和使用率
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void getDiskInfo() throws IOException {
|
||||||
|
DecimalFormat df = new DecimalFormat("#0.00");
|
||||||
|
File[] disks = File.listRoots();
|
||||||
|
for (File file : disks) {
|
||||||
|
// 获取盘符
|
||||||
|
System.out.print(file.getCanonicalPath() + " ");
|
||||||
|
// 获取总容量
|
||||||
|
long totalSpace = file.getTotalSpace();
|
||||||
|
// 获取剩余容量
|
||||||
|
long usableSpace = file.getUsableSpace();
|
||||||
|
// 获取已经使用的容量
|
||||||
|
long freeSpace = totalSpace - usableSpace;
|
||||||
|
// 获取使用率
|
||||||
|
float useRate = (float)((freeSpace * 1.0 / totalSpace) * 100);
|
||||||
|
System.out.print("总容量: " + transformation(totalSpace));
|
||||||
|
System.out.print("已经使用: " + transformation(freeSpace));
|
||||||
|
System.out.print("剩余容量: " + transformation(usableSpace));
|
||||||
|
System.out.println("使用率: " + Double.parseDouble(df.format(useRate)) + "% ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取内存使用情况
|
||||||
|
*/
|
||||||
|
public static void getMemoryInfo() {
|
||||||
|
OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||||
|
// 获取内存总容量
|
||||||
|
long totalMemorySize = mem.getTotalPhysicalMemorySize();
|
||||||
|
// 获取可用内存容量
|
||||||
|
long freeMemorySize = mem.getFreePhysicalMemorySize();
|
||||||
|
System.out.println("内存总容量:" + transformation(totalMemorySize) );
|
||||||
|
System.out.println("可用容量:" + transformation(freeMemorySize));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 将字节容量转化为GB
|
||||||
|
*/
|
||||||
|
public static String transformation(long size){
|
||||||
|
return size / 1024 / 1024 / 1024 + "GB"+" ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,100 @@
|
|||||||
|
package com.leaper.web.service.sftp;
|
||||||
|
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelSftp;
|
||||||
|
import com.jcraft.jsch.JSch;
|
||||||
|
import com.jcraft.jsch.JSchException;
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
|
import com.leaper.web.config.ConfigProperties;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.pool2.BasePooledObjectFactory;
|
||||||
|
import org.apache.commons.pool2.PooledObject;
|
||||||
|
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
||||||
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Slf4j
|
||||||
|
public class SftpFactory extends BasePooledObjectFactory<ChannelSftp> {
|
||||||
|
|
||||||
|
private Pool pool = new Pool();
|
||||||
|
|
||||||
|
public static class Pool extends GenericObjectPoolConfig<ChannelSftp> {
|
||||||
|
|
||||||
|
private int maxTotal = DEFAULT_MAX_TOTAL;
|
||||||
|
private int maxIdle = DEFAULT_MAX_IDLE;
|
||||||
|
private int minIdle = DEFAULT_MIN_IDLE;
|
||||||
|
|
||||||
|
public Pool() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getMaxTotal() {
|
||||||
|
return maxTotal;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setMaxTotal(int maxTotal) {
|
||||||
|
this.maxTotal = maxTotal;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getMaxIdle() {
|
||||||
|
return maxIdle;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setMaxIdle(int maxIdle) {
|
||||||
|
this.maxIdle = maxIdle;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getMinIdle() {
|
||||||
|
return minIdle;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setMinIdle(int minIdle) {
|
||||||
|
this.minIdle = minIdle;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigProperties configProperties;
|
||||||
|
|
||||||
|
public SftpFactory(ConfigProperties properties) {
|
||||||
|
this.configProperties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelSftp create() {
|
||||||
|
try {
|
||||||
|
JSch jsch = new JSch();
|
||||||
|
Session sshSession = jsch.getSession(configProperties.getSavePath().getCloudUser(),configProperties.getSavePath().getCloudIp(), 22);
|
||||||
|
sshSession.setPassword(configProperties.getSavePath().getCloudPassword());
|
||||||
|
Properties sshConfig = new Properties();
|
||||||
|
sshConfig.put("StrictHostKeyChecking", "no");
|
||||||
|
sshSession.setConfig(sshConfig);
|
||||||
|
sshSession.connect();
|
||||||
|
ChannelSftp channel = (ChannelSftp) sshSession.openChannel("sftp");
|
||||||
|
channel.connect();
|
||||||
|
return channel;
|
||||||
|
} catch (JSchException e) {
|
||||||
|
log.info("连接sfpt失败");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PooledObject<ChannelSftp> wrap(ChannelSftp channelSftp) {
|
||||||
|
return new DefaultPooledObject<>(channelSftp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 销毁对象
|
||||||
|
@Override
|
||||||
|
public void destroyObject(PooledObject<ChannelSftp> p) {
|
||||||
|
ChannelSftp channelSftp = p.getObject();
|
||||||
|
channelSftp.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,190 @@
|
|||||||
|
package com.leaper.web.service.sftp;
|
||||||
|
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelSftp;
|
||||||
|
import com.jcraft.jsch.SftpATTRS;
|
||||||
|
import com.jcraft.jsch.SftpException;
|
||||||
|
import io.swagger.models.auth.In;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
// sftp辅助类
|
||||||
|
@Slf4j
|
||||||
|
public class SftpHelper {
|
||||||
|
|
||||||
|
private SftpPool pool;
|
||||||
|
|
||||||
|
public SftpHelper(SftpPool pool) {
|
||||||
|
this.pool = pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param dir 远程目录
|
||||||
|
* @param name 远程文件名
|
||||||
|
* @return 文件字节数组
|
||||||
|
*/
|
||||||
|
public byte[] download(String dir, String name) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.cd(dir);
|
||||||
|
InputStream in = sftp.get(name);
|
||||||
|
|
||||||
|
return new byte[in.available()];
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp下载文件出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param dir 远程目录
|
||||||
|
* @param name 远程文件名
|
||||||
|
* @param in 输入流
|
||||||
|
*/
|
||||||
|
public void upload(String dir, String name, InputStream in) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
mkdirs(dir);
|
||||||
|
sftp.cd(dir);
|
||||||
|
sftp.put(in, name);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
|
||||||
|
log.info("sftp上传文件出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(String srcFilePath, String targetFilePath) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.put(srcFilePath, targetFilePath);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
|
||||||
|
log.info("sftp上传文件出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
* @param dir 远程目录
|
||||||
|
* @param name 远程文件名
|
||||||
|
*/
|
||||||
|
public void delete(String dir, String name) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.cd(dir);
|
||||||
|
sftp.rm(name);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp删除文件出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
* @param dirPath 远程目录文件名
|
||||||
|
*/
|
||||||
|
public void delete(String dirPath) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.rm(dirPath);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp删除文件出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看文件夹
|
||||||
|
* @param dirPath 远程目录文件名
|
||||||
|
*/
|
||||||
|
public List<ChannelSftp.LsEntry> ls(String dirPath) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
List<ChannelSftp.LsEntry> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
list = sftp.ls(dirPath);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp查看文件夹失败");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件是否存在
|
||||||
|
* @param dir 远程文件名
|
||||||
|
*/
|
||||||
|
public boolean isExist(String dir) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.stat(dir);
|
||||||
|
return true;
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp文件不存在");
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归创建多级目录
|
||||||
|
*
|
||||||
|
* @param dir 多级目录
|
||||||
|
*/
|
||||||
|
public void mkdirs(String dir) {
|
||||||
|
String[] folders = dir.split("/");
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.cd("/");
|
||||||
|
for (String folder: folders) {
|
||||||
|
if (folder.length()>0) {
|
||||||
|
try {
|
||||||
|
sftp.cd(folder);
|
||||||
|
} catch (Exception e) {
|
||||||
|
sftp.mkdir(folder);
|
||||||
|
sftp.cd(folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp创建目录出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rmdir(String dirPath) {
|
||||||
|
ChannelSftp sftp = pool.borrowObject();
|
||||||
|
try {
|
||||||
|
sftp.rmdir(dirPath);
|
||||||
|
} catch (SftpException e) {
|
||||||
|
log.info("sftp删除文件夹出错");
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
pool.returnObject(sftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
package com.leaper.web.service.sftp;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.ChannelSftp;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||||
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Data
|
||||||
|
public class SftpPool {
|
||||||
|
|
||||||
|
private GenericObjectPool<ChannelSftp> pool;
|
||||||
|
|
||||||
|
public SftpPool(SftpFactory factory) {
|
||||||
|
this.pool = new GenericObjectPool<>(factory, factory.getPool());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取一个sftp连接对象
|
||||||
|
* @return sftp连接对象
|
||||||
|
*/
|
||||||
|
public ChannelSftp borrowObject() {
|
||||||
|
try {
|
||||||
|
return pool.borrowObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("获取ftp连接失败");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 归还一个sftp连接对象
|
||||||
|
* @param channelSftp sftp连接对象
|
||||||
|
*/
|
||||||
|
public void returnObject(ChannelSftp channelSftp) {
|
||||||
|
if (channelSftp!=null) {
|
||||||
|
pool.returnObject(channelSftp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue