修复QQ发图片会资源泄露的问题
This commit is contained in:
parent
6a68e95f45
commit
6e4fc0c3d3
@ -20,6 +20,7 @@ import net.mamoe.mirai.utils.BotConfiguration;
|
|||||||
import net.mamoe.mirai.utils.ExternalResource;
|
import net.mamoe.mirai.utils.ExternalResource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -116,11 +117,19 @@ public class QQBotManager {
|
|||||||
|
|
||||||
private Image getImage(File file,Long qq) {
|
private Image getImage(File file,Long qq) {
|
||||||
if (bot != null) {
|
if (bot != null) {
|
||||||
|
ExternalResource resource=ExternalResource.create(file);
|
||||||
|
Image image;
|
||||||
if(QQNumberManager.getManager().isGroup(qq)) {
|
if(QQNumberManager.getManager().isGroup(qq)) {
|
||||||
return Objects.requireNonNull(bot.getGroup(qq)).uploadImage(ExternalResource.create(file));
|
image= Objects.requireNonNull(bot.getGroup(qq)).uploadImage(resource);
|
||||||
}else{
|
}else{
|
||||||
return Objects.requireNonNull(bot.getFriend(qq)).uploadImage(ExternalResource.create(file));
|
image= Objects.requireNonNull(bot.getFriend(qq)).uploadImage(resource);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
resource.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -38,4 +38,22 @@ public class GetSeTu extends Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
QQBotManager.getInstance();
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
String ret = HttpTools.get("https://api.ixiaowai.cn/api/api.php?return=json");
|
||||||
|
JSONObject json = JSONObject.parseObject(ret);
|
||||||
|
HttpTools.download(json.getString("imgurl"),
|
||||||
|
"setu.jpg", new DownloadInterface() {
|
||||||
|
@Override
|
||||||
|
public void onDownload(File file) {
|
||||||
|
super.onDownload(file);
|
||||||
|
QQBotManager.getInstance().sendMessage(file, 583819556L, "");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user