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

浦口区城乡建设集团网站百度推广价格表

浦口区城乡建设集团网站,百度推广价格表,域名注册和网站建设,短视频广告分析这篇文章算是一篇水文,因为也没啥好讲的,在Spring Boot中,上传文件是我们常常做的,包括我们在实际开发过程中,我们也经常碰到与文件上传有关的功能,这也算是我们常用的一个功能了,毕竟作为开发者…

这篇文章算是一篇水文,因为也没啥好讲的,在Spring Boot中,上传文件是我们常常做的,包括我们在实际开发过程中,我们也经常碰到与文件上传有关的功能,这也算是我们常用的一个功能了,毕竟作为开发者,我们避免不了与各种文件打交道,一般文件上传是我们最常见的一种方式,例如我们对Excel数据的解析入库,图片的裁剪,都需要我们先将文件上传之后再对文件进行解析。

注意本篇博客,主要适合初学者,如果不感兴趣,可以移步了。

单文件上传

本篇,我将采用Thymeleaf模版引擎进行,故而第一步,我们还是引入相关依赖。

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

在application.properties文件中配置存放地址,以及允许上传的文件的大小

server.port=1243
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MBfile.upload.path=D:/test/
@Controller
@Slf4j
public class UploadController {@Value("${file.upload.path}")private String path;@GetMapping("/")public String uploadPage() {return "upload";}@PostMapping("/upload")@ResponseBodypublic String create(@RequestPart MultipartFile file) throws IOException {String fileName = file.getOriginalFilename();String filePath = path + fileName;File dest = new File(filePath);Files.copy(file.getInputStream(), dest.toPath());return "上传本地文件路径: " + dest.getAbsolutePath();}}
<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8" /><title>文件上传页面</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">选择要上传的文件:<input type="file" name="file"><br><hr><input type="submit" value="提交">
</form>
</body>
</html>

通过浏览器,localhost:1243
在这里插入图片描述在这里插入图片描述

在这里插入图片描述
这里仅仅是一个案例,在实际开发过程中,我们需要考虑更多,比如文件上传后的文件我们需要进行相关处理,一般可以添加时间日期等预防同名,在分布式服务下,我们需要考虑文件如何共享访问等等。

多文件上传

多文件上传其实也很简单,在我们上边的基础之上,进行改造就行了,因为涉及到多个文件,故而我们需要将上述代码进行改造,如果是单个文件上传,那么我们就使用一个对象就可以解决,既然涉及到多个文件,那么我们可以尝试使用数组对象进行。

还是之前的那样,第一步先引入依赖:
注意:这里的依赖和上述依赖一模一样,毫无变化,配置文件也是。

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MBserver.port=1243file.upload.path=D:/test/

在前端页面上,我多加了一个文件提交按钮。

<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8" /><title>文件上传页面</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">文件1:<input type="file" name="files"><br>文件2:<input type="file" name="files"><br><hr><input type="submit" value="提交">
</form>
</body>
</html>

文件上传控制类:

@Controller
@Slf4j
public class UploadController {@Value("${file.upload.path}")private String path;@GetMapping("/")public String uploadPage() {return "upload";}@PostMapping("/upload")@ResponseBody//主要这里,我们改造的地方,由单个对象变成了数组对象public String create(@RequestPart MultipartFile[] files) throws IOException {StringBuffer message = new StringBuffer();//循环遍历出数组对象for (MultipartFile file : files) {//为了避免文件名同名,我加了日期前置,你也选择精确的具体时间String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());String fileName = date + file.getOriginalFilename();String filePath = path + fileName;File dest = new File(filePath);Files.copy(file.getInputStream(), dest.toPath());message.append("文件上传成功 : " + dest.getAbsolutePath()).append("<br>");}return message.toString();}}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
也不知道怎么说,其实实现一个简单的文件上传案例其实也没啥可以说的,也很简单,可以在以上基础之上,对代码进行改造,至于想改造成啥,那就得看你的需求了。

http://www.hkea.cn/news/299009/

相关文章:

  • 桂林创新大厦网站今日十大热点新闻事件
  • 做网站空间哪家好windows7系统优化工具
  • 网站建设首选公司seo推广一个月见效
  • 微信做模板下载网站有哪些推广网站要注意什么
  • 做网站 java c常德seo快速排名
  • 仙桃做网站找谁常用的网络推广方法
  • 品牌推广网站怎样做百度手机助手苹果版
  • 武汉工业网站制作百度人工服务热线24小时
  • 新闻头条最新消息今日头条站长之家seo综合
  • app与网站宁波seo网络推广渠道介绍
  • 国外学做咖啡的网站百度高级搜索网址
  • 建网站开源代码游戏推广怎么找玩家
  • 莱州哪里有做网站的浙江网站建设平台
  • ps网站设计与制作免费推广seo
  • 网站查询功能怎么做关键词搜索量怎么查
  • 付费网站推广网站优化包括哪些内容
  • 在日本做色情网站广州seo外包
  • 最棒的网站建设考研最靠谱的培训机构
  • 广州建设企业网站黑河seo
  • 招商网站建设性价比高seo排名优化的
  • 产品网站怎么做的长沙正规关键词优化价格从优
  • 怎样查询江西省城乡建设厅网站杭州seo网
  • 网站建设空间是指什么软件网站优化最为重要的内容是
  • 做美工要开通什么网站的会员呢新网站友链
  • 网站集约化建设推进情况推广app赚钱
  • 番禺大石做网站域名污染查询网站
  • 长沙市在建工程项目免费seo快速排名工具
  • 南宁定制网站制作电话图片外链生成工具
  • 哪些网站做的海报比较高大上百度客服电话是多少
  • 菏泽网站建设电话常州seo外包