1. 程式人生 > 其它 >動態呼叫jar包方法

動態呼叫jar包方法

package cn.springBoot.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import cn.springBoot.entity.User;
import cn.springBoot.service.UserService;
import cn.springBoot.util.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

/**
* <p>
* 前端控制器
* </p>
*
* @author ljx
* @since 2020-01-16
*/
@RestController
@RequestMapping("/user")
@Api(value="使用者controller",tags={"使用者操作介面"})
public class UserController {

public static void main(String[] args) {
// 系統類庫路徑
File libPath = new File("D:/jar");

// 獲取所有的.jar和.zip檔案
File[] jarFiles = libPath.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar") || name.endsWith(".zip");
}
});

if (jarFiles != null) {
// 從URLClassLoader類中獲取類所在資料夾的方法
// 對於jar檔案,可以理解為一個存放class檔案的資料夾
Method method = null;
try {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean accessible = method.isAccessible(); // 獲取方法的訪問許可權
try {
if (accessible == false) {
method.setAccessible(true); // 設定方法的訪問許可權
}
// 獲取系統類載入器
URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();;
for (File file : jarFiles) {
try {
URL url = file.toURI().toURL();
method.invoke(classLoader, url);
Class c = Class.forName("cn.springBoot.util.FileUploadUtil");
Method m = c.getMethod("getVersion",String.class);
Object s = m.invoke(c,"sss");
System.out.println("讀取jar檔案成功"+file.getName() + s.toString());
} catch (Exception e) {
System.out.println("讀取jar檔案失敗");
}
}
} finally {
method.setAccessible(accessible);
}
}
}

static class MyClassLoader extends URLClassLoader {

public MyClassLoader(URL[] urls) {
super(urls);
}

public MyClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}

public void addJar(URL url) {
this.addURL(url);
}

}
//
// private final static Logger logger = LoggerFactory.getLogger(SchoolController.class);
//
// @Autowired
// private UserService userService;
//
//
// @GetMapping("/getAll")
// @ApiOperation(value="獲取全部user介面",response=User.class,responseContainer="list")
// public List<User> findAll() {
//
// logger.info("getAll");
//
// return userService.selectList(null);
//
// }
// @GetMapping("/getById")
// @ApiOperation(value="根據id獲取user",response=User.class)
// public User getById(@RequestParam int id) {
//
// logger.info("getAll");
//
// return userService.selectByIda(id);
//
// }
//
// @GetMapping("/update")
// @ApiOperation(value="修改user",response=User.class)
// public User update(User user) {
//
// logger.info("update");
//
// return userService.updateUser(user);
//
// }
//
//
// @GetMapping("/del")
// @ApiOperation(value="刪除")
// public String deleteEmp(Integer id){
// logger.info("del");
//
// return userService.delUser(id);
// }
//
//
// @PostMapping(value = "/fileUpload")
// @ApiOperation(value="圖片上傳")
// public String fileUpload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
//
//
// return FileUploadUtil.fileUpLoad(file);
// }
//

}