(资料图)
Error: java:常量字符串过长网上还有一个说法,说是编译器问题,修改 idea 工具的编译为 eclipse 即可。
但是结果我仍然不满意,所以我决定把他放在文件中,然后需要的时候读取出来即可。
所以,我就把字符串放到了 resources 的某个 txt 文件下,然后再从文件中读取出来
一开始使用的是 hutool util 工具类去读取,但是不成功。
String filePath = "payload.txt";String contentString = FileUtil.readUtf8String(Thread.currentThread().getContextClassLoader().getResource("").getPath() + filePath);// 先转为流InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);// 再把流转为 StringString content = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n"));public final class ClassPathResourceReader { /** * path:文件路径 * @since JDK 1.8 */ private final String path; /** * content:文件内容 * @since JDK 1.6 */ private String content; public ClassPathResourceReader(String path) { this.path = path; } public String getContent() { if (content == null) { try { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path); if (inputStream!=null) { content = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n")); }else { throw new RuntimeException("创建 lookLike-app 受众出现异常:File not exist"); } } catch (Exception e) { throw new RuntimeException(e); } } return content; }}这样相当于做了个本地缓存,就不用每次都去读取文件了,性能嘎嘎快。
String content = new ClassPathResourceReader("payload.txt").getContent(); Copyright 2015-2022 北方评测网版权所有 备案号:京ICP备2021034106号-50 联系邮箱: 55 16 53 8@qq.com