mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
6-27更新 增加对接七牛云存储
This commit is contained in:
@@ -30,6 +30,11 @@ public class TbedApplication {
|
||||
/// 总上传数据大小
|
||||
factory.setMaxRequestSize("102400KB");
|
||||
return factory.createMultipartConfig();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -85,7 +85,19 @@ public class AdminController {
|
||||
// }
|
||||
return "admin/table";
|
||||
}
|
||||
|
||||
// 进入后台页面
|
||||
@RequestMapping(value = "/tosurvey")
|
||||
public String admin2(HttpSession session, Model model) {
|
||||
User u = (User) session.getAttribute("user");
|
||||
String sysetmname = System.getProperty("os.name");
|
||||
String isarch = System.getProperty("os.arch");
|
||||
String jdk = System.getProperty("java.version");
|
||||
model.addAttribute("username", u.getUsername());
|
||||
model.addAttribute("sysetmname",sysetmname);
|
||||
model.addAttribute("isarch", isarch);
|
||||
model.addAttribute("jdk", jdk);
|
||||
return "admin/survey";
|
||||
}
|
||||
//获取本站概况
|
||||
@RequestMapping(value = "/getwebconfig")
|
||||
@ResponseBody
|
||||
@@ -201,7 +213,6 @@ public class AdminController {
|
||||
Boolean b = StringUtils.doNull(key);//判断对象是否有空值
|
||||
if(b){
|
||||
ImgServiceImpl de = new ImgServiceImpl();
|
||||
|
||||
if (key.getStorageType() == 1) {
|
||||
de.delect(key, images.getImgname());
|
||||
} else if (key.getStorageType() == 2) {
|
||||
@@ -210,6 +221,7 @@ public class AdminController {
|
||||
de.delectUSS(key, images.getImgname());
|
||||
} else if (key.getStorageType() == 4) {
|
||||
//初始化七牛云
|
||||
de.delectKODO(key, images.getImgname());
|
||||
} else {
|
||||
System.err.println("未获取到对象存储参数,删除失败。");
|
||||
}
|
||||
@@ -250,6 +262,7 @@ public class AdminController {
|
||||
de.delectUSS(key, images.getImgname());
|
||||
} else if (key.getStorageType() == 4) {
|
||||
//初始化七牛云
|
||||
de.delectKODO(key, images.getImgname());
|
||||
} else {
|
||||
System.err.println("未获取到对象存储参数,删除失败。");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.hellohao.pojo.EmailConfig;
|
||||
import cn.hellohao.pojo.Keys;
|
||||
import cn.hellohao.pojo.UploadConfig;
|
||||
import cn.hellohao.service.*;
|
||||
import cn.hellohao.utils.Print;
|
||||
import cn.hellohao.utils.StringUtils;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -43,15 +44,24 @@ public class AdminRootController {
|
||||
Config config = configService.getSourceype();//查询当前系统使用的存储源类型。
|
||||
Keys key = keysService.selectKeys(config.getSourcekey());
|
||||
Boolean b = StringUtils.doNull(key);//判断对象是否有空值
|
||||
Integer StorageType = 0;
|
||||
if(b){
|
||||
//key信息
|
||||
model.addAttribute("AccessKey", key.getAccessKey());
|
||||
model.addAttribute("AccessSecret", key.getAccessSecret());
|
||||
|
||||
model.addAttribute("Endpoint", key.getEndpoint());
|
||||
model.addAttribute("Bucketname", key.getBucketname());
|
||||
model.addAttribute("RequestAddress", key.getRequestAddress());
|
||||
model.addAttribute("StorageType", config.getSourcekey());
|
||||
}
|
||||
//如果是4就是七牛
|
||||
if(config.getSourcekey()==4){
|
||||
model.addAttribute("Endpoint2", key.getEndpoint());
|
||||
}else{
|
||||
model.addAttribute("Endpoint2", 0);
|
||||
}
|
||||
|
||||
return "admin/storageconfig";
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.*;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import cn.hellohao.pojo.*;
|
||||
import cn.hellohao.service.*;
|
||||
import cn.hellohao.service.impl.KODOImageupload;
|
||||
import cn.hellohao.service.impl.OSSImageupload;
|
||||
import cn.hellohao.service.impl.USSImageupload;
|
||||
import cn.hellohao.utils.Sentence;
|
||||
@@ -37,7 +38,8 @@ public class UpdateImgController {
|
||||
private UploadConfigService uploadConfigService;
|
||||
@Autowired
|
||||
private USSImageupload ussImageupload;
|
||||
|
||||
@Autowired
|
||||
private KODOImageupload kodoImageupload;
|
||||
|
||||
@RequestMapping({"/", "/index"})
|
||||
public String indexImg(Model model, HttpSession httpSession) {
|
||||
@@ -69,6 +71,8 @@ public class UpdateImgController {
|
||||
//初始化又拍云
|
||||
}else if(key.getStorageType()==4){
|
||||
//初始化七牛云
|
||||
KODOImageupload.Initialize(key);
|
||||
System.out.println("KODO初始化成功。");
|
||||
}else{
|
||||
System.err.println("未获取到对象存储参数,初始化失败。");
|
||||
}
|
||||
@@ -140,6 +144,7 @@ public class UpdateImgController {
|
||||
//初始化腾讯云
|
||||
}else if(key.getStorageType()==4){
|
||||
//初始化七牛云
|
||||
m = kodoImageupload.ImageuploadKODO(map, username);
|
||||
}else{
|
||||
System.err.println("未获取到对象存储参数,上传失败。");
|
||||
}
|
||||
|
||||
@@ -20,10 +20,7 @@ public class InterceptorConfigTwo implements HandlerInterceptor {
|
||||
*/
|
||||
//这个方法是在访问接口之前执行的,我们只需要在这里写验证登陆状态的业务逻辑,就可以在用户调用指定接口之前验证登陆状态了
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
//设置不缓存
|
||||
// response.setHeader("Cache-Control","no-cache");
|
||||
// response.setHeader("Pragma","no-cache");
|
||||
// response.setDateHeader("Expires",0);
|
||||
|
||||
//每一个项目对于登陆的实现逻辑都有所区别,我这里使用最简单的Session提取User来验证登陆。
|
||||
HttpSession session = request.getSession();
|
||||
//这里的User是登陆时放入session的
|
||||
|
||||
@@ -6,6 +6,11 @@ import java.util.List;
|
||||
|
||||
import com.UpYun;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.upyun.UpException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -105,7 +110,25 @@ public class ImgServiceImpl implements ImgService {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//删除KODO对象存储的图片文件
|
||||
public void delectKODO(Keys key, String fileName) {
|
||||
Configuration cfg;
|
||||
//构造一个带指定Zone对象的配置类
|
||||
if(key.getEndpoint().equals("1")){cfg = new Configuration(Zone.zone0());}
|
||||
else if(key.getEndpoint().equals("2")){cfg = new Configuration(Zone.zone1());}
|
||||
else if(key.getEndpoint().equals("3")){cfg = new Configuration(Zone.zone2());}
|
||||
else if(key.getEndpoint().equals("4")){cfg = new Configuration(Zone.zoneNa0());}
|
||||
else{cfg = new Configuration(Zone.zoneAs0());}
|
||||
Auth auth = Auth.create(key.getAccessKey(), key.getAccessSecret());
|
||||
BucketManager bucketManager = new BucketManager(auth, cfg);
|
||||
try {
|
||||
bucketManager.delete(key.getBucketname(), fileName);
|
||||
} catch (QiniuException ex) {
|
||||
//如果遇到异常,说明删除失败
|
||||
System.err.println(ex.code());
|
||||
System.err.println(ex.response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer counts(Integer userid) {
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package cn.hellohao.service.impl;
|
||||
|
||||
import cn.hellohao.pojo.Keys;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.google.gson.Gson;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.DefaultPutRet;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class KODOImageupload {
|
||||
static String upToken;
|
||||
static UploadManager uploadManager;
|
||||
static Keys key;
|
||||
|
||||
public Map<String, Integer> ImageuploadKODO(Map<String, MultipartFile> fileMap, String username) throws Exception {
|
||||
// 要上传文件的路径
|
||||
File file = null;
|
||||
Map<String, Integer> ImgUrl = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
|
||||
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
|
||||
String times = format1.format(new Date());
|
||||
file = changeFile(entry.getValue());
|
||||
// 上传文件流。
|
||||
System.out.println("待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
|
||||
//ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),file,meta);
|
||||
try {
|
||||
Response response = uploadManager.put(file,username + "/" + uuid+times + "." + entry.getKey(),upToken);
|
||||
//解析上传成功的结果
|
||||
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||
ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()));
|
||||
//System.out.println(putRet.hash);
|
||||
} catch (QiniuException ex) {
|
||||
Response r = ex.response;
|
||||
System.err.println(r.toString());
|
||||
try {
|
||||
System.err.println(r.bodyString());
|
||||
} catch (QiniuException ex2) {
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
}
|
||||
|
||||
// 转换文件方法
|
||||
private File changeFile(MultipartFile multipartFile) throws Exception {
|
||||
// 获取文件名
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
// 获取文件后缀
|
||||
String prefix = fileName.substring(fileName.lastIndexOf("."));
|
||||
// todo 修改临时文件文件名
|
||||
File file = File.createTempFile(fileName, prefix);
|
||||
// MultipartFile to File
|
||||
multipartFile.transferTo(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
//初始化
|
||||
public static void Initialize(Keys k) {
|
||||
// 初始化
|
||||
// 创建KODOClient实例。
|
||||
Configuration cfg;
|
||||
//构造一个带指定Zone对象的配置类
|
||||
if(k.getEndpoint().equals("1")){cfg = new Configuration(Zone.zone0());}
|
||||
else if(k.getEndpoint().equals("2")){cfg = new Configuration(Zone.zone1());}
|
||||
else if(k.getEndpoint().equals("3")){cfg = new Configuration(Zone.zone2());}
|
||||
else if(k.getEndpoint().equals("4")){cfg = new Configuration(Zone.zoneNa0());}
|
||||
else{cfg = new Configuration(Zone.zoneAs0());}
|
||||
uploadManager = new UploadManager(cfg);
|
||||
Auth auth = Auth.create(k.getAccessKey(), k.getAccessSecret());
|
||||
upToken = auth.uploadToken(k.getBucketname());
|
||||
key = k;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.hellohao.test;
|
||||
|
||||
|
||||
import org.hyperic.sigar.Mem;
|
||||
import org.hyperic.sigar.Sigar;
|
||||
|
||||
/**
|
||||
* @author
|
||||
*
|
||||
*/
|
||||
|
||||
public class BaseController {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Sigar sigar = new Sigar();
|
||||
Mem mem = sigar.getMem();
|
||||
System.out.println( mem.getUsed());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package cn.hellohao.test;
|
||||
|
||||
import org.apache.http.impl.io.IdentityInputStream;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class DownloadPicFromURL {
|
||||
public static void main(String[] args) {
|
||||
String url = "https://hellohao.oss-cn-beijing.aliyuncs.com/Hellohao/0efe60609092414.png";
|
||||
String path="D:\\apps\\pic.jpg";
|
||||
downloadPicture(url,path);
|
||||
}
|
||||
//链接url下载图片
|
||||
private static void downloadPicture(String urlList,String path) {
|
||||
URL url = null;
|
||||
try {
|
||||
url = new URL(urlList);
|
||||
DataInputStream dataInputStream = new DataInputStream(url.openStream());
|
||||
|
||||
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
|
||||
while ((length = dataInputStream.read(buffer)) > 0) {
|
||||
output.write(buffer, 0, length);
|
||||
}
|
||||
fileOutputStream.write(output.toByteArray());
|
||||
dataInputStream.close();
|
||||
fileOutputStream.close();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cn.hellohao.utils;
|
||||
|
||||
public class Print {
|
||||
// 正常normal
|
||||
// 警告 warning
|
||||
|
||||
/* 样式:
|
||||
|
||||
0 空样式
|
||||
|
||||
1 粗体
|
||||
|
||||
4 下划线
|
||||
|
||||
7 反色
|
||||
|
||||
颜色1:
|
||||
|
||||
30 白色
|
||||
|
||||
31 红色
|
||||
|
||||
32 绿色
|
||||
|
||||
33 黄色
|
||||
|
||||
34 蓝色
|
||||
|
||||
35 紫色
|
||||
|
||||
36 浅蓝
|
||||
|
||||
37 灰色
|
||||
|
||||
背景颜色:
|
||||
|
||||
40-47 和颜色顺序相同
|
||||
|
||||
颜色2:
|
||||
|
||||
90-97 比颜色1更鲜艳一些,我也不太清楚为什么又两种
|
||||
* */
|
||||
public static void Normal(Object object){
|
||||
System.out.println("\033[32;1m" + object + "\033[0m");
|
||||
}
|
||||
public static void warning(Object object){
|
||||
System.out.println("\033[31;1m" + object + "\033[0m");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.hellohao.utils;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
//@Configuration
|
||||
public class WebConfigConfigurer extends WebMvcConfigurationSupport {
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
Print.Normal("修改响应头开始");
|
||||
registry.addResourceHandler("/**")
|
||||
.addResourceLocations("classpath:/static/")
|
||||
//.setCacheControl(CacheControl.noCache());
|
||||
.setCacheControl(
|
||||
CacheControl.maxAge(1, TimeUnit.SECONDS)
|
||||
.cachePublic());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user