新增OSS上传

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

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);
}
}
}