You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.2 KiB

package ${basePackage}.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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
/**
* Resource
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class ResourceUtil extends ResourceUtils {
/**
* : /modules/dir/
*
* @param resourcePath
* @return
*/
public static List<String> getResourceFiles(String resourcePath) {
List<String> result = new ArrayList<>();
File applicationHome = getApplicationHome();
if (applicationHome.getAbsolutePath().endsWith(".jar")) {
try {
Pattern pattern = Pattern.compile(".*" + 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));
}
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
ClassPathResource cpr = new ClassPathResource(resourcePath);
File in = cpr.getFile();
for (File file : in.listFiles()) {
result.add(file.getName());
}
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
*/
public static InputStream getResourceInput(String resource) {
try {
ClassPathResource classPathResource = new ClassPathResource(resource);
return classPathResource.getInputStream();
} catch (IOException e) {
return null;
}
}
/**
* jar
*
* @return
*/
public static boolean copyResource2File(String resource, File file) {
InputStream resourceInput = getResourceInput(resource);
FileOutputStream fileOutputStream = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
FileUtil.copy(resourceInput, fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (fileOutputStream != null) fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
*
*
* @return
*/
public static byte[] getResourceBytes(String resource) {
InputStream is = null;
byte[] result = null;
try {
is = getResourceInput(resource);
result = new byte[is.available()];
is.read(result);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}

Powered by TurnKey Linux.