最近的项目要用jsch把本地文件上传到服务器,但是出现问题:
public static void main(String[] args) {
try {
upload("/home/informix", "C:\\Users\\Administrator\\Desktop\\upload.jsp", getSession());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获得指定用户session
public static Session getSession() throws JSchException {
JSch jsch = new JSch();// 创建JSch对象
Session session = jsch.getSession("informix", "192.168.226.166", 22);
session.setPassword("ffcsffcs");
Properties config = new Properties();
// 设置 SSH 连接时不进行公钥确认
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
return session;
}
//上传单个文件
public static void upload(String remotePath, String uploadFile, Session session)
throws JSchException, FileNotFoundException, SftpException {
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.cd(remotePath);
File file = new File(uploadFile);
channelSftp.put(new FileInputStream(file), file.getName());
//channelSftp.put(new FileInputStream(file), remotePath, ChannelSftp.OVERWRITE);
System.out.println("Upload Success!");
}
异常:
4:
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:285)
at cn.ffcs.util.JSchUtils.upload(JSchUtils.java:121)
at cn.ffcs.util.JSchUtils.main(JSchUtils.java:29)
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.Packet.reset(Packet.java:43)
at com.jcraft.jsch.ChannelSftp.sendPacketPath(ChannelSftp.java:2025)
at com.jcraft.jsch.ChannelSftp.sendREALPATH(ChannelSftp.java:1954)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:1807)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:268)
... 2 more
求大神指点