这是我的代码:
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 文件位于 resources 文件夹中(它是 Maven 项目)。所以我运行它,它连接到数据库并将数据存储在数据库中。 但是构建之后。 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());
那么如何解决这个问题呢?
谢谢。
最佳答案
JSch#addIdentity(String)
expects the name of a file on the filesystem ,当它被装进 jar 里时你就没有了;据推测,原因是身份信息(私钥)是敏感的,不应该包含在 jar 中,而应该外部化。
如果有一个压倒性的令人信服的理由来覆盖它,您将需要使用将键作为字节数组传递的重载。您还可以考虑针对 JSch 打开一个错误,以使 PEM 读取器可用于 InputStream
或 Reader
。
关于java - 读取 .pem 文件时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24058031/