From 3fcf1e72cc53272342e6c90d7d8d3d1961d36b7c Mon Sep 17 00:00:00 2001 From: yutou Date: Fri, 18 Jun 2021 18:30:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E7=BB=99OSS=E9=99=90?= =?UTF-8?q?=E9=80=9F=EF=BC=8C=E5=A5=BD=E5=83=8F=E5=A4=B1=E8=B4=A5=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/yutou/nas/NasApplication.java | 2 +- .../com/yutou/nas/utils/DepotManager.java | 50 +++++++++++-------- .../java/com/yutou/nas/utils/HttpTools.java | 1 + .../java/com/yutou/nas/utils/OSSManager.java | 7 ++- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/yutou/nas/NasApplication.java b/src/main/java/com/yutou/nas/NasApplication.java index aa55454..6d02f39 100644 --- a/src/main/java/com/yutou/nas/NasApplication.java +++ b/src/main/java/com/yutou/nas/NasApplication.java @@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import; @Import(BTDownloadManager.class) @SpringBootApplication public class NasApplication { - public static final String version="1.1.4"; + public static final String version="1.1.4.2"; public static void main(String[] args) { SpringApplication.run(NasApplication.class, args); AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); diff --git a/src/main/java/com/yutou/nas/utils/DepotManager.java b/src/main/java/com/yutou/nas/utils/DepotManager.java index d331590..1c7af40 100644 --- a/src/main/java/com/yutou/nas/utils/DepotManager.java +++ b/src/main/java/com/yutou/nas/utils/DepotManager.java @@ -10,37 +10,45 @@ import java.io.FileNotFoundException; import java.util.Objects; public class DepotManager { - private DepotManager(){ + private DepotManager() { } - public static void scan(){ - String str=HttpTools.get("http://tools.yutou233.cn/nas/depot/list.do"); - if(!StringUtils.isEmpty(str)){ - JSONObject json=JSONObject.parseObject(str); - if(json.getInteger("code")==1){ - JSONArray array=json.getJSONArray("data"); - for (Object o : array) { - JSONObject item=JSONObject.parseObject(o.toString()); - scanFile(new File(item.getString("path")), new DownloadInterface() { - @Override - public void onDownload(File file) { - super.onDownload(file); - OSSManager.upload("oss-name-"+item.getString("type"),item.getString("path"),file); + + public static void scan() { + new Thread(new Runnable() { + @Override + public void run() { + String str = HttpTools.get("http://tools.yutou233.cn/nas/depot/list.do?token=" + HttpTools.serverKey); + if (!StringUtils.isEmpty(str)) { + JSONObject json = JSONObject.parseObject(str); + if (json.getInteger("code") == 1) { + JSONArray array = json.getJSONArray("data"); + for (Object o : array) { + JSONObject item = JSONObject.parseObject(o.toString()); + scanFile(new File(item.getString("path")), new DownloadInterface() { + @Override + public void onDownload(File file) { + super.onDownload(file); + OSSManager.upload("oss-name-" + item.getString("type"), item.getString("path"), file); + } + }); } - }); + } } } - } + }).start(); + } - private static void scanFile(File path, DownloadInterface interfaces){ - if(!path.exists()){ + + private static void scanFile(File path, DownloadInterface interfaces) { + if (!path.exists()) { interfaces.onError(new FileNotFoundException("没有该文件")); return; } for (File file : Objects.requireNonNull(path.listFiles())) { - if(file.isDirectory()){ - scanFile(file,interfaces); - }else{ + if (file.isDirectory()) { + scanFile(file, interfaces); + } else { interfaces.onDownload(file); } } diff --git a/src/main/java/com/yutou/nas/utils/HttpTools.java b/src/main/java/com/yutou/nas/utils/HttpTools.java index 7ec475f..456acb6 100644 --- a/src/main/java/com/yutou/nas/utils/HttpTools.java +++ b/src/main/java/com/yutou/nas/utils/HttpTools.java @@ -12,6 +12,7 @@ import java.net.URLEncoder; import java.util.Set; public class HttpTools { + public static final String serverKey="zIrsh9TUZP2lfRW753PannG49E7VJvor"; public static String get(String url) { try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); diff --git a/src/main/java/com/yutou/nas/utils/OSSManager.java b/src/main/java/com/yutou/nas/utils/OSSManager.java index 9cde526..08ba3fe 100644 --- a/src/main/java/com/yutou/nas/utils/OSSManager.java +++ b/src/main/java/com/yutou/nas/utils/OSSManager.java @@ -4,6 +4,7 @@ import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.ObjectMetadata; +import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.StorageClass; import com.yutou.nas.interfaces.DownloadInterface; @@ -61,7 +62,11 @@ public class OSSManager { return; } - client.putObject(ConfigTools.load(ConfigTools.CONFIG,type,String.class), uploadPath, file); + //client.putObject(ConfigTools.load(ConfigTools.CONFIG,type,String.class), uploadPath, file); + int limitSpeed = 100 * 1024 * 8; + PutObjectRequest request=new PutObjectRequest(ConfigTools.load(ConfigTools.CONFIG,type,String.class),uploadPath,file); + request.setTrafficLimit(limitSpeed); + client.putObject(request); System.out.println(file.getName()+"已上传"); } closeClient(client);