栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot文件上传在七牛云中

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot文件上传在七牛云中

首先需要注册一个七牛云账号:七牛云 - 国内领先的企业级云服务商 (qiniu.com)

这里我选择的地区是华东地区。

首先:pom.xml

        
            com.qiniu
            qiniu-java-sdk
            7.8.0
        

        
            com.google.code.gson
            gson
            2.8.6
        

upload.html:




    
    Title



service:

uploadservice

package com.yzx.boot.service;

import java.io.InputStream;

public interface UploadService {
    void upload(InputStream inputStream, String fileName);
}

uploadServiceimpl:

package com.yzx.boot.service;

import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;

import java.io.InputStream;
@Service
public class UploadServiceimpl implements UploadService{
    @Override
    public void upload(InputStream inputStream, String fileName) {

        try {
            //构造一个带指定 Region 对象的配置类
            //region0华东
            Configuration cfg = new Configuration(Region.region0());
            //...其他参数参考类注释
            UploadManager uploadManager = new UploadManager(cfg);
            //...生成上传凭证,然后准备上传
            //密钥
            String accessKey = "jArzWGjZeFXuOJhItw9KBMfCwXqm_r7cLpNsS1kQ";
            String secretKey = "yyXRdKg4YfV5NB--3wAN2eYi0wpg1XE14dN7tQQe";
            //空间名字
            String bucket = "yangp1";
            //默认不指定key的情况下,以文件内容的hash值作为文件名
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            Response response = uploadManager.put(inputStream, fileName, upToken,null,null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(ex.toString());
    }
    }
}

controller:

package com.yzx.boot.controller;


import com.yzx.boot.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploController {

    @Autowired
    private UploadService uploadService;

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam(name = "file") MultipartFile file) throws IOException {
        uploadService.upload(file.getInputStream(),file.getOriginalFilename());

        //如果不为空返回成功,为空返回失败
        if(uploadService!=null){
        return "success";
    }else{
            return "error";
        }


    }


}

 

 

最后建一个限制文件上传大小的类:

MyConfig
package com.yzx.boot.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@Configuration
public class MyConfig {

        //    设置上传文件限制
        @Bean
        public MultipartConfigElement multipartConfigElement() {
            MultipartConfigFactory config = new MultipartConfigFactory();

            //单个文件大小  KB、MB必须是大写
            config.setMaxFileSize(DataSize.parse("100MB"));
            //设置总文件大小
            config.setMaxRequestSize(DataSize.parse("1000MB"));
            return config.createMultipartConfig();

    }
}

最后成型照片:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678674.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号