首页 > 解决方案 > 在线程“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]);
}   }

预先感谢您的任何帮助。我不想为我完成这件事,我只是被困住了,需要帮助找到我的方式。

以及如何将文件从我的服务器传输到其他服务器?

标签: javajsch

解决方案


您需要提供program arguments它才能运行。

如果您正在使用一些 ide,例如eclipse.

Goto run-> run configurations-> give arguments-> then run

如果您正在使用 from cmduse 类似的东西:

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]);

推荐阅读