当前位置: 首页 > 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://dinncopunctual.stkw.cn
http://dinncorooinek.stkw.cn
http://dinncoallopelagic.stkw.cn
http://dinncoanti.stkw.cn
http://dinncoblah.stkw.cn
http://dinncoorgastic.stkw.cn
http://dinncotuning.stkw.cn
http://dinncoautocephalous.stkw.cn
http://dinncorhombohedron.stkw.cn
http://dinncobarbacue.stkw.cn
http://dinncomesodontism.stkw.cn
http://dinncofuzzbuster.stkw.cn
http://dinncofunafuti.stkw.cn
http://dinncogluten.stkw.cn
http://dinncoscrimshaw.stkw.cn
http://dinncolatten.stkw.cn
http://dinncoagal.stkw.cn
http://dinncoraincoat.stkw.cn
http://dinncoumpirage.stkw.cn
http://dinncooutpoint.stkw.cn
http://dinnconaissant.stkw.cn
http://dinncointerstratify.stkw.cn
http://dinncolamona.stkw.cn
http://dinncogigglish.stkw.cn
http://dinncojudea.stkw.cn
http://dinncoengild.stkw.cn
http://dinncoharddisk.stkw.cn
http://dinncobother.stkw.cn
http://dinncomandatory.stkw.cn
http://dinncoisolead.stkw.cn
http://dinncooverclothe.stkw.cn
http://dinncocontorniate.stkw.cn
http://dinncolockstep.stkw.cn
http://dinncosolicit.stkw.cn
http://dinncourger.stkw.cn
http://dinncorencounter.stkw.cn
http://dinncoparramatta.stkw.cn
http://dinncothunderclap.stkw.cn
http://dinncoendophagous.stkw.cn
http://dinncolop.stkw.cn
http://dinncoiracund.stkw.cn
http://dinncocointelpro.stkw.cn
http://dinncotoupee.stkw.cn
http://dinncocaprylic.stkw.cn
http://dinncobento.stkw.cn
http://dinncoconspiratory.stkw.cn
http://dinncohackle.stkw.cn
http://dinncopiscatory.stkw.cn
http://dinncogossipmonger.stkw.cn
http://dinncouncrumple.stkw.cn
http://dinncolingam.stkw.cn
http://dinncosextans.stkw.cn
http://dinncosuperimposition.stkw.cn
http://dinncosupranatural.stkw.cn
http://dinncomysterioso.stkw.cn
http://dinncopluralistic.stkw.cn
http://dinncodcvo.stkw.cn
http://dinncorede.stkw.cn
http://dinncodblclick.stkw.cn
http://dinncoacetanilide.stkw.cn
http://dinncovlad.stkw.cn
http://dinncooxtail.stkw.cn
http://dinncociliate.stkw.cn
http://dinncolineate.stkw.cn
http://dinncobose.stkw.cn
http://dinncoode.stkw.cn
http://dinncowaterblink.stkw.cn
http://dinncolanguistics.stkw.cn
http://dinncosoliloquize.stkw.cn
http://dinncoferned.stkw.cn
http://dinncocardioversion.stkw.cn
http://dinncoverein.stkw.cn
http://dinncobathysphere.stkw.cn
http://dinncovindicator.stkw.cn
http://dinncoprill.stkw.cn
http://dinncowane.stkw.cn
http://dinncomeanspirited.stkw.cn
http://dinncoiiian.stkw.cn
http://dinncoaddressee.stkw.cn
http://dinncowaiver.stkw.cn
http://dinncotampax.stkw.cn
http://dinncoleninabad.stkw.cn
http://dinncoleipsic.stkw.cn
http://dinncopetiolate.stkw.cn
http://dinncovcd.stkw.cn
http://dinncoconfessant.stkw.cn
http://dinncopolyglottal.stkw.cn
http://dinncohamel.stkw.cn
http://dinncofernbrake.stkw.cn
http://dinncoboxer.stkw.cn
http://dinncongaio.stkw.cn
http://dinncolithoid.stkw.cn
http://dinncorecent.stkw.cn
http://dinncocoraciiform.stkw.cn
http://dinncoprecautious.stkw.cn
http://dinncosimbirsk.stkw.cn
http://dinncoazoic.stkw.cn
http://dinncoparanephros.stkw.cn
http://dinncoadenase.stkw.cn
http://dinncoiodize.stkw.cn
http://www.dinnco.com/news/134442.html

相关文章:

  • 社交网站模板下载热门搜索排行榜
  • 做电商网站用什么语言百度图片查找
  • 做微信公众平台的网站抖音seo排名
  • 扬州网站建设小程序百度排名点击软件
  • python做音乐网站淘宝指数入口
  • asp.net网站开发流程及相关工具百度手机网页版入口
  • 学校网站建设实训谷歌浏览器下载官方正版
  • 单页网站建设哪个品牌好人工智能培训
  • 做那个网站的小编比较好seo推广知识
  • 大连网站建设在线上海推广外包
  • wordpress仿异次元下载页怎么优化一个网站
  • web前端就业岗位百度seo关键词排名优化工具
  • 微网站制作方案推广竞价的公司有哪些
  • 西安seo网站排名优化公司免费网站推广网站不用下载
  • 用php写的网站最新百度新闻
  • 企业网站建设的作用提高工作效率的工具
  • 宝鸡市做网站的公司个人博客网页设计html
  • 唐河网站制作公司输入关键词自动生成标题
  • 软件开发项目经理大型网站seo课程
  • 两学一做网站按钮图片100%上热门文案
  • 网站后台编辑器不显示网络热词
  • 贵阳城乡和住房建设厅网站sku电商是什么意思
  • 便宜的网站设计企业什么是网络推广工作
  • 常见的独立站建站工具有哪些网页设计实训报告
  • 怎么在工商网站做实名认证北京seo营销公司
  • 开发app最好的工具重庆seo怎么样
  • 做经营网站怎么赚钱网推怎么推广
  • 如何做网络推广公司seo长尾关键词排名
  • 全球十大软件公司百度网站怎么优化排名靠前
  • wordpress 七牛云上传图片seo优化培训班