package com.xzz.FileText;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* @ClassName JschTestS
* @Date 20220422
* @Author lcl-xiezezhong
* @Version 1.0
*/
@RestController
@Slf4j
public class JschTestS {
@Autowired
private JSCHTest jschTest;
@RequestMapping("JSCH")
public void jschTests() throws Exception{
ArrayList<Student> objects = new ArrayList<>();
Student student = new Student();
student.setAge("12");
student.setName("李四");
Student student1 = new Student();
student1.setAge("1216");
student1.setName("张三");
objects.add(student);
objects.add(student1);
System.out.println(objects);
StringBuffer stringBuffer = new StringBuffer();
//java 集合拼接
if (!CollectionUtils.isEmpty(objects)) {
for (int i = 0; i < objects.size(); i++) {
Student student2 = objects.get(i);
stringBuffer.append(student2.getAge()+"|"+student2.getName());
stringBuffer.append("\n");
}
}
System.out.println(stringBuffer.toString());
//日期
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
Date date1 = new Date();
String format1 = simpleDateFormat1.format(date1);
//拼接 交易码+File_+日期.txt //创建服务的文件名
String fineName = "DCEP"+"File"+"_"+format1+".txt";
jschTest.sshSftp(fineName,stringBuffer.toString());
}
}
package com.xzz.FileText;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* @ClassName JSCHTest
* @Date 20220422
* @Author lcl-xiezezhong
* @Version 1.0
* jsch 用于远程连接服务
*/
@Slf4j
@Component
public class JSCHTest {
public final static org.apache.log4j.Logger LOG = Logger.getLogger(JSCHTest.class);
@Value("${http.client.B2CIP}")
private String B2CIP;
@Value("${http.client.B2CUSER}")
private String B2CUSER;
@Value("${http.client.B2CPWD}")
private String B2CPWD;
@Value("${http.client.B2CPORT}")
private String B2CPORT;
@Value("${http.client.B2CPATH}")
private String B2CPATH;
public void sshSftp(String fileName,String stringBuffer) throws Exception {
byte[] bytes = new byte[1024];
//指定的服务器地址
String ip = B2CIP;
//用户名
String user = B2CUSER;
//密码
String password = B2CPWD;
//服务器端口 默认22
int port = 0;
if(StringUtils.isNotBlank(B2CPORT)){
port = Integer.valueOf(B2CPORT);
}
//上传文件到指定服务器的指定目录 自行修改
String path = B2CPATH;
Session session = null;
Channel channel = null;
JSch jsch = new JSch();
LOG.info("查看创建文件的为服务器地址:"+ip +"|用户:"+user + "|密码:" +password + "|端口号:" +port + "|上传文件目录:" + path );
if (port <= 0) {
//连接服务器,采用默认端口
session = jsch.getSession(user, ip);
} else {
//采用指定的端口连接服务器
session = jsch.getSession(user, ip, port);
}
//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
//设置登陆主机的密码
session.setPassword(password);//设置密码
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
//设置登陆超时时间
session.connect(30000);
OutputStream outstream = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(path);
//以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
outstream = sftp.put(fileName);
ByteArrayInputStream in = new ByteArrayInputStream(stringBuffer.getBytes("UTF-8"));
int ch;
while ((ch = in.read(bytes)) != -1) {
//数据写在文件夹里
outstream.write(bytes, 0, ch);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关流操作
if (outstream == null) {
outstream.flush();
outstream.close();
}
if (session != null) {
session.disconnect();
}
if (channel != null) {
channel.disconnect();
}
}
}
}