Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/com/bsd/cases/controller/CaseContentController.javamaster
commit
a9e44bf6af
@ -0,0 +1,53 @@
|
||||
package com.bsd.cases.conf;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "oss")
|
||||
public class OssProperties {
|
||||
//仓库名称
|
||||
// @Value("${oss.bucket}")
|
||||
public String bucket;
|
||||
//地域节点
|
||||
// @Value("${oss.endpoint}")
|
||||
public String endpoint;
|
||||
//AccessKey ID 阿里云AccessKey
|
||||
// @Value("${oss.accessId}")
|
||||
public String accessId ;
|
||||
//Access Key Secret 阿里云Secret
|
||||
// @Value("${oss.accessKey}")
|
||||
public String accessKey;
|
||||
|
||||
public String getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public void setBucket(String bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getAccessId() {
|
||||
return accessId;
|
||||
}
|
||||
|
||||
public void setAccessId(String accessId) {
|
||||
this.accessId = accessId;
|
||||
}
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.bsd.cases.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bsd.cases.conf.OssProperties;
|
||||
import com.bsd.cases.constants.Constants;
|
||||
import com.bsd.cases.mapper.CaseCategoryMapper;
|
||||
import com.bsd.cases.mapper.CaseContentAttachmentMapper;
|
||||
import com.bsd.cases.model.CaseCategory;
|
||||
import com.bsd.cases.model.CaseContentAttachment;
|
||||
import com.bsd.cases.util.FileSizeUtils;
|
||||
import com.bsd.cases.util.OSSClientUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class OssService {
|
||||
@Autowired
|
||||
OssProperties ossProperties;
|
||||
@Resource
|
||||
private CaseCategoryMapper caseCategoryMapper;
|
||||
|
||||
public JSONObject uploadAttachmentFile(MultipartFile file, String filedir) throws Exception {
|
||||
if (file == null || file.getSize() <= 0) {
|
||||
throw new Exception("file不能为空");
|
||||
}
|
||||
//文件名称
|
||||
String realFileName = file.getOriginalFilename();
|
||||
//文件前缀
|
||||
String fileName = realFileName.substring(0,realFileName.indexOf("."));
|
||||
//文件后缀
|
||||
String suffix = realFileName.substring(realFileName.lastIndexOf(".") + 1);
|
||||
//文件大小
|
||||
Long len = file.getSize();
|
||||
String size = FileSizeUtils.getPrintSize(len);
|
||||
|
||||
OSSClientUtil ossClient=new OSSClientUtil(ossProperties);
|
||||
String name = ossClient.uploadImg2Oss(file,filedir);
|
||||
String imgUrl = ossClient.getImgUrl(name,filedir);
|
||||
String[] split = imgUrl.split("\\?");
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("type",suffix);
|
||||
jsonObject.put("size",size);
|
||||
jsonObject.put("attachmentName",fileName);
|
||||
jsonObject.put("attachmentUrl",split[0]);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public String uploadFile(MultipartFile file, String filedir) throws Exception {
|
||||
if (file == null || file.getSize() <= 0) {
|
||||
throw new Exception("file不能为空");
|
||||
}
|
||||
|
||||
OSSClientUtil ossClient=new OSSClientUtil(ossProperties);
|
||||
String name = ossClient.uploadImg2Oss(file,filedir);
|
||||
String imgUrl = ossClient.getImgUrl(name,filedir);
|
||||
String[] split = imgUrl.split("\\?");
|
||||
|
||||
return split[0];
|
||||
}
|
||||
|
||||
// public JSONObject uploadContentMaterial(MultipartFile file, String filedir) throws Exception {
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.bsd.cases.util;
|
||||
|
||||
public class FileSizeUtils {
|
||||
/**
|
||||
* 字节 转换为B MB GB
|
||||
* @param size 字节大小
|
||||
* @return
|
||||
*/
|
||||
public static String getPrintSize(Long size){
|
||||
long rest = 0;
|
||||
if(size < 1024){
|
||||
return String.valueOf(size) + "B";
|
||||
}else{
|
||||
size /= 1024;
|
||||
}
|
||||
|
||||
if(size < 1024){
|
||||
return String.valueOf(size) + "KB";
|
||||
}else{
|
||||
rest = size % 1024;
|
||||
size /= 1024;
|
||||
}
|
||||
|
||||
if(size < 1024){
|
||||
size = size * 100;
|
||||
return String.valueOf((size / 100)) + "." + String.valueOf((rest * 100 / 1024 % 100)) + "MB";
|
||||
}else{
|
||||
size = size * 100 / 1024;
|
||||
return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "GB";
|
||||
}
|
||||
}
|
||||
public static void main(String[] args){
|
||||
System.out.println(getPrintSize(120005171L));
|
||||
System.out.println(getPrintSize(15522272L));
|
||||
System.out.println(getPrintSize(123456L));
|
||||
System.out.println(getPrintSize(12012L));
|
||||
System.out.println(getPrintSize(1021L));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue