您可以構建java源代碼,然後使用反射來加載java類。因此,添加一個新的Servlet來你的HTTP服務器,把它getJar
,並把在doGet
方法如下(假設你已經創建了數據庫連接):
File tmp = File.createTempFile("java-code-", ".java");
PrintWriter out = new PrintWriter(new FileWriter(tmp), true);
String classname = tmp.getName().substring(0, tmp.getName().length()-5);
out.println("public class " + classname);
out.println("{");
out.println(" public static void executeCode()");
out.println(" {");
out.println(connection.executeQuery("SELECT java-code FROM yourDb WHERE name='"
+ request.getParameter("name").replace("'", "''") + "';");
out.println(" }");
out.println("}");
out.close();
Runtime.getRuntime().exec("javac \"" + tmp.getAbsolutePath() + "\"").waitFor();
Runtime.getRuntime().exec("jar cfe \"" + tmp.getAbsolutePath() + ".jar\" \""
+ classname + "\" \"" + tmp.getAbsolutePath() + "\"");
InputStream in = new InputStream(new FileInputStream(tmp.getAbsolutePath() + ".jar"));
int read; byte[] buf = new byte[4096];
while ((read = in.read(buf)) != null)
response.getOutputStream().write(buf, 0, read);
in.close();
現在,你可以接受它,並創建一個ClassLoader
爲它,然後執行它:
ClassLoader cl = new URLClassLoader("http://localhost/getJar?name=whatever");
Class c = cl.loadClass(classname);
c.getMethod("executeCode").invoke(null);
那麼你的數據庫包括源代碼或/和字節碼? – msrd0 2014-10-06 13:32:45
你可以動態地用db中的代碼構建一個java類然後運行它。 – brso05 2014-10-06 13:37:12
@ msrd0 - 它的源代碼 – user3760419 2014-10-06 13:46:52