这是我的代码:
public class ConnectToDB {
private static final URL PEM_FILE_DIR = Thread.currentThread().getContextClassLoader().getResource("line_scrapper.pem");
private static final Logger LOGGER = Logger.getLogger(ConnectToDB.class);
private static void doSshTunnel(String strSshUser, String strSshPassword, String strSshHost, int nSshPort,
String strRemoteHost, int nLocalPort, int nRemotePort) throws JSchException, IOException,
URISyntaxException {
JSch jsch = new JSch();
jsch.addIdentity(PEM_FILE_DIR.getPath());
Session session = jsch.getSession(strSshUser, strSshHost, 22);
session.setPassword(strSshPassword);
final Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);
}
public static void ssh() {
try {
// System.out.println("start");
String strSshUser = "ec2-user"; // SSH loging username
String strSshPassword = "smartbites"; // SSH login password
String strSshHost = "54.204.144.132"; // hostname or ip or SSH
// server
int nSshPort = 22; // remote SSH host port number
String strRemoteHost = "127.0.0.1"; // hostname or ip of your
// database server
int nLocalPort = 3366; // local port number use to bind SSH tunnel
int nRemotePort = 3306; // remote port number of your database
String strDbUser = "root"; // database loging username
String strDbPassword = "smartbites"; // database login password
ConnectToDB.doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort,
nRemotePort);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:" + nLocalPort
+ "/okpanda_teachers_accounts", strDbUser, strDbPassword);
con.prepareCall("");
con.close();
} catch (Exception e) {
LOGGER.error("error ", e);
}
// finally
// {
// System.exit(0);
// }
}
}
我想连接到数据库(不是本地但在服务器上)并存储数据,为测试我创建这样的测试类并运行它:
public class HrantTest {
// public static void main(String[] args) {
// test();
// }
public static void test() {
ReadingFromTxtFile file = new ReadingFromTxtFile(new File("C:\\Users\\Hrant\\Desktop\\line"), "GMT-8:00");
ConnectToDB.ssh();
file.writeForAllFiles();
}
}
我的line_scrapper.pem文件位于rescources文件夹(它的maven项目)中。所以我运行它并连接到DB并将数据存储在DB中。 但是在构建之后。 jar文件,我运行它并抛出异常:
com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\Hrant\GitRepos\LineScraper\LineMessangerScraper\target\LineMessangerScraper-0.0.1-SNAPSHOT-jar-with-dependencies.jar!\line_scrapper.pem (The filename, directory name, or volume label syntax is incorrect)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:525)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:393)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:353)
at com.hrant.ConnectToDB.doSshTunnel(ConnectToDB.java:30)
at com.hrant.ConnectToDB.ssh(ConnectToDB.java:57)
at com.hrant.HrantTest.test(HrantTest.java:16)
at com.hrant.gui.LineFrame$3.run(LineFrame.java:124)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: file:\D:\Hrant\GitRepos\LineScraper\LineMessangerScraper\target\LineMessangerScraper-0.0.1-SNAPSHOT-jar-with-dependencies.jar!\line_scrapper.pem (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.jcraft.jsch.Util.fromFile(Util.java:508)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:522)
... 12 more
所以我不明白阅读.pem文件或
的问题jsch.addIdentity(PEM_FILE_DIR.getPath());
那怎么解决这个问题呢?
感谢。
答案 0 :(得分:1)
JSch#addIdentity(String)
expects the name of a file on the filesystem,当它被装入罐子时你没有;据推测,原因是身份信息(私钥)是敏感的,不应该包含在jar中,但应该外部化。
如果有一个绝对令人信服的理由来覆盖它,你将需要使用传递密钥作为字节数组的重载。您也可以考虑针对JSch打开一个错误,使PEM阅读器可用InputStream
或Reader
。