Java Spring sftp-读取文件失败;嵌套异常为4
我不时会在标题中指出例外情况。有人知道例外4是什么意思,或者在哪里有记录吗?我已经研究了与此相关的问题,我了解到有异常2,它显然意味着“没有这样的文件”和异常3,它是“拒绝许可”。然而,当我得到嵌套异常为4时,并没有描述,所以我不确定这意味着什么。这只是一般的读取超时吗?这种情况很少发生,而且很难重现,因为我们的一些用户根本没有问题,而有些用户偶尔会遇到这个问题Java Spring sftp-读取文件失败;嵌套异常为4,java,spring-boot,spring-integration,sftp,jsch,Java,Spring Boot,Spring Integration,Sftp,Jsch,我不时会在标题中指出例外情况。有人知道例外4是什么意思,或者在哪里有记录吗?我已经研究了与此相关的问题,我了解到有异常2,它显然意味着“没有这样的文件”和异常3,它是“拒绝许可”。然而,当我得到嵌套异常为4时,并没有描述,所以我不确定这意味着什么。这只是一般的读取超时吗?这种情况很少发生,而且很难重现,因为我们的一些用户根本没有问题,而有些用户偶尔会遇到这个问题 org.springframework.core.NestedIOException: failed to read file /ft
org.springframework.core.NestedIOException: failed to read file /ftadv:D=IBM-037,C=UTF-8/__'MAIN.FRAME.DATASET'; nested exception is 4:
at org.springframework.integration.sftp.session.SftpSession.readRaw(SftpSession.java:143)
at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.readRaw(CachingSessionFactory.java:268)
at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.readRaw(CachingSessionFactory.java:268)
at com.my.package.FTPService.ftpStoreAuthData(FTPService.java:166)
at com.my.package.FTPService.ftpStoreNetAuthData(FTPService.java:59)
at com.my.package.FTPEndpoint.ftpStoreNetAuthData(FTPEndpoint.java:27)
这是我的配置,如果有帮助的话
@Configuration
class SftpConfiguration {
@Inject
private Environment env;
SessionFactory<LsEntry> getSftpSessionFactory() {
DefaultSftpSessionFactory sftpSessionFactory =
new DefaultSftpSessionFactory(Boolean.parseBoolean(env.getRequiredProperty("sftp.isSharedSession")));
sftpSessionFactory.setHost(env.getRequiredProperty("sftp.host"));
sftpSessionFactory.setPort(Integer.parseInt(env.getRequiredProperty("sftp.port")));
sftpSessionFactory.setUser(env.getRequiredProperty("sftp.id"));
sftpSessionFactory.setPrivateKey(new FileSystemResource(env.getRequiredProperty("sftp.keyPath")));
sftpSessionFactory.setPrivateKeyPassphrase(env.getRequiredProperty("sftp.passphrase"));
sftpSessionFactory.setTimeout(Integer.parseInt(env.getRequiredProperty("sftp.timeout")));
sftpSessionFactory.setAllowUnknownKeys(Boolean.parseBoolean(env.getRequiredProperty("sftp.allowUnknownKeys")));
return new CachingSessionFactory<LsEntry>(sftpSessionFactory);
}
CachingSessionFactory getCachingSessionFactory(){
CachingSessionFactory cachingSessionFactory = new CachingSessionFactory(getSftpSessionFactory());
cachingSessionFactory.setSessionWaitTimeout(Integer.parseInt(env.getRequiredProperty("sftp.sessionWaitTimeout")));
cachingSessionFactory.setPoolSize(Integer.parseInt(env.getRequiredProperty("sftp.poolSize")));
return cachingSessionFactory;
}
}
@配置
类SftpConfiguration{
@注入
私人环境署;
SessionFactory getSftpSessionFactory(){
默认sftpSessionFactory sftpSessionFactory=
新的DefaultSftpSessionFactory(Boolean.parseBoolean(env.getRequiredProperty(“sftp.isSharedSession”));
setHost(env.getRequiredProperty(“sftp.host”);
setPort(Integer.parseInt(env.getRequiredProperty(“sftp.port”));
setUser(env.getRequiredProperty(“sftp.id”);
setPrivateKey(新文件系统资源(env.getRequiredProperty(“sftp.keyPath”));
setPrivateKeyPassphrase(env.getRequiredProperty(“sftp.passphrase”);
setTimeout(Integer.parseInt(env.getRequiredProperty(“sftp.timeout”));
setAllowunknowkeys(Boolean.parseBoolean(env.getRequiredProperty(“sftp.Allowunknowkeys”));
返回新的CachingSessionFactory(sftpSessionFactory);
}
CachingSessionFactory getCachingSessionFactory(){
CachingSessionFactory CachingSessionFactory=新的CachingSessionFactory(getSftpSessionFactory());
cachingSessionFactory.setSessionWaitTimeout(Integer.parseInt(env.getRequiredProperty(“sftp.sessionWaitTimeout”));
setPoolSize(Integer.parseInt(env.getRequiredProperty(“sftp.poolSize”));
返回cachingSessionFactory;
}
}
下面是调用代码的示例:
@Service
class FTPService {
private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(FTPEndpoint.class);
private CachingSessionFactory cachingSessionFactory;
private String adviceString;
@Inject
private FTPService(SftpConfiguration sftpConfig) {
this.cachingSessionFactory = sftpConfig.getCachingSessionFactory();
this.adviceString = sftpConfig.getAdviceString();
}
private String ftpStoreAuthData() {
Session session = this.cachingSessionFactory.getSession();
String mainframeDataSet = "'MAIN.FRAME.DATASET'";
try(BufferedInputStream inputStream = new BufferedInputStream(session.readRaw(adviceString + mainframeDataSet));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))
{
Stream<String> lines = reader.lines());
return doThingsWithFTPData(lines);
} catch (IOException ex) {
LogException.writeToFile(ex.getMessage(),ex.getStackTrace(), env.getProperty("sftp.exceptionsPath"));
}finally {
try {
if (session.isOpen()) {
session.finalizeRaw();
session.close();
}
}catch (IOException ioe){
System.out.println(ioe.getLocalizedMessage() + ioe.getCause().toString());
LogException.writeToFile(ioe.getMessage(),ioe.getStackTrace(), env.getProperty("sftp.exceptionsPath"));
throw new IOException();
}
}
return "test";
}
}
@服务
类FTP服务{
私有静态final org.apache.logging.log4j.Logger Logger=LogManager.getLogger(FTPEndpoint.class);
私人CachingSessionFactory CachingSessionFactory;
私有字符串adviceString;
@注入
专用FTP服务(SftpConfiguration sftpConfig){
this.cachingSessionFactory=sftpConfig.getCachingSessionFactory();
this.adviceString=sftpConfig.getAdviceString();
}
私有字符串ftpStoreAuthData(){
会话会话=this.cachingSessionFactory.getSession();
字符串mainframeDataSet=“'MAIN.FRAME.DATASET'”;
try(BufferedInputStream inputStream=new BufferedInputStream(session.readRaw(adviceString+mainframeDataSet));
BufferedReader reader=新的BufferedReader(新的InputStreamReader(inputStream))
{
streamlines=reader.lines());
返回带有ftpdata的dothings(行);
}捕获(IOEX异常){
LogException.writeToFile(例如getMessage()、getStackTrace()、env.getProperty(“sftp.exceptionpath”);
}最后{
试一试{
if(session.isOpen()){
session.finalizeRaw();
session.close();
}
}捕获(ioe异常ioe){
System.out.println(ioe.getLocalizedMessage()+ioe.getCause().toString());
LogException.writeToFile(ioe.getMessage()、ioe.getStackTrace()、env.getProperty(“sftp.exceptionPath”);
抛出新IOException();
}
}
返回“测试”;
}
}
查看jcraft
DEBUG选项。也许这将帮助您确定问题所在。发布完整的堆栈跟踪,包括所有“原因”部分。