package com.meritdata.cloud.cosp.bank.util;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Properties;
public class ConnectFtpUtil {
private static final Logger LOG = LoggerFactory.getLogger(ConnectFtpUtil.class);
public static void main(String[] args) {
listFileNames("8.140.110.141", 22, "root", "1qaz!QAZ","/opt/","113213");
}
private static void listFileNames(String host, int port, String username, final String password,String path,String name) {
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
LOG.debug("Session connected!");
channel = sshSession.openChannel("sftp");
channel.connect();
LOG.debug("Channel connected!");
sftp = (ChannelSftp) channel;
// sftp.mkdir("创建文件的地址加名字");
File file = new File("D:/temp/1423.txt");
if(!file.exists()){
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write("我是java");
bw.newLine();//换行,在不同的系统中会展示不同的符号,如果在Linux上不要读取到^M,则不应使用这种换行方式,直接在字符后面接\n
bw.write("哇,哇哇");
bw.newLine();
bw.write("结束来了来了");
bw.newLine();
bw.newLine();
bw.newLine();
bw.write("123");
bw.flush();//刷新缓冲流,把数据写进该文件。如果存在for循环,一定要在for循环里添加bw.flush()
bw.close();
sftp.cd("/opt/");
InputStream in = new FileInputStream(file);
sftp.put(in, file.getName());
in.close();
File file1 = new File("D:/temp");
deleteFolder(file1);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeChannel(sftp);
closeChannel(channel);
closeSession(sshSession);
}
}
/** private static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if(files !=null){
for(File f:files){
if(f.isDirectory()){
deleteFolder(f);
}else {
f.delete();
}
}
}
}
*/
private static void closeChannel(Channel channel) {
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
}
private static void closeSession(Session session) {
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
}
maven
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
package com.meritdata.cloud.cosp.bank.job;
import com.meritdata.cloud.cosp.bank.config.BankServiceConfig;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
/**
* @Author 72745
* @Date 2021/7/1 21:56
* @Version 1.0
* 定时删除本地生成的txt文件
*/
@Component(value = "deleteFileJob")
@JobHandler(value = "deleteFileJob")
@Slf4j
public class DeleteFileJob extends IJobHandler {
@Autowired
private BankServiceConfig bankServiceConfig;
@Override
public ReturnT<String> execute(String param) {
File file = null;
try {
file = new File(bankServiceConfig.getFtptempFile());
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
log.error("创建目录失败",e);
}
System.out.println("result:"+delFiles(file));
File [] files = file.listFiles();
String fileName = "";
try {
for(File f: files){
fileName = f.getName();
if(fileName.endsWith(".txt")){
f.delete();
}
}
} catch (Exception e) {
log.info("删除失败文件",e);
return FAIL;
}
log.info("删除文件成功");
return SUCCESS;
}
public static boolean delFiles(File file){
boolean result = false;
//目录
if(file.isDirectory()){
File[] childrenFiles = file.listFiles();
for (File childFile:childrenFiles){
result = delFiles(childFile);
if(!result){
return result;
}
}
}
//删除 文件、空目录
result = file.delete();
return result;
}
}