|  | @@ -0,0 +1,103 @@
 | 
	
		
			
				|  |  | +package com.hwrj.cloud.portal.util;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.google.gson.Gson;
 | 
	
		
			
				|  |  | +import com.hwrj.cloud.common.exception.GlobalException;
 | 
	
		
			
				|  |  | +import com.qiniu.common.QiniuException;
 | 
	
		
			
				|  |  | +import com.qiniu.common.Zone;
 | 
	
		
			
				|  |  | +import com.qiniu.http.Response;
 | 
	
		
			
				|  |  | +import com.qiniu.storage.Configuration;
 | 
	
		
			
				|  |  | +import com.qiniu.storage.UploadManager;
 | 
	
		
			
				|  |  | +import com.qiniu.storage.model.DefaultPutRet;
 | 
	
		
			
				|  |  | +import com.qiniu.util.Auth;
 | 
	
		
			
				|  |  | +import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | +import org.springframework.web.bind.annotation.RequestParam;
 | 
	
		
			
				|  |  | +import org.springframework.web.multipart.MultipartFile;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Slf4j
 | 
	
		
			
				|  |  | +@Component
 | 
	
		
			
				|  |  | +public class UploadFileUtil {
 | 
	
		
			
				|  |  | +    @Value("${qiniu.accessKey}")
 | 
	
		
			
				|  |  | +    private String accessKey;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Value("${qiniu.secretKey}")
 | 
	
		
			
				|  |  | +    private String secretKey;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Value("${qiniu.bucket}")
 | 
	
		
			
				|  |  | +    private String bucket;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Value("${qiniu.path}")
 | 
	
		
			
				|  |  | +    private String url;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public String uploadQiNiuFiles(@RequestParam("file") MultipartFile file, String path){
 | 
	
		
			
				|  |  | +        String contentType = file.getContentType();
 | 
	
		
			
				|  |  | +        String[] split = null;
 | 
	
		
			
				|  |  | +        try{
 | 
	
		
			
				|  |  | +            split = contentType.split("/");
 | 
	
		
			
				|  |  | +        }catch (Exception e){
 | 
	
		
			
				|  |  | +            throw new GlobalException("获取文件后缀出错!=======>"+split);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        path = path+"."+split[split.length-1];
 | 
	
		
			
				|  |  | +        //构建一个带指定对象的配置类
 | 
	
		
			
				|  |  | +        Configuration cof = new Configuration(Zone.autoZone());
 | 
	
		
			
				|  |  | +        UploadManager uploadManager = new UploadManager(cof);
 | 
	
		
			
				|  |  | +        String uploadToken = getUploadToken();
 | 
	
		
			
				|  |  | +        DefaultPutRet defaultPutRet = null;
 | 
	
		
			
				|  |  | +        try{
 | 
	
		
			
				|  |  | +            Response response = uploadManager.put(file.getInputStream(), path, uploadToken, null, null);
 | 
	
		
			
				|  |  | +            defaultPutRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
 | 
	
		
			
				|  |  | +            log.info(JSONObject.toJSONString(defaultPutRet));
 | 
	
		
			
				|  |  | +            return url+path;
 | 
	
		
			
				|  |  | +        }catch (QiniuException ex){
 | 
	
		
			
				|  |  | +            Response r = ex.response;
 | 
	
		
			
				|  |  | +            log.error(""+r.toString());
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                log.error(r.bodyString());
 | 
	
		
			
				|  |  | +            } catch (QiniuException ex2) {
 | 
	
		
			
				|  |  | +                //ignore
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +           return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * @MethodName getUploadToken
 | 
	
		
			
				|  |  | +     * @Param []
 | 
	
		
			
				|  |  | +     * @Return java.lang.String
 | 
	
		
			
				|  |  | +     * @Author qiubo
 | 
	
		
			
				|  |  | +     * @Date 2020/5/11
 | 
	
		
			
				|  |  | +     * @Description 获取七牛云上传图片的token
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public String getUploadToken(){
 | 
	
		
			
				|  |  | +        Auth auth = Auth.create(accessKey, secretKey);
 | 
	
		
			
				|  |  | +        return auth.uploadToken(bucket);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public String uploadLocalFile(String path,String url){
 | 
	
		
			
				|  |  | +        Configuration cof = new Configuration(Zone.autoZone());
 | 
	
		
			
				|  |  | +        UploadManager uploadManager = new UploadManager(cof);
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            Response response = uploadManager.put(path, url, getUploadToken());
 | 
	
		
			
				|  |  | +            //解析上传成功的结果
 | 
	
		
			
				|  |  | +            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
 | 
	
		
			
				|  |  | +            System.out.println(putRet.key);
 | 
	
		
			
				|  |  | +            return putRet.key;
 | 
	
		
			
				|  |  | +        } catch (QiniuException ex) {
 | 
	
		
			
				|  |  | +            Response r = ex.response;
 | 
	
		
			
				|  |  | +            System.err.println(r.toString());
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                System.err.println(r.bodyString());
 | 
	
		
			
				|  |  | +            } catch (QiniuException ex2) {
 | 
	
		
			
				|  |  | +                //ignore
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |