package com.sxsoft.admin.utils; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.qiniu.storage.BucketManager; import com.qiniu.storage.Region; import com.qiniu.storage.model.FileInfo; import com.qiniu.util.StringUtils; import com.sxsoft.admin.entity.SysAttachment; import com.sxsoft.admin.entity.TTrade; import com.sxsoft.admin.service.SysAttachmentService; import com.sxsoft.admin.service.TTradeService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import com.qiniu.storage.UploadManager; import com.qiniu.common.Zone; import com.qiniu.storage.Configuration; import com.qiniu.util.Auth; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; import java.util.UUID; @Component public class QiniuUtils { private final static Logger log = LoggerFactory.getLogger(QiniuUtils.class); public static String QiniuAccessKey; public static String QiniuSecretKey; public static String QiniuBucket; public static String QiniuDoMian; @Value("${sxsoft.Qiniu.QiniuAccessKey}") private String _QiniuAccessKey; @Value("${sxsoft.Qiniu.QiniuSecretKey}") private String _QiniuSecretKey; @Value("${sxsoft.Qiniu.QiniuBucket}") private String _QiniuBucket; @Value("${sxsoft.Qiniu.QiniuDoMian}") private String _QiniuDoMian; static Auth auth ; static Configuration cfg = new Configuration(Region.autoRegion()); static UploadManager uploadManager = new UploadManager(cfg); @PostConstruct private void init() { QiniuAccessKey=_QiniuAccessKey; QiniuSecretKey=_QiniuSecretKey; QiniuBucket=_QiniuBucket; QiniuDoMian=_QiniuDoMian; auth= Auth.create(QiniuAccessKey, QiniuSecretKey); } //简单上传,使用默认策略,只需要设置上传的空间名就可以了 public static String getUpToken() { return auth.uploadToken(QiniuBucket); } public static void upload(String savekey,String localFile) throws IOException { try { //调用put方法上传 Response res = uploadManager.put(localFile, savekey, getUpToken()); log.error("返回结果{}",res.bodyString()); } catch (QiniuException e) { Response r = e.response; // 请求失败时打印的异常的信息 System.out.println(r.toString()); try { //响应的文本信息 System.out.println(r.bodyString()); } catch (QiniuException e1) { //ignore } } } /** * 文件下上传 * @param savekey * @param localFile */ public Map<String,Object> UploadSignFile(String localFile, String savekey) { Map<String,Object> map = new HashMap<>(); try { //调用put方法上传 Response res = uploadManager.put(localFile, savekey, getUpToken()); log.error("上传七牛服务器返回结果{}",res.bodyString()); String jsonStr = JSONObject.toJSONString(res); map = JSONObject.parseObject(jsonStr); if(map != null && !map.isEmpty()){ BucketManager bucketManager = new BucketManager(auth, cfg); FileInfo fileInfo = bucketManager.stat(QiniuBucket, savekey); String md5 = fileInfo.md5; String mimeType = fileInfo.mimeType; map.put("md5",md5); map.put("mimeType",mimeType); } } catch (QiniuException e) { Response r = e.response; // 请求失败时打印的异常的信息 System.out.println(r.toString()); try { //响应的文本信息 System.out.println(r.bodyString()); } catch (QiniuException e1) { //ignore } } return map; } /** * 文件下载 * @param key * @return */ public String DownloadSignFile(String key) { //调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间 String downloadRUL = auth.privateDownloadUrl(QiniuDoMian+"/"+key, 3600); return downloadRUL; } }
标签:封装,String,com,qiniu,七牛,static,import,上传,public From: https://www.cnblogs.com/ht-privete-logs/p/17174339.html