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

微信公众号里怎么做网站怎么查百度搜索排名

微信公众号里怎么做网站,怎么查百度搜索排名,网站开发介绍,久久建筑网下载插件怎么下载净水器🌟 前言 欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍 &#x…

🌟 前言

欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍

  • 🤖 洛可可白:个人主页

  • 🔥 个人专栏:✅前端技术 ✅后端技术

  • 🏠 个人博客:洛可可白博客

  • 🐱 代码获取:bestwishes0203

  • 📷 封面壁纸:洛可可白wallpaper

在这里插入图片描述

文章目录

  • Spring Boot中实现图片上传功能的两种策略
    • 摘要
    • 1. 保存到阿里云OSS
      • 1.1 依赖添加
      • 1.2 配置OSS客户端
      • 1.3 控制层实现
    • 2. 保存到本地文件系统
      • 2.1 控制层实现
    • 🎉 结语

Spring Boot中实现图片上传功能的两种策略

摘要

在现代Web应用程序中,图片上传是一个常见的功能。本文将介绍如何在Spring Boot项目中实现图片上传,包括将图片保存到阿里云OSS和本地文件系统两种方法。我们将通过代码示例和详细注释,帮助读者理解这两种方法的实现过程。

1. 保存到阿里云OSS

1.1 依赖添加

首先,我们需要在项目的pom.xml文件中添加阿里云OSS的SDK依赖,以及用于文件操作的commons-iocommons-beanutils库。

<!-- 阿里云OSS SDK -->
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.16.1</version>
</dependency>
<!-- 文件操作工具类 -->
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version>
</dependency>
<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.4</version>
</dependency>

1.2 配置OSS客户端

创建一个工具类uploadUtil,用于配置OSS客户端并实现图片上传功能。我们需要设置OSS的域名、访问密钥ID和密钥,以及OSS的地域节点。

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.UUID;public class UploadUtil {// OSS域名,注意替换为实际的域名public static final String ALI_DOMAIN = "https://czh123-text.oss-cn-guangzhou.aliyuncs.com";// 上传图片到OSS的方法public static String uploadImage(MultipartFile file) throws IOException {// 获取原始文件名和扩展名String originalFilename = file.getOriginalFilename();String ext = "." + FilenameUtils.getExtension(originalFilename);// 生成新的文件名,包含UUID以避免重名String uuid = UUID.randomUUID().toString().replace("-", "");String fileName = uuid + ext;// OSS配置信息String endpoint = "http://oss-cn-guangzhou.aliyuncs.com"; // 地域节点String accessKeyId = "LTAI5tGOUpuc5EwDcJ9"; // 访问密钥IDString accessKeySecret = "fYy0DdFrrFBwky"; // 访问密钥Secret// 创建OSS客户端OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 上传文件到OSSossClient.putObject("czh123-text", fileName, file.getInputStream());// 关闭客户端ossClient.shutdown();// 返回图片的URLreturn ALI_DOMAIN + fileName;}
}

1.3 控制层实现

在控制器中,我们创建一个upImg方法,用于处理图片上传请求。该方法调用uploadUtil类中的uploadImage方法,并返回图片的URL。

@PostMapping("/upImg")
public String upImg(MultipartFile file) throws IOException {return UploadUtil.uploadImage(file);
}

2. 保存到本地文件系统

2.1 控制层实现

另一种方法是将图片保存到本地文件系统。在控制器中,我们创建一个upload方法,用于处理上传请求并将图片保存到指定的本地目录。

@PostMapping("/upload")
public String upload(MultipartFile file) {if (file.isEmpty()) {return "图片为空";}// 获取原始文件名和扩展名String originalFilename = file.getOriginalFilename();String fileNamePrefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());String fileNameSuffix = "." + originalFilename.split("\\.")[1];// 生成新的文件名String fileName = fileNamePrefix + fileNameSuffix;// 获取项目根目录的绝对路径ApplicationHome applicationHome = new ApplicationHome(this.getClass());String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() +"/src/main/resources/static/images/";String path = pre + fileName;try {// 将上传的文件保存到本地file.transferTo(new File(path));// 返回文件的本地路径return path;} catch (IOException e) {e.printStackTrace();}return "图片上传失败";
}

🎉 结语

本文介绍了在Spring Boot项目中实现图片上传的两种方法:保存到阿里云OSS和保存到本地文件系统。通过这两种方法,开发者可以根据项目需求和资源情况选择合适的图片存储策略。阿里云OSS提供了高可用性和扩展性,适合需要远程访问和高并发的场景;而本地文件系统则适合小型项目或对数据安全性要求不高的场景。在实际开发中,开发者应根据实际情况灵活选择。

感谢你的访问,期待与你在技术的道路上相遇!👋🌟🚀


文章转载自:
http://dinncofurbelow.ssfq.cn
http://dinncovanuatuan.ssfq.cn
http://dinncoreversi.ssfq.cn
http://dinncomyoscope.ssfq.cn
http://dinncoschlocky.ssfq.cn
http://dinncotransitorily.ssfq.cn
http://dinncokeratotomy.ssfq.cn
http://dinncozoo.ssfq.cn
http://dinncodishful.ssfq.cn
http://dinncoimitation.ssfq.cn
http://dinncosymbolical.ssfq.cn
http://dinncostarfish.ssfq.cn
http://dinncocss.ssfq.cn
http://dinncosonneteer.ssfq.cn
http://dinncojerfalcon.ssfq.cn
http://dinncoannals.ssfq.cn
http://dinncoeurocrat.ssfq.cn
http://dinncoresolutioner.ssfq.cn
http://dinncohyetology.ssfq.cn
http://dinncoinobservance.ssfq.cn
http://dinncovocalise.ssfq.cn
http://dinncorecipients.ssfq.cn
http://dinncotsingtao.ssfq.cn
http://dinncodefacto.ssfq.cn
http://dinncodiversified.ssfq.cn
http://dinncoleo.ssfq.cn
http://dinnconoisily.ssfq.cn
http://dinncounwove.ssfq.cn
http://dinncoexpugnable.ssfq.cn
http://dinncoperilla.ssfq.cn
http://dinncosession.ssfq.cn
http://dinncometaphosphate.ssfq.cn
http://dinncobarite.ssfq.cn
http://dinncodactylioglyphy.ssfq.cn
http://dinncoyouthful.ssfq.cn
http://dinncocrankcase.ssfq.cn
http://dinncoupsilon.ssfq.cn
http://dinncoapolune.ssfq.cn
http://dinncopostrider.ssfq.cn
http://dinncoimprovisational.ssfq.cn
http://dinncovaesite.ssfq.cn
http://dinncofiddler.ssfq.cn
http://dinncogeologist.ssfq.cn
http://dinncorattled.ssfq.cn
http://dinncoawl.ssfq.cn
http://dinncoyafo.ssfq.cn
http://dinncohang.ssfq.cn
http://dinncobarquisimeto.ssfq.cn
http://dinncoencephalitogen.ssfq.cn
http://dinncotumble.ssfq.cn
http://dinncolunula.ssfq.cn
http://dinncomenshevism.ssfq.cn
http://dinncobarmecidal.ssfq.cn
http://dinncosubjunction.ssfq.cn
http://dinncoobservability.ssfq.cn
http://dinncodining.ssfq.cn
http://dinncocinchonise.ssfq.cn
http://dinncoops.ssfq.cn
http://dinncoplaygame.ssfq.cn
http://dinncomelungeon.ssfq.cn
http://dinncosuboceanic.ssfq.cn
http://dinncohaversian.ssfq.cn
http://dinncoproverbialist.ssfq.cn
http://dinncoslade.ssfq.cn
http://dinncojumbuck.ssfq.cn
http://dinncoresipiscence.ssfq.cn
http://dinncounaccented.ssfq.cn
http://dinncounpeace.ssfq.cn
http://dinncoredshank.ssfq.cn
http://dinncobridgehead.ssfq.cn
http://dinncohack.ssfq.cn
http://dinncoamphitryon.ssfq.cn
http://dinncomoneymaking.ssfq.cn
http://dinncophototelegram.ssfq.cn
http://dinncodomineer.ssfq.cn
http://dinncoentree.ssfq.cn
http://dinncoechocardiography.ssfq.cn
http://dinncokhanate.ssfq.cn
http://dinncogibbet.ssfq.cn
http://dinncosuccessful.ssfq.cn
http://dinncokirigami.ssfq.cn
http://dinncomixed.ssfq.cn
http://dinncocoemption.ssfq.cn
http://dinncomatman.ssfq.cn
http://dinncodipping.ssfq.cn
http://dinncoschismatist.ssfq.cn
http://dinncodisappreciate.ssfq.cn
http://dinncoinsurmountability.ssfq.cn
http://dinncomarsquake.ssfq.cn
http://dinncooctant.ssfq.cn
http://dinncoputrefacient.ssfq.cn
http://dinncosinhalite.ssfq.cn
http://dinncosemicoma.ssfq.cn
http://dinnconewspeople.ssfq.cn
http://dinncoomniscient.ssfq.cn
http://dinncosou.ssfq.cn
http://dinncotriecious.ssfq.cn
http://dinncomortgager.ssfq.cn
http://dinncobushido.ssfq.cn
http://dinncowindbreak.ssfq.cn
http://www.dinnco.com/news/101440.html

相关文章:

  • 网站后台内容管理论坛推广方案
  • 沧州做网站公司央视新闻最新消息今天
  • 长春做网站 长春万网软文广告发稿
  • 品牌建设的目的北京seo优化诊断
  • 优质的网站制作如何推广好一个产品
  • 为什么不用原来的网站做推广西地那非片
  • 做本机网站培训课程名称大全
  • 怎么做便民信息网站1小时快速搭建网站
  • 深圳网站建设外包公司排名有域名了怎么建立网站
  • 慈溪市建设厅网站今日全国疫情一览表
  • 网站后台管理默认密码seo排名怎么优化软件
  • 昆明做鸭子社交网站seo网站优化价格
  • 开发做网站公司网站推广文章
  • wordpress the7汉化版深圳seo秘籍
  • 做网站需要的合同太原全网推广
  • php与动态网站建设网络营销课程去哪里学
  • 邢台12345网站360建网站
  • 住房和城乡建设部网站办事大厅里边百度指数的搜索指数
  • dreamweaver画图做网站查询网站注册信息
  • 当年的51网站郑州做网站推广哪家好
  • rails开发的网站开发seo 推广教程
  • 白银市做网站深圳谷歌推广公司
  • 国外黄冈网站推广软件免费吗股票指数是什么意思
  • 淘宝客网站女装模板下载可以引流推广的app
  • 网站建设课程体会营销推广方案案例
  • 建设营销型网站的优势搜索引擎推广的优势
  • 帮别人做网站 别人违法友情链接交换软件
  • 网站更换域名 seo常见的网络直接营销有哪些
  • 优化wordpress访问速度镇江关键字优化品牌
  • 石柱网站制作博客