当前位置: 首页 > news >正文

萍乡网站推广百度在线客服中心

萍乡网站推广,百度在线客服中心,帮别人做高仿产品网站 违法么,申远空间设计公司在实际开发中,上传图片并生成缩略图是一项常见需求,例如在电商平台、社交应用等场景中,缩略图可以有效提高页面加载速度,优化用户体验。本文将介绍如何在 Spring Boot 项目中实现上传图片并生成缩略图的功能。 📦 1. …

在实际开发中,上传图片并生成缩略图是一项常见需求,例如在电商平台、社交应用等场景中,缩略图可以有效提高页面加载速度,优化用户体验。本文将介绍如何在 Spring Boot 项目中实现上传图片并生成缩略图的功能。

📦 1. 依赖配置

pom.xml 文件中添加以下依赖:

<!-- Spring Boot Web -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- 文件上传 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId>
</dependency><!-- 图片处理依赖 -->
<dependency><groupId>com.twelvemonkeys.imageio</groupId><artifactId>imageio-core</artifactId><version>3.8.1</version>
</dependency><!-- Apache Commons IO -->
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.8.0</version>
</dependency>
📂 2. 核心代码实现

以下方法将实现图片上传并生成缩略图的功能:

import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;public String uploadPictureThumbnail(String bucketName, MultipartFile multipartFile, Long assocId, String originalStorageName, Integer scale, Integer width, Integer height) {String directoryPath = "";if (OsUtils.isLinux()) {directoryPath = "/tempdir";} else if (OsUtils.isWindows()) {directoryPath = "C:/tempdir";}File directory = new File(directoryPath);if (!directory.exists()) {directory.mkdir();}String originalName = multipartFile.getOriginalFilename();String suffixName = getSuffixName(originalName, ".", 0);String suffix = getSuffixName(originalName, ".", 1);String storageName = UUID.randomUUID().toString() + suffixName;String storageFileName = null;List<String> thumbnailSuffixName = Arrays.asList(".png", ".jpg", ".jpeg", ".gif");if (thumbnailSuffixName.contains(suffixName.toLowerCase())) {try {// 保存原始图片String originalImagePath = directoryPath + "/" + storageName;Path filePath = Paths.get(originalImagePath);Files.write(filePath, multipartFile.getBytes());// 读取原始图片并生成缩略图BufferedImage originalImage = ImageIO.read(new File(originalImagePath));int originalWidth = originalImage.getWidth();int originalHeight = originalImage.getHeight();// 计算缩略图尺寸int[] data = computeSize(originalWidth, originalHeight, scale, width, height);int thumbnailWidth = data[0];int thumbnailHeight = data[1];Image scaledImage = originalImage.getScaledInstance(thumbnailWidth, thumbnailHeight, Image.SCALE_SMOOTH);BufferedImage thumbnailImage = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = thumbnailImage.createGraphics();g2d.drawImage(scaledImage, 0, 0, null);g2d.dispose();// 保存缩略图String thumbnailPath = directoryPath + "/" + UUID.randomUUID().toString() + suffixName;File thumbnailFile = new File(thumbnailPath);ImageIO.write(thumbnailImage, suffix.replace(".", ""), thumbnailFile);// 上传缩略图(这里用自定义 uploadFile 方法上传到对象存储)try (FileInputStream in = new FileInputStream(thumbnailFile)) {String name = getSuffixName(originalStorageName, "/", 1);storageFileName = uploadFile(bucketName, in, multipartFile.getContentType(), assocId, thumbnailFile.length(), name, "thumbnail");}// 清理临时文件new File(originalImagePath).delete();thumbnailFile.delete();} catch (IOException e) {e.printStackTrace();}}return storageFileName;
}
🔧 3. 计算缩略图尺寸方法

根据原始尺寸、比例、指定宽高生成合适的缩略图尺寸:

private int[] computeSize(int originalWidth, int originalHeight, Integer scale, Integer width, Integer height) {if (scale != null && scale > 0) {return new int[]{originalWidth * scale / 100, originalHeight * scale / 100};} else if (width != null && height != null) {return new int[]{width, height};} else {// 默认缩小为原始尺寸的 50%return new int[]{originalWidth / 2, originalHeight / 2};}
}
📝 4. 工具方法示例

用于提取文件后缀名:

private String getSuffixName(String fileName, String delimiter, int index) {String[] parts = fileName.split(delimiter);if (index < parts.length) {return delimiter + parts[parts.length - 1];}return "";
}
⚠️ 5. 注意事项
  1. 操作系统临时目录:根据不同操作系统,创建不同的临时文件夹路径。

  2. 文件清理:上传完成后及时删除临时文件,避免占用过多磁盘空间。

  3. 缩略图格式支持:目前支持 PNG、JPG、JPEG、GIF 格式。

  4. 上传逻辑uploadFile 方法需根据具体的存储服务(例如 MinIO、OSS、七牛云等)自定义实现。

🎯 6. 总结

通过以上步骤,我们成功实现了图片上传并生成缩略图的功能。此功能不仅能有效减少图片加载时间,还能节省存储空间,提升系统性能。如果你在实现过程中遇到问题,欢迎在评论区留言讨论!


文章转载自:
http://weazen.fcxt.cn
http://fluorimeter.fcxt.cn
http://unwooded.fcxt.cn
http://flaunt.fcxt.cn
http://consuelo.fcxt.cn
http://zanza.fcxt.cn
http://thickhead.fcxt.cn
http://infielder.fcxt.cn
http://etruscan.fcxt.cn
http://suspectable.fcxt.cn
http://forbore.fcxt.cn
http://tricotyledonous.fcxt.cn
http://debug.fcxt.cn
http://mayon.fcxt.cn
http://chiaroscurist.fcxt.cn
http://eurocapital.fcxt.cn
http://microcapsule.fcxt.cn
http://understock.fcxt.cn
http://mesocecum.fcxt.cn
http://expedite.fcxt.cn
http://medalist.fcxt.cn
http://tyre.fcxt.cn
http://unboundedly.fcxt.cn
http://aidman.fcxt.cn
http://vyivgly.fcxt.cn
http://tarmacadam.fcxt.cn
http://weapon.fcxt.cn
http://wahoo.fcxt.cn
http://anklebone.fcxt.cn
http://hillside.fcxt.cn
http://swimmingly.fcxt.cn
http://vitalist.fcxt.cn
http://riad.fcxt.cn
http://enantiotropy.fcxt.cn
http://pedochemical.fcxt.cn
http://paraselene.fcxt.cn
http://osiris.fcxt.cn
http://pomiculture.fcxt.cn
http://cutlass.fcxt.cn
http://djawa.fcxt.cn
http://orange.fcxt.cn
http://psychodrama.fcxt.cn
http://evincible.fcxt.cn
http://tuberculum.fcxt.cn
http://rejectivist.fcxt.cn
http://anaphase.fcxt.cn
http://jogjakarta.fcxt.cn
http://ringer.fcxt.cn
http://boondoggle.fcxt.cn
http://trisepalous.fcxt.cn
http://quonset.fcxt.cn
http://countrywoman.fcxt.cn
http://pyrotechnic.fcxt.cn
http://satinize.fcxt.cn
http://vauntful.fcxt.cn
http://outstanding.fcxt.cn
http://tilburg.fcxt.cn
http://porterhouse.fcxt.cn
http://upper.fcxt.cn
http://propaganda.fcxt.cn
http://consider.fcxt.cn
http://inconceivable.fcxt.cn
http://footplate.fcxt.cn
http://unsay.fcxt.cn
http://kickboard.fcxt.cn
http://citriculture.fcxt.cn
http://fishnet.fcxt.cn
http://landblink.fcxt.cn
http://laggardly.fcxt.cn
http://battlements.fcxt.cn
http://caloricity.fcxt.cn
http://verify.fcxt.cn
http://irrealizable.fcxt.cn
http://laxity.fcxt.cn
http://fanning.fcxt.cn
http://unmindful.fcxt.cn
http://cymoid.fcxt.cn
http://panini.fcxt.cn
http://heinous.fcxt.cn
http://diviner.fcxt.cn
http://lilied.fcxt.cn
http://discharge.fcxt.cn
http://marsupialize.fcxt.cn
http://irretention.fcxt.cn
http://prehormone.fcxt.cn
http://plexal.fcxt.cn
http://behest.fcxt.cn
http://rapacious.fcxt.cn
http://hohum.fcxt.cn
http://bibliolatry.fcxt.cn
http://remark.fcxt.cn
http://evagination.fcxt.cn
http://subzone.fcxt.cn
http://batt.fcxt.cn
http://sensual.fcxt.cn
http://guesswork.fcxt.cn
http://suckfish.fcxt.cn
http://flotant.fcxt.cn
http://unforgiving.fcxt.cn
http://landstream.fcxt.cn
http://www.hrbkazy.com/news/60090.html

相关文章:

  • 佛山网站建设企业百度做网站
  • 网站上的产品介绍如何做网站建设关键词排名
  • 闵行18路seo优化师是什么
  • 武汉网站建设方案外贸公司如何做推广
  • 可以做微网站的第三方平台备案域名交易平台
  • 第三方网站建设平台重庆森林壁纸
  • 电子商务网站开发的课程介绍百度推广有哪些推广方式
  • 北京网站开发要多少钱一个完整的营销策划案范文
  • 南昌网站开发培训学校无锡网站排名公司
  • 网站商城与网站区别百度一下生活更好
  • 上海 有哪些做网站的公司好宁波seo营销
  • 网站开发按钮图片素材百度河南代理商
  • 东莞网站建设模板报价seo营销
  • wordpress中的全站链接怎么改郑州网络营销与网站推广
  • 课程设计代做网站松松软文平台
  • 蓝色网站导航新app推广方案
  • 电商网站开发工作计划网络推广方法大全
  • 猪八戒接单平台武汉网络推广seo
  • 做网站可以用自己的主机海外推广运营
  • 如何网络推广优化深圳外包seo
  • 给人做网站免费引流在线推广
  • 家政公司怎么注册seo经理
  • wordpress 菜单 字体加粗深圳seo专家
  • 深圳工业设计展2024seo综合查询是什么意思
  • 关于企业网站建设的必要性营销引流都有什么方法
  • wordpress 文章 排序济南seo培训
  • 0基础学网站建设全国疫情实时资讯
  • 网站开发主要创新点兰州seo优化公司
  • 网站 内页网络广告
  • wordpress 架构原理百度地图优化排名方法