java使用七牛云实现上传图片云存储-自己记录下

1. 先贴个本地上传到七牛云的

    public static void main(String[] args) {
        String accessKey = "dkoxFvCZPL9h8fNkKhPL9lUycB_ZA5oL-NXAdv";
        String secretKey = "ZJ8Z1UHaPL9lUyDnUkKhbE5BtqWnHjv-OM9RqUEx";
        String bucket = "takkimg";

        Configuration configuration = new Configuration(Zone.zone0());
        UploadManager manager = new UploadManager(configuration);

        String key = "weishenmemeiyouhouzhui.png";

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String localFilePath = "D:\\test.png";


        Response response = null;
        try {
            response = manager.put(localFilePath, key, upToken);
            DefaultPutRet set = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(set.key);
            System.out.println(set.hash);

        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
        }
    }

2. 上传图片云存储

1.后端

@PostMapping("upload1")
    @ResponseBody
    public String upload1(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("[文件类型] - [{}]", file.getContentType());
        log.info("[文件名称] - [{}]", file.getOriginalFilename());
        log.info("[文件大小] - [{}]", file.getSize());

        String accessKey = "dkoxFvCZ8IaNh8fNaPL9lUyDFkyHocB_ZA5oL-NXAdv";
        String secretKey = "ZJ8Z1UHaPL9lUyDZ8IaNh8fNE5BtqWnHjv-OM9RqUEx";
        String bucket = "tangz243mg";

        Configuration configuration = new Configuration(Zone.zone0());
        UploadManager manager = new UploadManager(configuration);

        String key = null;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        Response response = null;
        DefaultPutRet set = null;
        try {
            response = manager.put(byteArrayInputStream, key, upToken,null,null);
             set = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(set.key);
            System.out.println(set.hash);

        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
        }

        String imgUrl = "http://imgs.tangzekk.xyz/" + set.hash;
        return imgUrl;

    }

2. 前端


<template>
    <div>
    <el-upload
            class="upload-demo"
            drag
            action="http://localhost:8080/api/upload1"
            :on-success="handleAvatarSuccess"
            multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
    </el-upload>
        <div style="margin-top: 15px;">
            <el-input placeholder="目前未有图片地址" v-model="imgUrl" class="input-with-select">

                <el-button slot="append" icon="el-icon-search"
                           v-clipboard:copy="imgUrl"
                           v-clipboard:success="onCopy"
                           v-clipboard:error="onError"></el-button>
            </el-input>
        <img :src="imgUrl"/>
        </div>
</div>
</template>

<script>
    export default {
        name: "Upload",
        data() {
            return {
                imgUrl: ''
            }
        },
        methods:{
            handleAvatarSuccess(response,file,fileList) {
                console.log(response)
                this.imgUrl = response;

            },
            onCopy() {
                alert("复制到剪切板")
            },
            onError() {
                alert("复制失败")
            }

        }
    }
</script>

<style scoped>

</style>


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值