新增OSS上传

新增处理apk打包穿山甲功能(工作需要)
This commit is contained in:
Yutousama 2020-06-03 00:01:11 +08:00
parent ec0e94feec
commit 6a0acdc779
7 changed files with 235 additions and 7 deletions

View File

@ -81,7 +81,11 @@
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,123 @@
package com.yutou.tools.home.nas;
import com.alibaba.fastjson.JSONObject;
import com.qy.Interfaces.SmaliApkToolsPath;
import com.qy.utils.CsjTools;
import com.yutou.tools.interfaces.DownloadInterface;
import com.yutou.tools.utils.OSSTools;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import redis.clients.jedis.Jedis;
import java.io.File;
import java.util.*;
public class BotTools {
protected static String download(String msg) {
String _json = new String(Base64.getDecoder().decode(msg));
JSONObject json = JSONObject.parseObject(_json);
String url = json.getString("url");
Tools.download(url, new DownloadInterface() {
@Override
public void onDownload(String file) {
super.onDownload(file);
if (file.endsWith(".apk")) {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
Jedis redisTools = RedisTools.getRedis();
redisTools.select(1);
redisTools.setex("downloadApk_" + uuid, RedisTools.TOKEN_TIMEOUT_DEFAULT, file);
redisTools.close();
RedisTools.set(1, "msg_" + System.currentTimeMillis(), "检测到上传apk请选择处理方式" +
"\n1)生成穿山甲资源包 (回复 #csj#穿山甲包名#穿山甲版本号#downloadApk_" + uuid + ")" +
"\n2)不处理 (360s后自动取消)" +
"\n$downloadApk_" + uuid);
}
}
@Override
public void onError(Exception e) {
super.onError(e);
}
});
return "ok";
}
private static List<JSONObject> list = new ArrayList<>();
private static boolean isRun = false;
static void csjAssets(String data, String packagename,String version) {
String path = RedisTools.get(data,1);
File file = new File(path);
JSONObject json = new JSONObject();
json.put("file", file);
json.put("version", version);
json.put("packagename", packagename);
list.add(json);
if (isRun) {
RedisTools.set(1, "msg_" + System.currentTimeMillis(), file + "\n已加入到队列");
return;
}
csj();
}
private static void csj() {
if (list.size() > 0) {
List<JSONObject> tmp = new ArrayList<>(list);
list.clear();
for (JSONObject jsonObject : tmp) {
isRun=true;
new CsjTools(jsonObject.getString("file")
, new File("csj" + File.separator + "demo.apk").getAbsolutePath()
, jsonObject.getString("packagename")
, jsonObject.getString("version")
, new SmaliApkToolsPath() {
@Override
public void smaliPath(String s) {
OSSTools.uploadFile(new File(s), new DownloadInterface() {
String path;
@Override
public void onDownload(String file) {
super.onDownload(file);
File apk=new File(s);
RedisTools.set(1,"msg_"+System.currentTimeMillis(),apk.getName()
+"已处理完成,下载地址" +
"\n"+file +
"\n请在十分钟内下载超时将自动删除");
path = file.replace("https://yutou-oss-test.oss-cn-hangzhou.aliyuncs.com/", "");
isRun=false;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10 * 60 * 1000);
OSSTools.delete(path);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
});
}
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
if (list.size() > 0) {
csj();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}

View File

@ -6,6 +6,8 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
@Controller
@RequestMapping("/localhome/bot")
public class QQBot {
@ -26,4 +28,26 @@ public class QQBot {
}
return json.toJSONString();
}
@RequestMapping("msg.do")
@ResponseBody
public String botMsg(String msg,String type){
System.out.println(type);
if(type.trim().equals("download")){
BotTools.download(msg);
}
return "OK~";
}
@RequestMapping("csj/assets.do")
@ResponseBody
public String csjAssets(String data,String packagename,String version){
new Thread(new Runnable() {
@Override
public void run() {
BotTools.csjAssets(data, packagename, version);
}
}).start();
return "ok";
}
}

View File

@ -0,0 +1,6 @@
package com.yutou.tools.interfaces;
public abstract class DownloadInterface {
public void onDownload(String file){};
public void onError(Exception e){};
}

View File

@ -0,0 +1,47 @@
package com.yutou.tools.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.yutou.tools.interfaces.DownloadInterface;
import java.io.*;
public class OSSTools {
// Endpoint以杭州为例其它Region请按实际情况填写
private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限风险很高强烈建议您创建并使用RAM账号进行API访问或日常运维请登录 https://ram.console.aliyun.com 创建RAM账号
private static String accessKeyId = "LTAI4G7J5pWbZeYdhFvGb4Dj";
private static String accessKeySecret = "GyXRDaOkER2qArGu4A2Tiym6ajuQPd";
private static String bucketName = "yutou-oss-test";
public static void uploadFile(File file, DownloadInterface downloadInterface) {
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径例如abc/efg/123.jpg
String objectName = "bot/download/"+System.currentTimeMillis()+"/"+file.getName();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传内容到指定的存储空间bucketName并保存为指定的文件名称objectName
try {
InputStream inputStream=new FileInputStream(file);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] bytes=new byte[4096];
int len;
while ((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(outputStream.toByteArray()));
outputStream.close();
inputStream.close();
downloadInterface.onDownload("https://yutou-oss-test.oss-cn-hangzhou.aliyuncs.com/"+objectName);
} catch (Exception e) {
e.printStackTrace();
downloadInterface.onError(e);
}
// 关闭OSSClient
ossClient.shutdown();
}
public static void delete(String path){
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.deleteObject(bucketName, path);
ossClient.shutdown();
}
}

View File

@ -10,7 +10,7 @@ import redis.clients.jedis.Jedis;
public class RedisTools {
private static String host;
private static int port;
private static int TOKEN_TIMEOUT_DEFAULT=360;
public static int TOKEN_TIMEOUT_DEFAULT=360;
private RedisTools() {
@ -21,7 +21,7 @@ public class RedisTools {
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
//host = properties.getProperty("redis.host");
//port = Integer.valueOf(properties.getProperty("redis.port"));
host="127.0.0.1";
host="192.168.31.88";
port=6379;
}
public static boolean set(int dbIndex,String key,String value){

View File

@ -1,6 +1,7 @@
package com.yutou.tools.utils;
import com.alibaba.fastjson.JSONArray;
import com.yutou.tools.interfaces.DownloadInterface;
import com.yutou.tools.nas.UpdateIp;
import org.springframework.web.multipart.MultipartFile;
@ -8,10 +9,7 @@ import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@ -231,4 +229,30 @@ public class Tools {
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
return path + fileName;
}
public static void download(String url, DownloadInterface downloadInterface) {
try {
HttpURLConnection connection= (HttpURLConnection) new URL(url).openConnection();
connection.disconnect();
new File("tmp").mkdirs();
File file=new File("tmp"+File.separator+url.trim().split("/")[url.trim().split("/").length-1]);
if(file.exists()){
file.delete();
}
FileOutputStream outputStream=new FileOutputStream(file);
InputStream inputStream=connection.getInputStream();
byte[] bytes=new byte[4096];
int len;
while ((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
outputStream.close();
inputStream.close();
downloadInterface.onDownload(file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
downloadInterface.onError(e);
}
}
}