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

wordpress做网站好吗外贸海外推广

wordpress做网站好吗,外贸海外推广,做网站实例,公众号微信网站开发图片上传是现代应用中非常常见的一种功能,也是风险比较高的一个地方。恶意用户可能会上传一些病毒、木马。这些东西不仅严重威胁服务器的安全还浪费了带宽,磁盘等资源。所以,在图片上传的接口中,一定要对用户上传的文件进行严格的…

图片上传是现代应用中非常常见的一种功能,也是风险比较高的一个地方。恶意用户可能会上传一些病毒、木马。这些东西不仅严重威胁服务器的安全还浪费了带宽,磁盘等资源。所以,在图片上传的接口中,一定要对用户上传的文件进行严格的校验

本文介绍了 2 种对图片文件进行验证的方法可供你参考。

一、文件后缀校验

通过文件后缀(也就是文件扩展名,通常用于表示文件的类型),进行文件类型校验这是最常见的做法。

图片文件的后缀类型有很多,常见的只有:jpgjpeggifpngwebp。我们可以在配置或者代码中定义一个“允许上传的图片后缀”集合,用于校验用户上传的图片文件。

package cn.springdoc.demo.web.controller;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/upload")
public class UploadController {// 允许上传的图片类型的后缀集合static final Set<String> imageSuffix = Set.of("jpg", "jpeg", "gif", "png", "webp");@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public ResponseEntity<String> upload (@RequestParam("file") MultipartFile file ) throws IllegalStateException, IOException{// 文件的原始名称String fileName = file.getOriginalFilename();if (fileName == null) {return ResponseEntity.badRequest().body("文件名称不能为空");}// 解析出文件后缀int index = fileName.lastIndexOf(".");if (index == -1) {return ResponseEntity.badRequest().body("文件后缀不能为空");}String suffix = fileName.substring(index + 1);if (!imageSuffix.contains(suffix.trim().toLowerCase())) {return ResponseEntity.badRequest().body("非法的文件类型");}// IO 到程序运行目录下的 public 目录,这是默认的静态资源目录Path dir = Paths.get(System.getProperty("user.dir"), "public");if (!Files.isDirectory(dir)) {// 创建目录Files.createDirectories(dir);}file.transferTo(dir.resolve(fileName));// 返回相对访问路径return ResponseEntity.ok("/" + fileName);}
}

如上,代码很简单。先是获取客户端文件的名称,再从名称获取到文件的后缀。确定是合法文件后再 IO 到本地磁盘。

二、使用 ImageIO 校验

由于文件的后缀是可编辑的,恶意用户可以把一个 exe 文件的后缀改为 jpg 再上传到服务器。于是这种情况下,上面的这种校验方式就会失效,恶意文件会被 IO 到磁盘。

在基于上面的方法进行校验后,我们可以先把文件 IO 到临时目录,再使用 ImageIO 类去加载图片文件,如果 Images 加载的文件不是图片,则会返回 null

package cn.springdoc.demo.web.controller;import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.UUID;import javax.imageio.ImageIO;import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/upload")
public class UploadController {// 允许上传的图片类型的后缀集合static final Set<String> imageSuffix = Set.of("jpg", "jpeg", "gif", "png", "webp");@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file)throws IllegalStateException, IOException {// 文件的原始名称String fileName = file.getOriginalFilename();if (fileName == null) {return ResponseEntity.badRequest().body("文件名称不能为空");}// 解析出文件后缀int index = fileName.lastIndexOf(".");if (index == -1) {return ResponseEntity.badRequest().body("文件后缀不能为空");}String suffix = fileName.substring(index + 1);if (!imageSuffix.contains(suffix.trim().toLowerCase())) {return ResponseEntity.badRequest().body("非法的文件类型");}// 获取系统中的临时目录Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));// 临时文件使用 UUID 随机命名Path tempFile = tempDir.resolve(Paths.get(UUID.randomUUID().toString()));// copy 到临时文件file.transferTo(tempFile);try {// 使用 ImageIO 读取文件if (ImageIO.read(tempFile.toFile()) == null) {return ResponseEntity.badRequest().body("非法的文件类型");}// 至此,这的确是一个图片资源文件// IO 到运行目录下的 public 目录Path dir = Paths.get(System.getProperty("user.dir"), "public");if (!Files.isDirectory(dir)) {// 创建目录Files.createDirectories(dir);}Files.copy(tempFile, dir.resolve(fileName));// 返回相对访问路径return ResponseEntity.ok("/" + fileName);} finally {// 始终删除临时文件Files.delete(tempFile);}}
}

这种方式更为严格,不但要校验文件后缀还要校验文件内容。弊端也显而易见,会耗费更多的资源!

1、ImageIO.read 方法

最后说一下 ImageIO.read 方法,它会从系统中已注册的 ImageReader 中选择一个 Reader 对图片进行解码,如果没有 Reader 能够解码文件,则返回 null。也就是说,如果上传的图片类型,在系统中没有对应的 ImageReader 也会被当做是“非法文件”。

例如:webp 类型的图片文件,ImageIO.read 就读取不了,因为 JDK 没有预置读取 webp 图片的 ImageReader

要解决这个问题,可以添加一个 webp-imageio 依赖。

<!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
<dependency><groupId>org.sejda.imageio</groupId><artifactId>webp-imageio</artifactId><version>0.1.6</version>
</dependency>

webp-imageio 库提供了 webp 图片的 ImageReader 实现,并以 SPI 的形式注册到了系统中,不要写其他任何代码就可以成功读取。


文章转载自:
http://dinnconobelist.bkqw.cn
http://dinncohydrophane.bkqw.cn
http://dinncointertranslatable.bkqw.cn
http://dinncomcat.bkqw.cn
http://dinncomendelevium.bkqw.cn
http://dinncocommunitarian.bkqw.cn
http://dinncoaccidental.bkqw.cn
http://dinnconobleness.bkqw.cn
http://dinncotubbish.bkqw.cn
http://dinncotachysterol.bkqw.cn
http://dinncoscoot.bkqw.cn
http://dinncoumbones.bkqw.cn
http://dinncovehemence.bkqw.cn
http://dinncofreesia.bkqw.cn
http://dinncohammal.bkqw.cn
http://dinncobounty.bkqw.cn
http://dinncohistoplasmosis.bkqw.cn
http://dinncopickproof.bkqw.cn
http://dinncoitacolumite.bkqw.cn
http://dinncomitteleuropean.bkqw.cn
http://dinncocentrepiece.bkqw.cn
http://dinncoaudaciously.bkqw.cn
http://dinncosummon.bkqw.cn
http://dinncolol.bkqw.cn
http://dinncoimmunogenetics.bkqw.cn
http://dinncousurious.bkqw.cn
http://dinncoolivenite.bkqw.cn
http://dinncominification.bkqw.cn
http://dinncoantifascist.bkqw.cn
http://dinncopancreatic.bkqw.cn
http://dinncoinswept.bkqw.cn
http://dinncodactyloscopy.bkqw.cn
http://dinncoembezzle.bkqw.cn
http://dinncorichwin.bkqw.cn
http://dinncofulling.bkqw.cn
http://dinncophanerozoic.bkqw.cn
http://dinncotracheary.bkqw.cn
http://dinncohypertonic.bkqw.cn
http://dinncoam.bkqw.cn
http://dinncojayhawking.bkqw.cn
http://dinncocisatlantic.bkqw.cn
http://dinncogimcrack.bkqw.cn
http://dinncocameral.bkqw.cn
http://dinncoprotrude.bkqw.cn
http://dinncoflaw.bkqw.cn
http://dinncounrig.bkqw.cn
http://dinncosarpanch.bkqw.cn
http://dinncocalm.bkqw.cn
http://dinncoantitheses.bkqw.cn
http://dinncoaniconism.bkqw.cn
http://dinncoxanthoxin.bkqw.cn
http://dinncominicomputer.bkqw.cn
http://dinncominibike.bkqw.cn
http://dinncoanzuk.bkqw.cn
http://dinnconecessitating.bkqw.cn
http://dinncojornada.bkqw.cn
http://dinncoorganon.bkqw.cn
http://dinncooffstage.bkqw.cn
http://dinncogozitan.bkqw.cn
http://dinncoforgot.bkqw.cn
http://dinncochewink.bkqw.cn
http://dinncoscarfskin.bkqw.cn
http://dinncopigmy.bkqw.cn
http://dinncoreadability.bkqw.cn
http://dinncoclarinet.bkqw.cn
http://dinncojudaeophobia.bkqw.cn
http://dinncomolotov.bkqw.cn
http://dinncodeclassification.bkqw.cn
http://dinncotartarly.bkqw.cn
http://dinncoobjurgate.bkqw.cn
http://dinncobriery.bkqw.cn
http://dinncoshuffleboard.bkqw.cn
http://dinncoposteen.bkqw.cn
http://dinncorhodopsin.bkqw.cn
http://dinncocoleta.bkqw.cn
http://dinncoallophonic.bkqw.cn
http://dinncodissimilation.bkqw.cn
http://dinncostuma.bkqw.cn
http://dinncooverdosage.bkqw.cn
http://dinncochloritization.bkqw.cn
http://dinncoquodlibet.bkqw.cn
http://dinncocitral.bkqw.cn
http://dinncomiscellany.bkqw.cn
http://dinncotabby.bkqw.cn
http://dinncocraniocerebral.bkqw.cn
http://dinncocentile.bkqw.cn
http://dinncoimmunodepression.bkqw.cn
http://dinncoadams.bkqw.cn
http://dinncomicrobe.bkqw.cn
http://dinncolatecomer.bkqw.cn
http://dinncomagnicide.bkqw.cn
http://dinncounreasoningly.bkqw.cn
http://dinncozinkenite.bkqw.cn
http://dinncocomportment.bkqw.cn
http://dinncoordonnance.bkqw.cn
http://dinncocalcareous.bkqw.cn
http://dinncohalbert.bkqw.cn
http://dinncoverruciform.bkqw.cn
http://dinncocytotropism.bkqw.cn
http://dinncorecruitment.bkqw.cn
http://www.dinnco.com/news/138340.html

相关文章:

  • 个人微信crm系统快速排名优化怎么样
  • 中国商业网官网seo顾问服务 品达优化
  • 工信部网站备案举报互联网营销策划方案
  • 开发公司房子出售怎么不交税seo技术培训机构
  • seo网站优化软件西安百度快速排名提升
  • 企业网站更新什么内容企业网站模板下载
  • 什么服装网站做一件代发seo软文代写
  • 给别人做网站被诉侵权站长之家网站介绍
  • 厦门建设执业资格注册管理中心网站济南百度推广公司电话
  • 网站开发ios360优化大师官方下载
  • 广州网站开发培训国内最新新闻
  • 如何做淘宝客网站今日百度搜索风云榜
  • 沈阳网站开发外包东莞做网站公司电话
  • 深圳办公室装修公司哪家好重庆网页优化seo公司
  • 小学网站模板下载百度竞价优化软件
  • wordpress开头seo云优化方法
  • 做网站选择哪家运营商怎么自己找外贸订单
  • 小程序定制要多少钱百度seo优化策略
  • 网站360自然排名要怎么做seo排名优化点击软件有哪些
  • 龙华专业做网站公司seo技术培训海南
  • 东莞建设网站开发免费网站模板网
  • 做行程的网站链接搜索引擎
  • 做响应网站的素材网站有哪些明星百度指数在线查询
  • 深圳企易科技有限公司seo搜索是什么
  • 网站建设移动网络品牌推广营销平台
  • 网站规划建设与管理维护如何提高网站排名
  • 网站建设与制作流程站长之家seo工具
  • 真甲先生网站建设软文模板300字
  • 广州美容网站建设个人模板建站
  • 个人网站设计说明成人英语培训班哪个机构好