电子商务社交平台源码请加企鹅求求:三五三六二四七二五九。

这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件。

构建工程
为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖。为例能够上传文件在服务器,你需要在web.xml中加入标签做相关的配置,但在sringboot 工程中,它已经为你自动做了,所以不需要你做任何的配置。

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
</dependencies>复制代码

创建文件上传controller
直接贴代码:

@Controller
public class FileUploadController {private final StorageService storageService;@Autowiredpublic FileUploadController(StorageService storageService) {this.storageService = storageService;}@GetMapping("/")public String listUploadedFiles(Model model) throws IOException {model.addAttribute("files", storageService.loadAll().map(path ->MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()).build().toString()).collect(Collectors.toList()));return "uploadForm";}@GetMapping("/files/{filename:.+}")@ResponseBodypublic ResponseEntity<Resource> serveFile(@PathVariable String filename) {Resource file = storageService.loadAsResource(filename);return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"").body(file);}@PostMapping("/")public String handleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) {storageService.store(file);redirectAttributes.addFlashAttribute("message","You successfully uploaded " + file.getOriginalFilename() + "!");return "redirect:/";}@ExceptionHandler(StorageFileNotFoundException.class)public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {return ResponseEntity.notFound().build();}}复制代码

这个类通过@Controller注解,表明自己上一个Spring mvc的c。每个方法通过
@GetMapping 或者@PostMapping注解表明自己的 http方法。

GET / 获取已经上传的文件列表
GET /files/{filename} 下载已经存在于服务器的文件
POST / 上传文件给服务器
创建一个简单的 html模板
为了展示上传文件的过程,我们做一个界面:
在src/main/resources/templates/uploadForm.html

<html xmlns:th="http://www.thymeleaf.org">
<body><div th:if="${message}"><h2 th:text="${message}"/></div><div><form method="POST" enctype="multipart/form-data" action="/"><table><tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr><tr><td></td><td><input type="submit" value="Upload" /></td></tr></table></form></div><div><ul><li th:each="file : ${files}"><a th:href="${file}" th:text="${file}" /></li></ul></div></body>
</html>复制代码

上传文件大小限制
如果需要限制上传文件的大小也很简单,只需要在springboot 工程的src/main/resources/application.properties 加入以下:

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB复制代码

电子商务社交平台源码请加企鹅求求:三五三六二四七二五九

转载于:https://juejin.im/post/5cff232af265da1bd1464e03

java版spring cloud+spring boot+redis社交电子商务平台(十七)上传文件相关推荐

  1. (十三)java版spring cloud+spring boot+redis社交电子商务平台-springboot集成spring cache...

    电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.本文介绍如何在springboot中使用默认的spring cache, 声明式缓存 Spring 定义 CacheManager 和 Cach ...

  2. java版spring cloud+spring boot+redis社交电子商务平台(八)消息总线(Spring Cloud Bus)

    B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六.Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控. ...

  3. java版spring cloud+spring boot+redis社交电子商务平台-spring-cloud-config

    社交电商平台源码请加企鹅求求:一零三八七七四六二六.创建配置管理服务器及实现分布式配置管理应用,实现统一配置管理. 提供三种方式: 基于git 基于svn(淘汰) 基于本地文件(测试使用) 如何使用 ...

  4. java版spring cloud+spring boot+redis社交电子商务平台(四)SpringBoot 整合JPA

    java b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行 ...

  5. springboot分销商城spring cloud+redis社交电子商务平台

    涉及平台:平台管理(包含自营店面).买家平台(PC端.H5/公众号.小程序.APP端(IOS/Android).微服务 核心架构:Spring Cloud.Spring Boot.Mybatis Pl ...

  6. (四)java版spring cloud+spring boot 社交电子商务平台-断路器(Hystrix)

    b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTempl ...

  7. Java版知识付费源码 Spring Cloud+Spring Boot+Mybatis+uniapp+前后端分离实现知识付费平台

    提供职业教育.企业培训.知识付费系统搭建服务.系统功能包含:录播课.直播课.题库.营销.公司组织架构.员工入职培训等. 提供私有化部署,免费售后,专业技术指导,支持PC.APP.H5.小程序多终端同步 ...

  8. Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI

    鸿鹄云架构[系统管理平台]是一个大型 企业.分布式.微服务.云架构的JavaEE体系快速研发平台,基于 模块化.微服务化.原子化.热部署的设计思想,使用成熟领先的无商业限制的主流开源技术 (Sprin ...

  9. java电子商务系统源码 Spring MVC+mybatis+spring cloud+spring boot+spring security

    鸿鹄云商大型企业分布式互联网电子商务平台,推出PC+微信+APP+云服务的云商平台系统,其中包括B2B.B2C.C2C.O2O.新零售.直播电商等子平台. 分布式.微服务.云架构电子商务平台 java ...

  10. (九)Java版Spring Cloud B2B2C o2o鸿鹄云商平台--平台管理通用设置

    上一篇文章我们介绍了<鸿鹄云商b2b2c o2o平台管理功能清单>,从今天开始,我们对当前清单中的每一个功能进行剖析,会将产品设计.UI设计.功能设计详细给大家讲解,也希望能够帮助更多的朋 ...

最新文章

  1. Python使用pandas保存csv文件:如果文件存在则只添加内容(append),如果无表则同时写入表头和内容(write)
  2. 安卓应用程序插件化开发框架 -AAP Framework
  3. pyhanlp 提取关键词、自动摘要
  4. Cortex-M3-中断/异常的响应序列
  5. vue 按钮多次点击重复提交数据
  6. android 辅助功能_辅助功能简介
  7. 线程的语法 (event,重要)
  8. 模态对话框和全选反选
  9. php5.3+for+linux,Centos 安装 nginx + php5.3
  10. TQ210 —— NandFlash
  11. 设计模式_3_建造者模式
  12. Orchard源码分析(5.3):EndRequest事件处理(DefaultOrchardHost.EndRequest方法)
  13. dapper 调用 存储过程返回值_数据库事务、存储过程、函数以及触发器之间的区别和联系...
  14. 长虹新一代人工智能电视Q6A、Q6K发布,搭载远场语音识别
  15. 开心消消乐简单的逆向破解过程
  16. [CISCN2019 总决赛 Day2 Web1]Easyweb
  17. KTV评分系统实现总结
  18. zk服务启动报错:Unexpected exception, exiting abnormally.java.io.IOException:
  19. 如何在电脑上看微信阅读
  20. 安全模式删除入网小助手_入网小助手 监控

热门文章

  1. Skyline Web 二次开发- 1.地图显示、Position、AttachEvent
  2. brave+kafka+zipkin+cassandra搭建分布式链路跟踪系统
  3. 大话设计の设计原理摘要(二)
  4. [Asp.net]缓存简介
  5. ASIHTTPRequest
  6. 虚方法和重写方法的继承特性
  7. [转]VS .NET 2005中水晶报表的打包部署
  8. 文件夹里没有index.html,gatsby-cli建立后没有index.html文件吗?
  9. 阻塞和非阻塞(串口自环测试失败原因定位)
  10. Linux进程地址空间布局