@Configuration
public class FileUploadConfig {
@Autowired
private SftpCofig sftpCofig;
@Autowired
private MagConfig magConfig;
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory config = new MultipartConfigFactory();
config.setMaxFileSize("200MB");
config.setMaxRequestSize("200MB");
return config.createMultipartConfig();
}
@Bean
public ChannelSftp getSftp() {
//if(client==null||session==null||!client.isConnected()||!session.isConnected()){
try {
JSch jsch = new JSch();
// if (privateKey != null) {
// jsch.addIdentity(privateKey);// 设置私钥
// }
Session session = jsch.getSession(sftpCofig.getUsername(), sftpCofig.getIp(), sftpCofig.getPort());
if (sftpCofig.getPassword() != null) {
session.setPassword(sftpCofig.getPassword());
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp client = (ChannelSftp) channel;
client.cd(magConfig.getFileBasePath() +magConfig.getFiledir());
Logger.info(this, "sftp服务器连接成功");
return client;
} catch (JSchException e) {
Logger.error(this, "sftp登录失败,检测登录ip,端口号,用户名密码是否正确,错误信息为" + e.getMessage());
} catch (SftpException e) {
Logger.error(this, "sftp文件下载,目录不存在,错误信息" + e.getMessage());
}
//}
return null;
}
}
使用的时候直接@Auto注入进去。当有错误使怎么让他重新注入一遍bean节点