为什么我的 return 语句 return 结果为空?
why does my return statement return a null result?
我正在尝试连接到远程计算机并显示特定目录中的一些文件。
问题是当我测试我的函数时 returns 结果为空,我想做的是显示文件名。
这是我的代码:
@Service
public class SftpClientImpl implements SftpClient {
private LsEntry entry;
@Override
public LsEntry connectToServer() {
String SFTPHOST = "xxxxx";
int SFTPPORT = 22;
String SFTPUSER = "xxx";
String SFTPPASS = "xxxxx";
String SFTPWORKINGDIR = "/dir/dir2/dir3";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
System.out.println("Starting the session ..");
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
for(int i=0; i<filelist.size();i++){
LsEntry entry = (LsEntry) filelist.get(i);
System.out.println(entry.getFilename());
}
while(session != null){
System.out.println("Killing the session");
session.disconnect();
System.exit(0);
}
}catch(Exception ex){
ex.printStackTrace();
}
return entry;
}
}
和:
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<LsEntry> getDirectories() {
LsEntry entry = sftpClient.connectToServer();
return new ResponseEntity<>(entry, HttpStatus.OK);
}
知道为什么这不起作用吗?
entry
为空,因为它的值仅包含在 for 循环中,并且实际上被声明了两次(一次在私有 class 范围内,一次在 for 循环内的本地范围内)。
我的建议是更正您的变量声明并测试连接和文件名打印。如果它仍然不起作用,请在已知工作的 spring 端点内尝试。如果它按预期打印您的目录,而不是移动到它自己的端点并重试。这样做有助于缩小问题的范围。
在过去的几年里,我一直使用下面的代码来连接和打印文件名,并且很大程度上基于当时 JSCH 提供的示例代码:
JSch jsch = new JSch();
Session session;
session = jsch.getSession(username, hostname, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
//List our files within the directory
Vector vv = sftpChannel.ls(srcDir);
if (vv != null) {
LOGGER.debug("We have a file listing!");
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof ChannelSftp.LsEntry) {
LOGGER.debug("[" + ((ChannelSftp.LsEntry) obj).getFilename() + "]");
if (ii < 1) { // empty directory contains entries for . and ..
continue;
}
String filename = ((ChannelSftp.LsEntry) obj).getFilename();
filenames.add(filename);
LOGGER.debug("filename is: {}", filename);
....
这就是我解决问题的方法:)
我更正了我的变量声明并且它工作得很好,就像你告诉我的那样:)谢谢
@Override
public LsEntry connectToServer() {
String HOST = "xxxxx";
int PORT = 22;
String USER = "xxx";
String PASS = "xxxxx";
String DIR = "/dir/dir2/dir3";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
// LsEntry declaration
LsEntry entry = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(USER, HOST, PORT);
session.setPassword(PASS);
// the rest of the code
//....
//...
for (int i = 0; i < filelist.size(); i++) {
//cast
entry = (LsEntry) filelist.get(i);
System.out.println(((LsEntry) entry).getFilename());
}
while (session != null) {
System.out.println("Killing the session");
session.disconnect();
System.exit(0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return (LsEntry) entry;
}
我正在尝试连接到远程计算机并显示特定目录中的一些文件。
问题是当我测试我的函数时 returns 结果为空,我想做的是显示文件名。
这是我的代码:
@Service
public class SftpClientImpl implements SftpClient {
private LsEntry entry;
@Override
public LsEntry connectToServer() {
String SFTPHOST = "xxxxx";
int SFTPPORT = 22;
String SFTPUSER = "xxx";
String SFTPPASS = "xxxxx";
String SFTPWORKINGDIR = "/dir/dir2/dir3";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
System.out.println("Starting the session ..");
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
for(int i=0; i<filelist.size();i++){
LsEntry entry = (LsEntry) filelist.get(i);
System.out.println(entry.getFilename());
}
while(session != null){
System.out.println("Killing the session");
session.disconnect();
System.exit(0);
}
}catch(Exception ex){
ex.printStackTrace();
}
return entry;
}
}
和:
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<LsEntry> getDirectories() {
LsEntry entry = sftpClient.connectToServer();
return new ResponseEntity<>(entry, HttpStatus.OK);
}
知道为什么这不起作用吗?
entry
为空,因为它的值仅包含在 for 循环中,并且实际上被声明了两次(一次在私有 class 范围内,一次在 for 循环内的本地范围内)。
我的建议是更正您的变量声明并测试连接和文件名打印。如果它仍然不起作用,请在已知工作的 spring 端点内尝试。如果它按预期打印您的目录,而不是移动到它自己的端点并重试。这样做有助于缩小问题的范围。
在过去的几年里,我一直使用下面的代码来连接和打印文件名,并且很大程度上基于当时 JSCH 提供的示例代码:
JSch jsch = new JSch();
Session session;
session = jsch.getSession(username, hostname, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
//List our files within the directory
Vector vv = sftpChannel.ls(srcDir);
if (vv != null) {
LOGGER.debug("We have a file listing!");
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof ChannelSftp.LsEntry) {
LOGGER.debug("[" + ((ChannelSftp.LsEntry) obj).getFilename() + "]");
if (ii < 1) { // empty directory contains entries for . and ..
continue;
}
String filename = ((ChannelSftp.LsEntry) obj).getFilename();
filenames.add(filename);
LOGGER.debug("filename is: {}", filename);
....
这就是我解决问题的方法:) 我更正了我的变量声明并且它工作得很好,就像你告诉我的那样:)谢谢
@Override
public LsEntry connectToServer() {
String HOST = "xxxxx";
int PORT = 22;
String USER = "xxx";
String PASS = "xxxxx";
String DIR = "/dir/dir2/dir3";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
// LsEntry declaration
LsEntry entry = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(USER, HOST, PORT);
session.setPassword(PASS);
// the rest of the code
//....
//...
for (int i = 0; i < filelist.size(); i++) {
//cast
entry = (LsEntry) filelist.get(i);
System.out.println(((LsEntry) entry).getFilename());
}
while (session != null) {
System.out.println("Killing the session");
session.disconnect();
System.exit(0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return (LsEntry) entry;
}