You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB
Java

6 years ago
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 uploadCategoryUrl(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 {
//
// }
}