java - 在线程“main”java.lang.ArrayIndexOutOfBoundsException 中获取错误异常:0
问题描述
这是我得到的错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at TrfFiles2Dev.main(TrfFiles2Dev.java:55
这是我正在处理的代码
public void send (String fileName) {
String SFTPHOST = "";
int SFTPPORT = 22;
String SFTPUSER = "";
String SFTPPASS = "";
String SFTPWORKINGDIR = "";
Logger log = Logger.getLogger(TrfFiles2Dev.class.getName() );
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
System.out.println("preparing the host information for sftp.");
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();
System.out.println("Host connected.");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
File f = new File(fileName);
channelSftp.put(new FileInputStream(f), f.getName());
log.info("File transfered successfully to host.");
} catch (Exception ex) {
System.out.println("Exception found while tranfer the response.");
}
finally{
channelSftp.exit();
System.out.println("sftp Channel exited.");
channel.disconnect();
System.out.println("Channel disconnected.");
session.disconnect();
System.out.println("Host Session disconnected.");
}
}
public static void main(String[] args) {
TrfFiles2Dev trfFiles2Dev = new TrfFiles2Dev();
trfFiles2Dev.send(args[0]);
} }
预先感谢您的任何帮助。我不想为我完成这件事,我只是被困住了,需要帮助找到我的方式。
以及如何将文件从我的服务器传输到其他服务器?
解决方案
您需要提供program arguments
它才能运行。
如果您正在使用一些 ide,例如eclipse
.
Goto run-> run configurations-> give arguments-> then run
如果您正在使用 from cmd
use 类似的东西:
java TrfFiles2Dev you-file-name-here.txt
您也可以使用类fileName
来运行 while 程序。Scanner
就像是:
Scanner scn = new Scanner(System.in);
String fileName = scn.nextLine();
TrfFiles2Dev trfFiles2Dev = new TrfFiles2Dev();
trfFiles2Dev.send(args[0]);
推荐阅读
- node.js - 赫罗库 | 不能获取
- concurrency - ConcurrentHashMap::computeIfAbsent 是每个键还是每个 ConcurrentHashMap 的原子?
- firebase - 如何在 Firebase 中使用私人帐户实现追随者系统?
- python - 如何使用 for 循环简化 lst = [ x**2 for x in [x**2 for x in range(11)]] 此代码
- c++ - Win32 api中的画笔
- java - androidx.appcompat.widget.AppCompatEditText 错误
- javascript - Dropbox API Javascript - uploadFile() 导致 400 错误请求
- sql - 谈 ANSI SQL 时,JOIN 和 LEFT JOIN 有区别吗?
- r - 闪亮的应用程序错误无法将类型“环境”强制转换为“字符”类型的向量
- scala - 如何告诉 scalac 抑制警告是宏生成的代码?