尝试给OSS限速,好像失败了

This commit is contained in:
yutou 2021-06-18 18:30:08 +08:00
parent 8491879c2a
commit 3fcf1e72cc
4 changed files with 37 additions and 23 deletions

View File

@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import;
@Import(BTDownloadManager.class) @Import(BTDownloadManager.class)
@SpringBootApplication @SpringBootApplication
public class NasApplication { 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) { public static void main(String[] args) {
SpringApplication.run(NasApplication.class, args); SpringApplication.run(NasApplication.class, args);
AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir"); AppData.defaultMusicPath = (String) ConfigTools.load(ConfigTools.CONFIG, "musicDir");

View File

@ -10,37 +10,45 @@ import java.io.FileNotFoundException;
import java.util.Objects; import java.util.Objects;
public class DepotManager { public class DepotManager {
private DepotManager(){ private DepotManager() {
} }
public static void scan(){
String str=HttpTools.get("http://tools.yutou233.cn/nas/depot/list.do"); public static void scan() {
if(!StringUtils.isEmpty(str)){ new Thread(new Runnable() {
JSONObject json=JSONObject.parseObject(str); @Override
if(json.getInteger("code")==1){ public void run() {
JSONArray array=json.getJSONArray("data"); String str = HttpTools.get("http://tools.yutou233.cn/nas/depot/list.do?token=" + HttpTools.serverKey);
for (Object o : array) { if (!StringUtils.isEmpty(str)) {
JSONObject item=JSONObject.parseObject(o.toString()); JSONObject json = JSONObject.parseObject(str);
scanFile(new File(item.getString("path")), new DownloadInterface() { if (json.getInteger("code") == 1) {
@Override JSONArray array = json.getJSONArray("data");
public void onDownload(File file) { for (Object o : array) {
super.onDownload(file); JSONObject item = JSONObject.parseObject(o.toString());
OSSManager.upload("oss-name-"+item.getString("type"),item.getString("path"),file); 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("没有该文件")); interfaces.onError(new FileNotFoundException("没有该文件"));
return; return;
} }
for (File file : Objects.requireNonNull(path.listFiles())) { for (File file : Objects.requireNonNull(path.listFiles())) {
if(file.isDirectory()){ if (file.isDirectory()) {
scanFile(file,interfaces); scanFile(file, interfaces);
}else{ } else {
interfaces.onDownload(file); interfaces.onDownload(file);
} }
} }

View File

@ -12,6 +12,7 @@ import java.net.URLEncoder;
import java.util.Set; import java.util.Set;
public class HttpTools { public class HttpTools {
public static final String serverKey="zIrsh9TUZP2lfRW753PannG49E7VJvor";
public static String get(String url) { public static String get(String url) {
try { try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

View File

@ -4,6 +4,7 @@ import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.StorageClass; import com.aliyun.oss.model.StorageClass;
import com.yutou.nas.interfaces.DownloadInterface; import com.yutou.nas.interfaces.DownloadInterface;
@ -61,7 +62,11 @@ public class OSSManager {
return; 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()+"已上传"); System.out.println(file.getName()+"已上传");
} }
closeClient(client); closeClient(client);