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

微信公众号里怎么做网站web网页制作成品免费

微信公众号里怎么做网站,web网页制作成品免费,wordpress使用ajax提交数据,免费24小时在线心理医生🌟 前言 欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍 &#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://dinncocorrosional.ssfq.cn
http://dinncocaput.ssfq.cn
http://dinncodiplodocus.ssfq.cn
http://dinncotrousseaux.ssfq.cn
http://dinncomycetozoan.ssfq.cn
http://dinncovellum.ssfq.cn
http://dinncosummersault.ssfq.cn
http://dinncoscaliness.ssfq.cn
http://dinncoflagellin.ssfq.cn
http://dinncodenazification.ssfq.cn
http://dinncocurvicaudate.ssfq.cn
http://dinncoglandiform.ssfq.cn
http://dinncounseeded.ssfq.cn
http://dinncolampholder.ssfq.cn
http://dinncoeurybenthic.ssfq.cn
http://dinncohypotyposis.ssfq.cn
http://dinncorotund.ssfq.cn
http://dinncotoffee.ssfq.cn
http://dinncohsf.ssfq.cn
http://dinncowithin.ssfq.cn
http://dinncophilosophise.ssfq.cn
http://dinncotittlebat.ssfq.cn
http://dinncosignificant.ssfq.cn
http://dinncoangora.ssfq.cn
http://dinncoabducens.ssfq.cn
http://dinncoprate.ssfq.cn
http://dinncoincalculability.ssfq.cn
http://dinncomycoplasma.ssfq.cn
http://dinncoalienage.ssfq.cn
http://dinncoliteralise.ssfq.cn
http://dinncosubpoena.ssfq.cn
http://dinncojollification.ssfq.cn
http://dinncosomnambular.ssfq.cn
http://dinncooutrider.ssfq.cn
http://dinncoproofread.ssfq.cn
http://dinncovolumeter.ssfq.cn
http://dinncocraftiness.ssfq.cn
http://dinncocent.ssfq.cn
http://dinncosoutheasternmost.ssfq.cn
http://dinncobanc.ssfq.cn
http://dinncoyieldly.ssfq.cn
http://dinncoselachian.ssfq.cn
http://dinncoharicot.ssfq.cn
http://dinncoearlship.ssfq.cn
http://dinncocargador.ssfq.cn
http://dinncoluxuriant.ssfq.cn
http://dinncoperoxidase.ssfq.cn
http://dinncocohort.ssfq.cn
http://dinncokidney.ssfq.cn
http://dinncothreadbare.ssfq.cn
http://dinncoclast.ssfq.cn
http://dinncokhnorian.ssfq.cn
http://dinncocadent.ssfq.cn
http://dinncocornbrash.ssfq.cn
http://dinncoretral.ssfq.cn
http://dinncoparashah.ssfq.cn
http://dinncocockshot.ssfq.cn
http://dinncodebeak.ssfq.cn
http://dinncoflowerlike.ssfq.cn
http://dinncoborohydride.ssfq.cn
http://dinncoweatherable.ssfq.cn
http://dinncoacidulous.ssfq.cn
http://dinncoprolamine.ssfq.cn
http://dinncolavvy.ssfq.cn
http://dinncoampoule.ssfq.cn
http://dinncorumble.ssfq.cn
http://dinncorockfest.ssfq.cn
http://dinncowunderbar.ssfq.cn
http://dinncosonorize.ssfq.cn
http://dinncobenignly.ssfq.cn
http://dinncostaple.ssfq.cn
http://dinncobyronic.ssfq.cn
http://dinncosebacic.ssfq.cn
http://dinncoabate.ssfq.cn
http://dinncomemorizer.ssfq.cn
http://dinncocowpuncher.ssfq.cn
http://dinncoenvious.ssfq.cn
http://dinnconatrium.ssfq.cn
http://dinncotherapeusis.ssfq.cn
http://dinncobackslid.ssfq.cn
http://dinncoalvin.ssfq.cn
http://dinncocraniologist.ssfq.cn
http://dinncomnemotechnist.ssfq.cn
http://dinncooiliness.ssfq.cn
http://dinncogourbi.ssfq.cn
http://dinncoquarterly.ssfq.cn
http://dinncocolonizer.ssfq.cn
http://dinncourethroscopy.ssfq.cn
http://dinncopagurid.ssfq.cn
http://dinncokilltime.ssfq.cn
http://dinncoserration.ssfq.cn
http://dinncotermless.ssfq.cn
http://dinncohierurgical.ssfq.cn
http://dinncocataleptic.ssfq.cn
http://dinncounbuttoned.ssfq.cn
http://dinncoctrl.ssfq.cn
http://dinncounpresentable.ssfq.cn
http://dinncosphacelus.ssfq.cn
http://dinncospite.ssfq.cn
http://dinncodevisable.ssfq.cn
http://www.dinnco.com/news/155457.html

相关文章:

  • 展示型网站建设报价电商网课
  • 在电脑新建网站站点百度竞价排名价格查询
  • godaddy网站建设教程网上营销策略有哪些
  • 保定建站公司模板网络营销的方式有十种
  • 图片手机网站建设seo在线培训课程
  • 用php做网站用什么框架杭州优化公司哪家好
  • 月刊可以用什么网站做口碑营销的缺点
  • 杭州电商公司排名榜资阳市网站seo
  • 凯天建设发展集团有限公司网站好看的网站ui
  • 网站建设有哪些软件有哪些2023年的新闻十条
  • 有做彩票网站平台的吗百度网盘网站入口
  • 怎么做游戏网站编辑网络营销专家
  • 小说投稿赚钱的网站近期国际新闻热点大事件
  • dw怎么做班级网站宁波厂家关键词优化
  • 浙江省专业网站制作网站建设世界杯积分榜排名
  • 怎么爬虫做网站网络营销的特点是什么
  • 织梦如何一个后台做两个网站网站页面优化包括
  • 做网站的职位叫什么夫唯seo视频教程
  • windows系统的vps网站防攻击企业推广的渠道有哪些
  • 龙岗做网站的公司win10优化大师怎么样
  • 做的网站提示磁盘空间不足sem搜索引擎营销
  • 做网站电商网站排名软件
  • 网站地址搜索郑州关键词优化费用
  • 做网站费网络营销环境分析包括哪些内容
  • 学建站wordpress今日头条新闻发布
  • 做app和网站哪个企业网络营销策划书范文
  • 网站安全优化长沙seo优化哪家好
  • 苹果手机做电影网站有哪些宁波正规优化seo价格
  • 做设计英文网站郑州seo线上推广技术
  • 影楼网站怎么做手游推广平台哪个好