-
七牛云文件上传
-
package com.hljt.uav.utils;
-
import com.google.gson.Gson;
import com.qiniu.common.QiniuException; import
com.qiniu.http.Response; import com.qiniu.storage.BucketManager;
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 com.qiniu.util.UrlSafeBase64; import
okhttp3.OkHttpClient; import okhttp3.Request; import
okhttp3.RequestBody; import
org.springframework.beans.factory.annotation.Value; import
org.springframework.stereotype.Service; import
org.springframework.web.multipart.MultipartFile; import
java.io.ByteArrayOutputStream; import java.io.IOException; import
java.io.InputStream; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.util.*;/** * @ProjectName: uav * @Package: com.hljt.uav.utils *
@ClassName: QiNiuUtil * @Description: java 类作用描述 * @Author: lhb *
@CreateDate: 2019/9/26 0026 下午 15:09 * @UpdateUser: 更新者 *
@UpdateDate: 2019/9/26 0026 下午 15:09 * @UpdateRemark: 更新说明 *
@Version: 1.0 */ @Service public class QiNiuUtil {@Value("${qiniu.accesskey}") private String qnyAccesskey; @Value("${qiniu.secretkey}") private String qnySecretkey; @Value("${qiniu.bucket}") private String qnyBucket; @Value("${qiniu.zone}") private String qnyZone; @Value("${qiniu.domain.bucket}") private String qnyDomainBucket; @Value("${qiniu.expire.in.seconds}") private long qnyExpireInSeconds; /** * **文件上传七牛云使用版** * 单文件上传 * @param file * @param * @param * @return */ public String uploadFile(MultipartFile file) { String originalFilename = file.getOriginalFilename(); int lastIndexOf = originalFilename.lastIndexOf("."); //获取文件后缀 String suffix = originalFilename.substring(lastIndexOf); //使用UUID随机产生文件名称,防止同名文件覆盖 String fileName = UUID.randomUUID().toString() + suffix; //构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Region.autoRegion()); //...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); //把文件转化为字节数组 InputStream is = null; ByteArrayOutputStream bos = null; try { is = file.getInputStream(); bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = -1; while ((len = is.read(b)) != -1){ bos.write(b, 0, len); } byte[] uploadBytes= bos.toByteArray(); Auth auth = getAuth(); String upToken; if(false){ upToken = auth.uploadToken(qnyBucket, fileName);//覆盖上传凭证 }else{ upToken = auth.uploadToken(qnyBucket); } //默认上传接口回复对象 DefaultPutRet putRet; //进行上传操作,传入文件的字节数组,文件名,上传空间,得到回复对象 Response response = uploadManager.put(uploadBytes, fileName, upToken); putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key);//key 文件名 System.out.println(putRet.hash);//hash 七牛返回的文件存储的地址,可以使用这个地址加七牛给你提供的前缀访问到这个视频。 return putRet.key; }catch (QiniuException e){ e.printStackTrace(); return ""; }catch (IOException e) { e.printStackTrace(); return ""; } } /** * 文件上传 * **多文件处理内存溢出** * @param * @return */ public List<Map<String,String>> uploadFiles(MultipartFile[] upFile) { List<Map<String,String> > resultMap = new ArrayList<>(); if (upFile.length>0){ for (MultipartFile file:upFile){ if (!file.isEmpty()&&file.getSize()>0){ String fileName = file.getOriginalFilename(); // int lastIndexOf = originalFilename.lastIndexOf("."); //获取文件后缀 // String suffix = originalFilename.substring(lastIndexOf); //使用UUID随机产生文件名称,防止同名文件覆盖 // String fileName = "hljt"+UUID.randomUUID().toString() + suffix; // String fileName = Uuids.shortUuid() + suffix; //构造一个带指定Zone对象的配置类 // Configuration cfg = new Configuration(Region.autoRegion()); Configuration cfg = new Configuration(Region.region2()); //...其他参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); //把文件转化为字节数组 InputStream is = null; ByteArrayOutputStream bos = null; try { is = file.getInputStream(); bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = -1; while ((len = is.read(b)) != -1){ bos.write(b, 0, len); } byte[] uploadBytes= bos.toByteArray(); Auth auth = getAuth(); String upToken; if(false){ upToken = auth.uploadToken(qnyBucket, fileName);//覆盖上传凭证 }else{ upToken = auth.uploadToken(qnyBucket); } //默认上传接口回复对象 DefaultPutRet putRet; //进行上传操作,传入文件的字节数组,文件名,上传空间,得到回复对象 Response response = uploadManager.put(uploadBytes, fileName, upToken); putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); /* Map<String,String> map = new HashMap<>(); map.put("name",putRet.key); map.put("url",qnyDomainBucket+putRet.key);*/ resultMap.add(BuildMap.nameAndUrl(putRet.key,qnyDomainBucket+putRet.key)); }catch (QiniuException e){ e.printStackTrace(); System.out.println(e.error()); }catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); } } } } return resultMap; } /** * 七牛云文件删除 * @param url * @throws QiniuException */ public void delete(String url) throws QiniuException{ Configuration cfg = new Configuration(Region.autoRegion()); // Auth auth = Auth.create(qnyAccesskey, qnySecretkey); Auth auth = getAuth(); BucketManager bucketManager = new BucketManager(auth, cfg); bucketManager.delete(qnyBucket, url.replaceAll(qnyDomainBucket,"")); } /** * 七牛云文件下载 * @param targetUrl * @return */ public String getDownloadUrl(String targetUrl) { Auth auth = getAuth(); String downloadUrl = auth.privateDownloadUrl(targetUrl); return downloadUrl; }
}
七牛云文件上传,解决大文件上传时idea 内存溢出问题
最新推荐文章于 2022-07-05 10:29:58 发布