|
|
@@ -1,5 +1,13 @@
|
|
|
+import cn.reghao.bnt.web.util.JarFileResources;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Path;
|
|
|
import java.time.DayOfWeek;
|
|
|
import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -20,6 +28,8 @@ public class DateTest {
|
|
|
String[] shengXiao = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
|
|
|
String[] jieQi = {"立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑",
|
|
|
"立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至", "小寒", "大寒"};
|
|
|
+
|
|
|
+ @Test
|
|
|
public void dateTest() {
|
|
|
LocalDateTime localDateTime = LocalDateTime.now();
|
|
|
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
|
|
|
@@ -38,4 +48,66 @@ public class DateTest {
|
|
|
Duration duration = Duration.between(localDateTime, localDateTime1);
|
|
|
System.out.println();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ // print input stream
|
|
|
+ private static void printInputStream(InputStream is) {
|
|
|
+ try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
|
|
|
+ BufferedReader reader = new BufferedReader(streamReader)) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ System.out.println(line);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getFileFromJar() throws IOException, URISyntaxException {
|
|
|
+ ClassLoader classLoader = this.getClass().getClassLoader();
|
|
|
+ ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
|
|
|
+ ClassLoader classLoader2 = DateTest.class.getClassLoader();
|
|
|
+
|
|
|
+ String resourcePath = "templates/index.html";
|
|
|
+ URL resource = classLoader.getResource(resourcePath);
|
|
|
+ File file = new File(resource.toURI());
|
|
|
+ InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
|
|
|
+
|
|
|
+ URL resource1 = classLoader1.getResource(resourcePath);
|
|
|
+ URL resource2 = classLoader2.getResource(resourcePath);
|
|
|
+
|
|
|
+ InputStream resourceAsStream = new ClassPathResource(resourcePath).getInputStream();
|
|
|
+
|
|
|
+
|
|
|
+ /*URL resource = Thread.currentThread().getContextClassLoader().getResource(avatarResource);
|
|
|
+ String avatarPath = resource.getPath();
|
|
|
+ System.out.println(avatarPath);
|
|
|
+ File file = new File(avatarPath);
|
|
|
+ byte[] imageBytes = Files.readAllBytes(file.toPath());*/
|
|
|
+
|
|
|
+ JarFileResources app = new JarFileResources();
|
|
|
+ // Sample 3 - read all files from a resources folder (jar version)
|
|
|
+ try {
|
|
|
+ // get paths from src/main/resources/json
|
|
|
+ List<Path> result = app.getPathsFromResourceJar("json");
|
|
|
+ for (Path path : result) {
|
|
|
+ System.out.println("Path : " + path);
|
|
|
+
|
|
|
+ String filePathInJar = path.toString();
|
|
|
+ // Windows will returns /json/file1.json, cut the first /
|
|
|
+ // the correct path should be json/file1.json
|
|
|
+ if (filePathInJar.startsWith("/")) {
|
|
|
+ filePathInJar = filePathInJar.substring(1, filePathInJar.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("filePathInJar : " + filePathInJar);
|
|
|
+ // read a file from resource folder
|
|
|
+ InputStream is = app.getFileFromResourceAsStream(filePathInJar);
|
|
|
+ printInputStream(is);
|
|
|
+ }
|
|
|
+ } catch (URISyntaxException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|