Open Orisland opened 1 month ago
能否检查出运行时获取到的文件或者格式?我这边测试结果貌似都是jpg、png之类的图片格式
能否检查出运行时获取到的文件或者格式?我这边测试结果貌似都是jpg、png之类的图片格式
除了在源码上加try catch来获取异常外,我暂时不知道还有什么办法可以得到错误时的格式。 如果你愿意,可以给源码增加try catch之后重新编译一份插件,我会继续测试并在这里给出遇到错误时的log信息。
public static void chaiq(Contact subject, User user) {
try {
URL url = new URL("https://api.lolimi.cn/API/chaiq/c.php");
InputStream stream = url.openStream();
// 文件类型测试
File directory= new File("./aronaBotDebug");
if (!directory.exists()) {
if (directory.mkdir()){
Log.debug("debug文件夹创建成功");
} else {
Log.error("debug文件夹创建失败");
}
}
long time = System.currentTimeMillis();
File tempFile = File.createTempFile("tempFile", null);
try (
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile))) {
outputStream.write(stream.readAllBytes());
}
URLConnection connection = tempFile.toURI().toURL().openConnection();
String mimeType = connection.getContentType();
Log.debug("文件类型: " + mimeType);
String[] suf = mimeType.split("/");
String suffix = suf[suf.length - 1];
File finalFile = new File("./aronaBotDebug/" + time + "file." + suffix);
FileUtil.copy(tempFile, finalFile, true);
ExternalResource resource = ExternalResource.create(stream);
Image image = subject.uploadImage(resource);
subject.sendMessage(new At(user.getId()).plus(image));
stream.close();
resource.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
来获取文件的格式与保存获取到的文件
试着将
改为
public static void chaiq(Contact subject, User user) { try { URL url = new URL("https://api.lolimi.cn/API/chaiq/c.php"); InputStream stream = url.openStream(); // 文件类型测试 File dictionary = new File("./aronaBotDebug"); if (!dictionary.exists()) { if (dictionary.mkdir()){ Log.debug("debug文件夹创建成功"); } else { Log.error("debug文件夹创建失败"); } } long time = System.currentTimeMillis(); File tempFile = File.createTempFile("tempFile", null); try ( BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile))) { outputStream.write(stream.readAllBytes()); } URLConnection connection = tempFile.toURI().toURL().openConnection(); String mimeType = connection.getContentType(); Log.debug("文件类型: " + mimeType); String[] suf = mimeType.split("/"); String suffix = suf[suf.length - 1]; File finalFile = new File("./aronaBotDebug/" + time + "file." + suffix); FileUtil.copy(tempFile, finalFile, true); ExternalResource resource = ExternalResource.create(stream); Image image = subject.uploadImage(resource); subject.sendMessage(new At(user.getId()).plus(image)); stream.close(); resource.close(); } catch (Exception e) { throw new RuntimeException(e); } }
来获取文件的格式与保存获取到的文件
那这边我会重新编译一份插件后测试该问题,有最新的问题报告我会同步在这个issue。
https://pste.ch/epuxewulok.pl 问题出现频繁且错误完全一致,是否是因为api调用的时候有限额问题? 我在进行测试的时候似乎没有发现问题,不确定问题来源。 https://api.lolimi.cn/API/chaiq/c.php