obs 上传缩略图
public void thumbnail(PutObjectRequest request, UploadParam param) throws IOException { if (ObjectUtils.isEmpty(param.getThumbnail()) || !param.getThumbnail()) { return; } String fileExtension = getFileExtension(request.getObjectKey()); List<String> fileExtensionList = Arrays.asList(obsConfig.getImgType().toString().split(",")); if (!fileExtensionList.contains(fileExtension)) { return; } BufferedImage originalImage = ImageIO.read(param.getUploadFile().getInputStream()); // 获取原始图片宽度 int originalWidth = originalImage.getWidth(); // 获取原始图片高度 int originalHeight = originalImage.getHeight(); //计算宽高比例 double ratio = (double) originalWidth / originalHeight; // 设置压缩后图片的宽度 int targetWidth = param.getTargetWidth(); // 设置压缩后图片的宽度为100px // 根据宽高比例计算压缩后图片的高度 int targetHeight = (int) (targetWidth / ratio); BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null); g.dispose(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(resizedImage, fileExtension, byteArrayOutputStream); InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); try (ObsClient obsClient = new ObsClient(obsConfig.getAk(), obsConfig.getSk(), obsConfig.getEndPoint())) { PutObjectRequest thumbnailRequest = new PutObjectRequest(); thumbnailRequest.setBucketName(obsConfig.getBucketName()); thumbnailRequest.setObjectKey("thumbnail/" + request.getObjectKey()); thumbnailRequest.setAcl(AccessControlList.REST_CANNED_PRIVATE); thumbnailRequest.setInput(inputStream); PutObjectResult result = obsClient.putObject(thumbnailRequest); String url = generateObjectUrl(result); log.info("{} upload img : {}", param.getUserName(), url); } catch (ObsException e) { log.error("HTTP Code: " + e.getResponseCode()); log.error("Error Code:" + e.getErrorCode()); log.error("Error Message: " + e.getErrorMessage()); log.error("Request ID:" + e.getErrorRequestId()); log.error("Host ID:" + e.getErrorHostId()); } catch (Exception e) { log.error("其他错误: " + e.getMessage()); } finally { byteArrayOutputStream.close(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Data public class UploadParam { private MultipartFile uploadFile; private String userName; private String type; private Boolean byName; private Boolean thumbnail; private Integer targetWidth; public UploadParam() { this .thumbnail = false ; this .targetWidth = 100 ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2023-12-13 excel 取跨tab页的值