1、静态文件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>文件上传</title>
  6. </head>
  7. <body>
  8. <form enctype="multipart/form-data" method="post" action="/v1/upload">
  9. 文件:<input type="file" name="fileName"/></br></br>
  10. 备注:<input type="text" name="remark"/>&nbsp;&nbsp;
  11. <input type="submit" value="上传"/>
  12. </form>
  13. </body>
  14. </html>

2、return result

  1. package cn.xiaobing.demo.pojo;
  2.  
  3. public class Result {
  4. private int code;
  5. private Object data;
  6. private String msg;
  7. public int getCode() {
  8. return code;
  9. }
  10. public void setCode(int code) {
  11. this.code = code;
  12. }
  13. public Object getData() {
  14. return data;
  15. }
  16. public void setData(Object data) {
  17. this.data = data;
  18. }
  19. public String getMsg() {
  20. return msg;
  21. }
  22. public void setMsg(String msg) {
  23. this.msg = msg;
  24. }
  25. public Result(int code, Object data, String msg) {
  26. super();
  27. this.code = code;
  28. this.data = data;
  29. this.msg = msg;
  30. }
  31. public Result() {
  32. super();
  33. }
  34. @Override
  35. public String toString() {
  36. return "Result [code=" + code + ", data=" + data + ", msg=" + msg + "]";
  37. }
  38. }

3、FileController

  1. package cn.xiaobing.demo.controller;
  2.  
  3. import java.io.File;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.UUID;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9.  
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.context.annotation.PropertySource;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.ResponseBody;
  16. import org.springframework.web.multipart.MultipartFile;
  17.  
  18. import cn.xiaobing.demo.pojo.Result;
  19.  
  20. @Controller
  21. @PropertySource(value = { "application.properties" })//指定配置文件
  22. public class FileController {
  23.  
  24. @Value("${web.upload.filepath}")//获取配置文件中的配置参数
  25. private String filePath;
  26. // private static final String filePath = "C:/oneself/eclipse-workspace/springboot-v0/src/main/resources/static/images";
  27.  
  28. @RequestMapping(value="/v1/upload")
  29. @ResponseBody
  30. public Object upload(@RequestParam("fileName") MultipartFile file,HttpServletRequest request) {
  31. String remark = request.getParameter("remark");//备注信息
  32. String filename = file.getOriginalFilename();//获取文件名称
  33. String suffixname = filename.substring(filename.lastIndexOf("."));//后缀
  34. filename = UUID.randomUUID() + suffixname;//文件上传后重命名数据库存储
  35. File dest = new File(filePath,filename);
  36. Map<String, String> data = new HashMap<String, String>();
  37. data.put("filename", filename);
  38. data.put("备注 ", remark);
  39. try {
  40. //MultipartFile对象的transferTo方法用于文件的保存(效率和操作比原来用的FileOutputStream方便和高效)
  41. file.transferTo(dest);//拷贝文件到指定路径储存
  42. return new Result(0, data, "上传成功");
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. return new Result(-1, data, "上传失败");
  46. }
  47. }
  48. }

4、启动项目

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.0.1.RELEASE)

5、访问upload.html上传文件

6、点击上传,上传文件成功

7、点击上传,上传失败

8、设置上传文件大小限制,编码如下,在启动类中添加@Bean方法

  1. package cn.xiaobing.demo;
  2. import javax.servlet.MultipartConfigElement;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.web.servlet.MultipartConfigFactory;
  6. import org.springframework.context.annotation.Bean;
  7.  
  8. @SpringBootApplication
  9. public class XiaoBingApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(XiaoBingApplication.class,args);
  12. }
  13. @Bean
  14. public MultipartConfigElement multipartConfigElement() {
  15. MultipartConfigFactory factory = new MultipartConfigFactory();
  16. factory.setMaxFileSize("10240KB");//设置上传单个文件最大10M
  17. factory.setMaxRequestSize("102400KB");//设置上传文件总数据最大100M
  18. return factory.createMultipartConfig();
  19. }
  20. }

9、maven打包执行

(1) pom.xml引入依赖

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

(2)打jar包-项目右键-Run As-Maven Install

  1. [INFO] ------------------------------------------------------------------------
  2. [INFO] BUILD SUCCESS
  3. [INFO] ------------------------------------------------------------------------
  4. [INFO] Total time: 45.874 s
  5. [INFO] Finished at: 2020-06-29T23:44:45+08:00
  6. [INFO] ------------------------------------------------------------------------

(3)启动项目

(4) Test

10、不足之处,后续优化。。。

SpringBoot之MultipartFile文件上传(6)的更多相关文章

  1. SpringBoot项目实现文件上传和邮件发送

    前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...

  2. Springboot如何启用文件上传功能

    网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...

  3. SpringBoot+BootStrap多文件上传到本地

    1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...

  4. SpringBoot之KindEditor文件上传

    后端核心代码如下: package com.blog.springboot.controller; import java.io.BufferedOutputStream; import java.i ...

  5. springboot+vue实现文件上传

    https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: ...

  6. SpringBoot: 6.文件上传(转)

    1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  7. Springboot(九).多文件上传下载文件(并将url存入数据库表中)

    一.   文件上传 这里我们使用request.getSession().getServletContext().getRealPath("/static")的方式来设置文件的存储 ...

  8. springboot升级导致文件上传自动配置/tmp目录问题解决

    1,..\web\src\main\resources\spring\web-men-applicationContext.xml 保留原有的bean配置 <bean id="mult ...

  9. SpringMVC实现 MultipartFile 文件上传

    1. Maven 工程引入所需要的依赖包 2. 页面需要开放多媒体标签 3. 配置文件上传试图解析器 4. 接收图片信息,通过 IO 流写入磁盘(调用解析其中的方法即可) 如下: 1.1 引入所依赖的 ...

随机推荐

  1. pycharm 增删改查 mysql数据库

    1.pycharm创建mysql数据表######################################################import pymysql# 创建连接con = p ...

  2. 事项同步事项编码(mt_code)长度超过数据库限制的varchar(32)线上问题

    改下长度限制重新同步下,可以恢复正常!

  3. 微信小程序自动化测试

    使用官方工具 使用webview测试方法,当2019年被微信封禁 使用native定位

  4. python学习笔记(一)-基础知识

    O.解释型语言和编译型语言 编译型语言就是先把写好的程序翻译成计算机语言然后执行,就是所谓的一次编译到处运行,比如c.c++就是编译型语言,这样的语言特点是运行速度快,但是需要事先把程序编译好才可以. ...

  5. Java对象构造

    关于对象构造的一些认识. 默认域初始化 如果在构造器中没有显示地给域赋予初值,那么就会被自动地赋予默认值:数值为0,布尔值为false,对象引用为null.然而,这显然是不安全的,在一个null引用上 ...

  6. Dockerfile 的常用参数注解和范例

    一. docker hello world 1.1 Dockerfile FROM centos:7.5.1804 MAINTAINER 11@qq.com CMD echo "hello ...

  7. P6329-[模板]点分树 | 震波

    正题 题目链接:https://www.luogu.com.cn/problem/P6329 解题思路 给出\(n\)个点的一棵树,每个点有权值,有\(m\)次操作 修改一个点\(x\)的权值为\(y ...

  8. P7518-[省选联考2021A/B卷]宝石【主席树,二分】

    正题 题目链接:https://www.luogu.com.cn/problem/P7518 题目大意 给出\(n\)个点的一棵树,每个点上有不大于\(m\)的数字. 然后给出一个长度为\(c\)的各 ...

  9. Liunx下Mysql,MongoDB性能优化的配置

    场景 这几天在赶十一上线的项目,但是突然发现接口性能不好,高并发支持不住.又不想改代码,就在数据库层面进行优化. Mysql 分区:项目中有对40万条的数据进行时间查询的要求,就算对DateTime建 ...

  10. 实验4:开源控制器实践——OpenDaylight

    实验4:开源控制器实践--OpenDaylight 一.实验目的 能够独立完成OpenDaylight控制器的安装配置: 能够使用Postman工具调用OpenDaylight API接口下发流表. ...