|
@ -29,6 +29,7 @@ import com.epmet.entity.OssEntity; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
import com.epmet.exception.ModuleErrorCode; |
|
|
import com.epmet.service.OssService; |
|
|
import com.epmet.service.OssService; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import net.coobird.thumbnailator.Thumbnails; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
@ -37,8 +38,13 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
|
|
|
import java.math.BigDecimal; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
@ -94,6 +100,37 @@ public class OssServiceImpl extends BaseServiceImpl<OssDao, OssEntity> implement |
|
|
return new Result<UploadImgResultDTO>().ok(dto); |
|
|
return new Result<UploadImgResultDTO>().ok(dto); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public Result<UploadImgResultDTO> compressUploadImg(MultipartFile file, String privacy) { |
|
|
|
|
|
if (file.isEmpty()) { |
|
|
|
|
|
return new Result<UploadImgResultDTO>().error(ModuleErrorCode.UPLOAD_FILE_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
//上传文件
|
|
|
|
|
|
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
|
|
|
|
|
String url = null; |
|
|
|
|
|
String ossDomain = null; |
|
|
|
|
|
try { |
|
|
|
|
|
byte[] fileBytes = compressPicCycle(file.getBytes(),200L,0.7); |
|
|
|
|
|
AbstractCloudStorageService storageService = OssFactory.build(); |
|
|
|
|
|
url = storageService.uploadSuffix(fileBytes, extension, privacy); |
|
|
|
|
|
ossDomain = storageService.getOssDomain(privacy); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
logger.error("图片上传异常", e); |
|
|
|
|
|
throw new RenException("图片上传异常"); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
//保存文件信息
|
|
|
|
|
|
OssEntity ossEntity = new OssEntity(); |
|
|
|
|
|
ossEntity.setUrl(url); |
|
|
|
|
|
|
|
|
|
|
|
baseDao.insert(ossEntity); |
|
|
|
|
|
//文件信息
|
|
|
|
|
|
UploadImgResultDTO dto = new UploadImgResultDTO(); |
|
|
|
|
|
dto.setUrl(url); |
|
|
|
|
|
dto.setDomain(ossDomain); |
|
|
|
|
|
return new Result<UploadImgResultDTO>().ok(dto); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public Result<UploadImgResultDTO> extUpload(MultipartFile file, String fileName, String privacy) { |
|
|
public Result<UploadImgResultDTO> extUpload(MultipartFile file, String fileName, String privacy) { |
|
|
try { |
|
|
try { |
|
@ -262,5 +299,33 @@ public class OssServiceImpl extends BaseServiceImpl<OssDao, OssEntity> implement |
|
|
return ossDomain.concat(File.separator).concat(ossPrefix).concat(File.separator).concat(filePath); |
|
|
return ossDomain.concat(File.separator).concat(ossPrefix).concat(File.separator).concat(filePath); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* |
|
|
|
|
|
* @param bytes 原图片字节数组 |
|
|
|
|
|
* @param desFileSize 指定图片大小,单位 kb |
|
|
|
|
|
* @param accuracy 精度,递归压缩的比率,建议小于0.9 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private byte[] compressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException{ |
|
|
|
|
|
// 获取目标图片
|
|
|
|
|
|
long fileSize = bytes.length; |
|
|
|
|
|
System.out.println("=====fileSize======== "+fileSize); |
|
|
|
|
|
// 判断图片大小是否小于指定图片大小
|
|
|
|
|
|
if(fileSize <= desFileSize * 1024){ |
|
|
|
|
|
return bytes; |
|
|
|
|
|
} |
|
|
|
|
|
//计算宽高
|
|
|
|
|
|
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes)); |
|
|
|
|
|
int imgWidth = bim.getWidth(); |
|
|
|
|
|
System.out.println(imgWidth+"====imgWidth====="); |
|
|
|
|
|
int imgHeight = bim.getHeight(); |
|
|
|
|
|
int desWidth = new BigDecimal(imgWidth).multiply( new BigDecimal(accuracy)).intValue(); |
|
|
|
|
|
System.out.println(desWidth+"====desWidth====="); |
|
|
|
|
|
int desHeight = new BigDecimal(imgHeight).multiply( new BigDecimal(accuracy)).intValue(); |
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
|
|
|
|
|
|
Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos); |
|
|
|
|
|
//如果不满足要求,递归直至满足要求
|
|
|
|
|
|
return compressPicCycle(baos.toByteArray(), desFileSize, accuracy); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|