新增BT下载器

This commit is contained in:
yutou 2021-04-07 18:31:35 +08:00
parent 5d5a4eef97
commit 778f37ed4c
11 changed files with 276 additions and 12 deletions

View File

@ -33,9 +33,9 @@ public class QQBot {
}else{
Tools.download(imgUrl, new DownloadInterface() {
@Override
public void onDownload(String file) {
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(new File(file),msg);
QQBotManager.getInstance().sendMessage(file,msg);
}
@Override

View File

@ -1,12 +1,15 @@
package com.yutou.nas;
import com.yutou.nas.Datas.AppData;
import com.yutou.nas.utils.BTDownloadManager;
import com.yutou.nas.utils.ConfigTools;
import com.yutou.nas.utils.QQBotManager;
import com.yutou.nas.utils.RedisTools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@Import(BTDownloadManager.class)
@SpringBootApplication
public class NasApplication {
public static final String version="1.0.0";

View File

@ -0,0 +1,9 @@
package com.yutou.nas.Services;
import com.yutou.nas.mybatis.model.BangumiItem;
import java.util.List;
public interface IBangumiService {
List<BangumiItem> getAllBangumi();
}

View File

@ -0,0 +1,20 @@
package com.yutou.nas.Services.impl;
import com.yutou.nas.Services.IBangumiService;
import com.yutou.nas.mybatis.dao.BangumiItemDao;
import com.yutou.nas.mybatis.model.BangumiItem;
import com.yutou.nas.mybatis.model.BangumiItemExample;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("BangumiService")
public class BangumiServiceImpl implements IBangumiService {
@Resource
BangumiItemDao itemDao;
@Override
public List<BangumiItem> getAllBangumi() {
return itemDao.selectByExample(new BangumiItemExample());
}
}

View File

@ -1,6 +1,9 @@
package com.yutou.nas.interfaces;
import java.io.File;
public abstract class DownloadInterface {
public void onDownload(String file){};
public void onDownloading(double soFarBytes, double totalBytes){};
public void onDownload(File file){};
public void onError(Exception e){};
}

View File

@ -0,0 +1,10 @@
package com.yutou.nas.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AppTools {
public static String getToDayNowTimeToString(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,10 @@
package com.yutou.nas.utils;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas.interfaces.DownloadInterface;
import com.yutou.nas.utils.Interfaces.NetworkInterface;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@ -129,4 +127,60 @@ public class HttpTools {
System.out.println(url);
//String str=get(url);
}
private static String donwloadPath="tmp"+File.separator;
public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
new Thread(new Runnable() {
@Override
public void run() {
File jar = null;
try {
File savePath = new File(donwloadPath);
if (!savePath.exists()) {
savePath.mkdirs();
}
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
InputStream inputStream = connection.getInputStream();
jar = new File(donwloadPath + saveName + "_tmp.tmp");
jar.createNewFile();
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(jar);
byte[] bytes = new byte[1024];
double size=connection.getContentLength();
double downSize=0;
int len;
while ((len = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
downSize+=len;
if(downloadInterface!=null){
downloadInterface.onDownloading(downSize,size);
}
}
outputStream.close();
inputStream.close();
File oldJar = new File(donwloadPath + saveName);
if (oldJar.exists()) {
oldJar.delete();
}
jar.renameTo(oldJar);
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
if (downloadInterface != null) {
downloadInterface.onDownload(oldJar);
}
} catch (Exception e) {
e.printStackTrace();
if (jar != null) {
jar.delete();
}
downloadInterface.onError(e);
}
}
}).start();
}
}

View File

@ -0,0 +1,14 @@
package com.yutou.nas.utils;
public class Log {
public static void i(String tag,Object log){
i('['+tag+']'+log);
}
public static void i(Object log){
System.out.printf("[%s]%s%n",
AppTools.getToDayNowTimeToString(),
log
);
}
}

View File

@ -304,9 +304,9 @@ public class QQBotManager {
for (String img : imgs) {
Tools.download(img, new DownloadInterface() {
@Override
public void onDownload(String file) {
public void onDownload(File file) {
super.onDownload(file);
files.add(new File(file));
files.add(file);
send(imgs.size(), text);
}

View File

@ -112,9 +112,9 @@ public class Tools {
if (QQBotManager.getInstance().isLogin()) {
download(img, new DownloadInterface() {
@Override
public void onDownload(String file) {
public void onDownload(File file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(new File(file), title + "\n" + finalMsg);
QQBotManager.getInstance().sendMessage(file, title + "\n" + finalMsg);
}
@Override
@ -283,7 +283,7 @@ public class Tools {
}
outputStream.close();
inputStream.close();
downloadInterface.onDownload(file.getAbsolutePath());
downloadInterface.onDownload(file);
} catch (IOException e) {
e.printStackTrace();
downloadInterface.onError(e);