对象存储OSS

2021-01-20 15:51:26 浏览数 (1)

概述

一、定期启动腾讯云服务备份数据(自己服务备份)

FileController

代码语言:javascript复制
package com.qf.push.oss;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * zt
 * 2020/12/15
 * 11:11
 */
@RestController
@CrossOrigin
public class FileController {
    /**
     * 文件上传
     *
     */
    @RequestMapping("uploadFile")
    public String uploadFile(@RequestParam MultipartFile file) throws IOException {
        //得到源文件名
        String oldFileName = file.getOriginalFilename();
        //得到源文件后缀
        String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replace("-", "") suffix;
        String url = FileUtil.uploadFile(newFileName, file.getInputStream());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        return jsonObject.toJSONString();
    }

}

FileUtil

代码语言:javascript复制
package com.qf.push.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

/**
 * zt
 * 2020/12/15
 * 11:14
 */
public class FileUtil {

    public static OSS ossClient = new OSSClientBuilder().build(OSSConfig.endPoint, OSSConfig.AccessKeyId,OSSConfig.AccessKeySecret);
    /**
     * 上传文件
     */
    public static String uploadFile(String key, InputStream inputStream){
        //我们要是用ali的api,api是以sdk的形式调用的
        //放入阿里云的容器名、放入时候的key、放入时候的文件流
        ossClient.putObject(OSSConfig.bucketName, key, inputStream);
        return getURL(key);
    }
    /**
     * 获取url方法
     */
    public static String getURL(String fileName){
        String uploadUrl = "https://" OSSConfig.bucketName "." OSSConfig.endPoint "/" fileName;
        return uploadUrl;
    }
}

OSSConfig

代码语言:javascript复制
package com.qf.push.oss;

/**
 * zt
 * 2020/12/15
 * 11:03
 */
public class OSSConfig {
    //oss的地址
    public static final String endPoint = "oss-cn-beijing.aliyun.com";
    //accessKey
    public static final String AccessKeyId = "LAI4GGrCjDWWxMwNpZWDLfC";
    //accessKeySecret
    public static final String AccessKeySecret = "aRLe31h5H7r30SkcPS8P4mNw1Krlj1";
    //bucketName
    public static final String bucketName = "xuzhong";


}

client.html

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button onclick="onConnectionClick();">连接</button>
    <script>
        function onConnectionClick() {
            if("webSocket" in window){
                //浏览器支持webSocket
                let connectionURL = "ws://127.0.0.1:19999/ws?uid=1&token=ASDF";
                ws = new WebSocket(connectionURL);
                //当打开连接的时候
                ws.onopen=function () {
                    console.info("open");
                    sendHeartMsg();
                    setInterval("sendHeartMsg()",15000,null);
                };
                //当服务器发送给我消息的时候
                ws.onmessage=function (evt) {
                    let msg = evt.data;
                    console.info("onmessage"   msg);
                };
                //当服务器关闭连接的时候
                ws.onclose=function () {
                    console.info("onclose");
                };
            }else{
                alert("您的浏览器不支持webSocket!!!");
            }
        }

        //申请链接的时候,消息的类型是0
        //心跳1
        //其他2
        function sendHeartMsg() {
            ws.send("{"message":"","messageType":"1"}");
        }
    </script>
</body>
</html>

0 人点赞