资源工具

master
wangbing 4 years ago
parent 46ad3a03f1
commit 9e16cc8377

@ -16,7 +16,6 @@ import javafx.scene.control.CheckBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
@ -64,6 +63,7 @@ import xyz.wbsite.dbtool.javafx.tool.Tool;
import xyz.wbsite.dbtool.javafx.view.DBCheckBoxTableCell;
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
@ -112,15 +112,12 @@ public class JavaFxApplication extends Application {
System.exit(0);
}
});
primaryStage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
primaryStage.getIcons().add(new Image(ResourceUtil.getInput("icon.png")));
}
@Override
public void init() throws Exception {
URL main = JavaFxApplication.class.getClassLoader().getResource("fxml/main.fxml");
if (main == null) {
main = getClass().getResource("../../../fxml/main.fxml");
}
URL main = ResourceUtil.getURL("classpath:fxml/main.fxml");
mMainLoader = new FXMLLoader(main);
mMainLoader.load();
mainController = mMainLoader.getController();
@ -1171,7 +1168,7 @@ public class JavaFxApplication extends Application {
if (fieldType.isFix() && fieldType.getDefaultLength() != field.getFieldLength()) {
field.setFieldLength(fieldType.getDefaultLength());
}
if (!field.getFieldType().contains("String_var")){
if (!field.getFieldType().contains("String_var")) {
field.setIsLike(false);
}
if (field.getIsSystem()) {

@ -10,6 +10,7 @@ import xyz.wbsite.dbtool.javafx.tool.Tool;
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -96,22 +97,22 @@ public class ApiCallable implements Callable {
HashMap<String, Object> ctx = new HashMap<String, Object>();
ctx.put("domain", String.join(".", domainList));
for (String name : ResourceUtil.getResourceFiles("/modules/Java_api/frame/base/")) {
for (String name : ResourceUtil.listFileName("/modules/Java_api/frame/base/")) {
freeMarkerManager.outputTemp(Tool.createFile(base, name), "Java_api/frame/base/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/Java_api/frame/client/")) {
for (String name : ResourceUtil.listFileName("/modules/Java_api/frame/client/")) {
freeMarkerManager.outputTemp(Tool.createFile(client, name), "Java_api/frame/client/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/Java_api/frame/utils/")) {
for (String name : ResourceUtil.listFileName("/modules/Java_api/frame/utils/")) {
if (name.equals("ValidationUtil.java")){//去掉验证 减少体积
continue;
}
freeMarkerManager.outputTemp(Tool.createFile(utils, name), "Java_api/frame/utils/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/Java_api/frame/okhttp3/")) {
for (String name : ResourceUtil.listFileName("/modules/Java_api/frame/okhttp3/")) {
freeMarkerManager.outputTemp(Tool.createFile(okhttp3, name), "Java_api/frame/okhttp3/" + name, ctx);
}
}

@ -19,12 +19,15 @@ import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
public class SpringBootCallable implements Callable {
@ -155,7 +158,7 @@ public class SpringBootCallable implements Callable {
if (project.getFrame().value() == Frame..value()) {//管理架构
File wsys = Tool.createPath(ajax, "wsys");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/action/ajax/wsys/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/action/ajax/wsys/")) {
freeMarkerManager.outputTemp(Tool.createFile(wsys, name), "SpringBoot/java/action/ajax/wsys/" + name, ctx);
}
}
@ -185,7 +188,7 @@ public class SpringBootCallable implements Callable {
if (project.getFrame().value() >= Frame..value()) {//生成系统模块
File wsys = Tool.createPath(api, "wsys");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/action/api/wsys/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/action/api/wsys/")) {
freeMarkerManager.outputTemp(Tool.createFile(wsys, name), "SpringBoot/java/action/api/wsys/" + name, ctx);
}
}
@ -337,15 +340,15 @@ public class SpringBootCallable implements Callable {
File mgr = Tool.createPath(root, "wsys", "mgr");
File req = Tool.createPath(root, "wsys", "req");
File rsp = Tool.createPath(root, "wsys", "rsp");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/module/wsys/ent/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/module/wsys/ent/")) {
freeMarkerManager.outputTemp(Tool.createFile(ent, name), "SpringBoot/java/module/wsys/ent/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/module/wsys/mgr/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/module/wsys/mgr/")) {
freeMarkerManager.outputTemp(Tool.createFile(mgr, name), "SpringBoot/java/module/wsys/mgr/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/module/wsys/mpr/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/module/wsys/mpr/")) {
if (name.endsWith(".java")) {
freeMarkerManager.outputTemp(Tool.createFile(mpr, name), "SpringBoot/java/module/wsys/mpr/" + name, ctx);
} else if (name.contains(project.getDatabase().name())) {
@ -353,11 +356,11 @@ public class SpringBootCallable implements Callable {
}
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/module/wsys/req/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/module/wsys/req/")) {
freeMarkerManager.outputTemp(Tool.createFile(req, name), "SpringBoot/java/module/wsys/req/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/module/wsys/rsp/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/module/wsys/rsp/")) {
freeMarkerManager.outputTemp(Tool.createFile(rsp, name), "SpringBoot/java/module/wsys/rsp/" + name, ctx);
}
}
@ -384,7 +387,7 @@ public class SpringBootCallable implements Callable {
//auth
{
File auth = Tool.createPath(root, "auth");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/auth/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/auth/")) {
freeMarkerManager.outputTemp(Tool.createFile(auth, name), "SpringBoot/java/frame/auth/" + name, ctx);
}
}
@ -392,7 +395,7 @@ public class SpringBootCallable implements Callable {
//base
{
File base = Tool.createPath(root, "base");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/base/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/base/")) {
freeMarkerManager.outputTemp(Tool.createFile(base, name), "SpringBoot/java/frame/base/" + name, ctx);
}
}
@ -400,7 +403,7 @@ public class SpringBootCallable implements Callable {
//utils
{
File utils = Tool.createPath(root, "utils");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/utils/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/utils/")) {
freeMarkerManager.outputTemp(Tool.createFile(utils, name), "SpringBoot/java/frame/utils/" + name, ctx);
}
}
@ -408,7 +411,7 @@ public class SpringBootCallable implements Callable {
//schedule
{
File schedule = Tool.createPath(root, "schedule");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/schedule/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/schedule/")) {
freeMarkerManager.outputTemp(Tool.createFile(schedule, name), "SpringBoot/java/frame/schedule/" + name, ctx);
}
}
@ -420,22 +423,22 @@ public class SpringBootCallable implements Callable {
File exception = Tool.createPath(excel, "exception");
File excelhandler = Tool.createPath(excel, "handler");
File excellistener = Tool.createPath(excel, "listener");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/")) {
freeMarkerManager.outputTemp(Tool.createFile(excel, name), "SpringBoot/java/frame/excel/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/annotation/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/annotation/")) {
freeMarkerManager.outputTemp(Tool.createFile(excelannotation, name), "SpringBoot/java/frame/excel/annotation/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/converter/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/converter/")) {
freeMarkerManager.outputTemp(Tool.createFile(excelconverter, name), "SpringBoot/java/frame/excel/converter/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/exception/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/exception/")) {
freeMarkerManager.outputTemp(Tool.createFile(exception, name), "SpringBoot/java/frame/excel/exception/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/handler/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/handler/")) {
freeMarkerManager.outputTemp(Tool.createFile(excelhandler, name), "SpringBoot/java/frame/excel/handler/" + name, ctx);
}
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/excel/listener/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/excel/listener/")) {
freeMarkerManager.outputTemp(Tool.createFile(excellistener, name), "SpringBoot/java/frame/excel/listener/" + name, ctx);
}
}
@ -445,7 +448,7 @@ public class SpringBootCallable implements Callable {
File mail = Tool.createPath(root, "mail");
File message = Tool.createPath(mail, "message");
freeMarkerManager.outputTemp(Tool.createFile(mail, "WMailSender.java"), "SpringBoot/java/frame/mail/WMailSender.java", ctx);
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/mail/message/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/mail/message/")) {
freeMarkerManager.outputTemp(Tool.createFile(message, name), "SpringBoot/java/frame/mail/message/" + name, ctx);
}
}
@ -453,7 +456,7 @@ public class SpringBootCallable implements Callable {
//validation
{
File validation = Tool.createPath(root, "validation");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/frame/validation/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/frame/validation/")) {
freeMarkerManager.outputTemp(Tool.createFile(validation, name), "SpringBoot/java/frame/validation/" + name, ctx);
}
}
@ -476,7 +479,7 @@ public class SpringBootCallable implements Callable {
ctx.put("moduleName", project.getName());
//task
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/java/task/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/java/task/")) {
if (name.contains("TokenTask") && project.getFrame().value() == Frame..value()) {
continue;
}
@ -548,7 +551,7 @@ public class SpringBootCallable implements Callable {
if (wsysModule == null || !wsysModule.getNeedGenerate()) {// 覆盖
File wsys = Tool.createPath(dbtool, "wsys_table");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/resources/dbtool/wsys_table/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/resources/dbtool/wsys_table/")) {
if (name.contains(project.getDatabase().name())) {
freeMarkerManager.outputTemp(Tool.createFile(wsys, name), "SpringBoot/resources/dbtool/wsys_table/" + name, ctx);
}
@ -656,7 +659,7 @@ public class SpringBootCallable implements Callable {
Module wsysModule = tryGetModule(project, "wsys");// 防止与系统冲突,一般不会重复,通常是架构重新设计时遇到
if (wsysModule == null || !wsysModule.getNeedGenerate()) {
File wsys = Tool.createPath(screen, "wsys");
for (String name : ResourceUtil.getResourceFiles("/modules/SpringBoot/resources/templates/screen/module/wsys/")) {
for (String name : ResourceUtil.listFileName("/modules/SpringBoot/resources/templates/screen/module/wsys/")) {
Tool.outputResource("SpringBoot/resources/templates/screen/module/wsys/" + name, Tool.createFile(wsys, name));
}
}
@ -730,7 +733,7 @@ public class SpringBootCallable implements Callable {
ctx.put("project", project);
File wsys = Tool.createPath(root, "wsys");
for (String apiFile : ResourceUtil.getResourceFiles("/modules/SpringBoot/test/wsys/")) {
for (String apiFile : ResourceUtil.listFileName("/modules/SpringBoot/test/wsys/")) {
freeMarkerManager.outputTemp(Tool.createFile(wsys, apiFile), "SpringBoot/test/wsys/" + apiFile, ctx);
}
freeMarkerManager.outputTemp(Tool.createFile(root, "DataInit.java"), "SpringBoot/test/DataInit.java", ctx);
@ -745,4 +748,5 @@ public class SpringBootCallable implements Callable {
}
return null;
}
}

@ -122,7 +122,7 @@ public class Dialog {
public static void showYesNo(String title, String message, ConfirmCall call) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
stage.getIcons().add(new Image(ResourceUtil.getInput("icon.png")));
alert.setHeaderText(null);
alert.setTitle(title);
alert.setContentText(message);
@ -143,7 +143,7 @@ public class Dialog {
public void run() {
Alert alert = new Alert(Alert.AlertType.ERROR);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
stage.getIcons().add(new Image(ResourceUtil.getInput("icon.png")));
alert.setTitle("错误");
alert.setX(JavaFxApplication.primaryStage.getX() + JavaFxApplication.primaryStage.getWidth() / 2 - 213);
alert.setY(JavaFxApplication.primaryStage.getY() + JavaFxApplication.primaryStage.getHeight() / 2 - 70);
@ -159,7 +159,7 @@ public class Dialog {
public void run() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
stage.getIcons().add(new Image(ResourceUtil.getInput("icon.png")));
alert.setTitle("消息");
alert.setX(JavaFxApplication.primaryStage.getX() + JavaFxApplication.primaryStage.getWidth() / 2 - 213);
alert.setY(JavaFxApplication.primaryStage.getY() + JavaFxApplication.primaryStage.getHeight() / 2 - 70);
@ -177,7 +177,7 @@ public class Dialog {
dialog = new javafx.scene.control.Dialog();
dialog.setTitle("提示");
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
stage.getIcons().add(new Image(ResourceUtil.getInput("icon.png")));
dialog.setResult("1");
dialog.setX(JavaFxApplication.primaryStage.getX() + JavaFxApplication.primaryStage.getWidth() / 2 - 68);
dialog.setY(JavaFxApplication.primaryStage.getY() + JavaFxApplication.primaryStage.getHeight() / 2 - 70);

@ -11,6 +11,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -126,6 +128,37 @@ public class FileUtil {
}
}
public static FileOutputStream openOutputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
} else if (!file.canRead()) {
throw new IOException("File '" + file + "' cannot be read");
} else {
return new FileOutputStream(file);
}
} else {
throw new FileNotFoundException("File '" + file + "' does not exist");
}
}
/**
* @param fileLocation
* @return URL
*/
public static URL getURL(String fileLocation) {
try {
return new URL(fileLocation);
} catch (MalformedURLException var6) {
try {
return (new File(fileLocation)).toURI().toURL();
} catch (MalformedURLException var5) {
var5.printStackTrace();
}
}
return null;
}
public static byte[] toByteArray(InputStream input, long size) throws IOException {
if (size > 2147483647L) {
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);

@ -1,19 +1,21 @@
package xyz.wbsite.dbtool.web.frame.utils;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@ -25,27 +27,78 @@ import java.util.regex.Pattern;
*/
public class ResourceUtil extends ResourceUtils {
/**
*
* :E:\...\classes
* JAR: E:\...\***.jar
*
* @return jar
*/
public static File getApplicationHome() {
ApplicationHome home = new ApplicationHome(ResourceUtil.class);
return home.getSource();
}
/**
*
*
* @param resourcePath
* @return
*/
public static List<String> listFileName(String resourcePath) {
if (!resourcePath.endsWith("/")) {
resourcePath = resourcePath + "/";
}
List<String> result = new ArrayList<>();
List<URL> urls = ResourceUtil.listURL(resourcePath);
for (URL url : urls) {
File file1 = new File(url.getFile());
if (file1.isFile()) {
result.add(file1.getName());
} else if (!file1.isDirectory() && !url.getFile().endsWith("/")) {
result.add(file1.getName());
}
}
return result;
}
/**
* @param resourceLocation
* @return URL
* @throws FileNotFoundException
*/
public static URL getURL(String resourceLocation) throws FileNotFoundException {
if (!resourceLocation.startsWith("classpath:")) {
resourceLocation = "classpath:" + resourceLocation;
}
return ResourceUtils.getURL(resourceLocation);
}
/**
* : /modules/dir/
*
* @param resourcePath
* @return
*/
public static List<String> getResourceFiles(String resourcePath) {
List<String> result = new ArrayList<>();
public static List<URL> listURL(String resourcePath) {
if (!resourcePath.startsWith("/")) {
resourcePath = "/" + resourcePath;
}
if (!resourcePath.endsWith("/")) {
resourcePath = resourcePath + "/";
}
List<URL> result = new ArrayList<>();
File applicationHome = getApplicationHome();
if (applicationHome.getAbsolutePath().endsWith(".jar")) {
if (applicationHome.getName().endsWith(".jar")) {
try {
Pattern pattern = Pattern.compile(".*" + resourcePath + "(.+\\..+)");
Pattern pattern = Pattern.compile("BOOT-INF/classes" + resourcePath + "[^/]+/?$");
JarFile jarFile = new JarFile(applicationHome);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
String name = jarEntry.getName();
if (name.matches(".*" + resourcePath + "(.+\\..+)")) {
Matcher matcher = pattern.matcher(name);
if (matcher.find()) result.add(matcher.group(1));
if (pattern.matcher(name).find()) {
result.add(getURL(name));
}
}
} catch (IOException e) {
@ -56,36 +109,24 @@ public class ResourceUtil extends ResourceUtils {
ClassPathResource cpr = new ClassPathResource(resourcePath);
File in = cpr.getFile();
for (File file : in.listFiles()) {
String name = file.getName();
if (name.matches(".+\\..+")) result.add(name);
result.add(FileUtil.getURL(file.getAbsolutePath()));
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* jar
*
* @return jar
*/
public static File getApplicationHome() {
ApplicationHome home = new ApplicationHome(ResourceUtil.class);
return home.getSource();
}
/**
* resource
*
*
* @return
* @return
*/
public static InputStream getResourceInput(String resource) {
public static InputStream getInput(String resourceLocation) {
try {
ClassPathResource classPathResource = new ClassPathResource(resource);
ClassPathResource classPathResource = new ClassPathResource(resourceLocation);
return classPathResource.getInputStream();
} catch (IOException e) {
return null;
@ -93,53 +134,54 @@ public class ResourceUtil extends ResourceUtils {
}
/**
* jar
*
*
* @return
* @return
*/
public static boolean copyResource2File(String resource, File file) {
InputStream resourceInput = getResourceInput(resource);
FileOutputStream fileOutputStream = null;
public static byte[] getBytes(String resourceLocation) {
InputStream is = null;
byte[] result = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
FileUtil.copy(resourceInput, fileOutputStream);
is = getInput(resourceLocation);
result = new byte[is.available()];
is.read(result);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (fileOutputStream != null) fileOutputStream.close();
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
return result;
}
/**
*
*
*
* @return
* @param resourceLocation
* @param file
* @return
*/
public static byte[] getResourceBytes(String resource) {
InputStream is = null;
byte[] result = null;
public static boolean copyToFile(String resourceLocation, File file) {
InputStream resourceInput = getInput(resourceLocation);
FileOutputStream fileOutputStream = null;
try {
is = getResourceInput(resource);
result = new byte[is.available()];
is.read(result);
fileOutputStream = FileUtil.openOutputStream(file);
FileUtil.copy(resourceInput, fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (is != null) is.close();
if (fileOutputStream != null) fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
return true;
}
}

@ -154,6 +154,37 @@ public class FileUtil {
}
}
public static FileOutputStream openOutputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
} else if (!file.canRead()) {
throw new IOException("File '" + file + "' cannot be read");
} else {
return new FileOutputStream(file);
}
} else {
throw new FileNotFoundException("File '" + file + "' does not exist");
}
}
/**
* @param fileLocation
* @return URL
*/
public static URL getURL(String fileLocation) {
try {
return new URL(fileLocation);
} catch (MalformedURLException var6) {
try {
return (new File(fileLocation)).toURI().toURL();
} catch (MalformedURLException var5) {
var5.printStackTrace();
}
}
return null;
}
public static byte[] toByteArray(InputStream input, long size) throws IOException {
if (size > 2147483647L) {
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);

@ -5,15 +5,16 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@ -25,27 +26,78 @@ import java.util.regex.Pattern;
*/
public class ResourceUtil extends ResourceUtils {
/**
*
* :E:\...\classes
* JAR: E:\...\***.jar
*
* @return jar
*/
public static File getApplicationHome() {
ApplicationHome home = new ApplicationHome(ResourceUtil.class);
return home.getSource();
}
/**
*
*
* @param resourcePath
* @return
*/
public static List<String> listFileName(String resourcePath) {
if (!resourcePath.endsWith("/")) {
resourcePath = resourcePath + "/";
}
List<String> result = new ArrayList<>();
List<URL> urls = ResourceUtil.listURL(resourcePath);
for (URL url : urls) {
File file1 = new File(url.getFile());
if (file1.isFile()) {
result.add(file1.getName());
} else if (!file1.isDirectory() && !url.getFile().endsWith("/")) {
result.add(file1.getName());
}
}
return result;
}
/**
* @param resourceLocation
* @return URL
* @throws FileNotFoundException
*/
public static URL getURL(String resourceLocation) throws FileNotFoundException {
if (!resourceLocation.startsWith("classpath:")) {
resourceLocation = "classpath:" + resourceLocation;
}
return ResourceUtils.getURL(resourceLocation);
}
/**
* : /modules/dir/
*
* @param resourcePath
* @return
*/
public static List<String> getResourceFiles(String resourcePath) {
List<String> result = new ArrayList<>();
public static List<URL> listURL(String resourcePath) {
if (!resourcePath.startsWith("/")) {
resourcePath = "/" + resourcePath;
}
if (!resourcePath.endsWith("/")) {
resourcePath = resourcePath + "/";
}
List<URL> result = new ArrayList<>();
File applicationHome = getApplicationHome();
if (applicationHome.getAbsolutePath().endsWith(".jar")) {
if (applicationHome.getName().endsWith(".jar")) {
try {
Pattern pattern = Pattern.compile(".*" + resourcePath + "(.+\\..+)");
Pattern pattern = Pattern.compile("BOOT-INF/classes" + resourcePath + "[^/]+/?$");
JarFile jarFile = new JarFile(applicationHome);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
String name = jarEntry.getName();
if (name.matches(".*" + resourcePath + "(.+\\..+)")) {
Matcher matcher = pattern.matcher(name);
if (matcher.find()) result.add(matcher.group(1));
if (pattern.matcher(name).find()) {
result.add(getURL(name));
}
}
} catch (IOException e) {
@ -56,36 +108,24 @@ public class ResourceUtil extends ResourceUtils {
ClassPathResource cpr = new ClassPathResource(resourcePath);
File in = cpr.getFile();
for (File file : in.listFiles()) {
String name = file.getName();
if (name.matches(".+\\..+")) result.add(name);
result.add(FileUtil.getURL(file.getAbsolutePath()));
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* jar
*
*
* @return jar
* @return
*/
public static File getApplicationHome() {
ApplicationHome home = new ApplicationHome(ResourceUtil.class);
return home.getSource();
}
/**
* resource
*
* @return
*/
public static InputStream getResourceInput(String resource) {
public static InputStream getInput(String resourceLocation) {
try {
ClassPathResource classPathResource = new ClassPathResource(resource);
ClassPathResource classPathResource = new ClassPathResource(resourceLocation);
return classPathResource.getInputStream();
} catch (IOException e) {
return null;
@ -93,53 +133,52 @@ public class ResourceUtil extends ResourceUtils {
}
/**
* jar
*
*
* @return
* @return
*/
public static boolean copyResource2File(String resource, File file) {
InputStream resourceInput = getResourceInput(resource);
FileOutputStream fileOutputStream = null;
public static byte[] getBytes(String resourceLocation) {
InputStream is = null;
byte[] result = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
FileUtil.copy(resourceInput, fileOutputStream);
is = getInput(resourceLocation);
result = new byte[is.available()];
is.read(result);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (fileOutputStream != null) fileOutputStream.close();
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
return result;
}
/**
*
*
*
* @return
* @param resourceLocation
* @param file
* @return
*/
public static byte[] getResourceBytes(String resource) {
InputStream is = null;
byte[] result = null;
public static boolean copyToFile(String resourceLocation, File file) {
InputStream resourceInput = getInput(resourceLocation);
FileOutputStream fileOutputStream = null;
try {
is = getResourceInput(resource);
result = new byte[is.available()];
is.read(result);
fileOutputStream = FileUtil.openOutputStream(file);
FileUtil.copy(resourceInput, fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (is != null) is.close();
if (fileOutputStream != null) fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
return true;
}
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.