1、静态文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/v1/upload">
文件:<input type="file" name="fileName"/></br></br>
备注:<input type="text" name="remark"/>&nbsp;&nbsp;
<input type="submit" value="上传"/>
</form>
</body>
</html>

2、return result

package cn.xiaobing.demo.pojo;

public class Result {
private int code;
private Object data;
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Result(int code, Object data, String msg) {
super();
this.code = code;
this.data = data;
this.msg = msg;
}
public Result() {
super();
}
@Override
public String toString() {
return "Result [code=" + code + ", data=" + data + ", msg=" + msg + "]";
}
}

3、FileController

package cn.xiaobing.demo.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import cn.xiaobing.demo.pojo.Result; @Controller
@PropertySource(value = { "application.properties" })//指定配置文件
public class FileController { @Value("${web.upload.filepath}")//获取配置文件中的配置参数
private String filePath;
// private static final String filePath = "C:/oneself/eclipse-workspace/springboot-v0/src/main/resources/static/images"; @RequestMapping(value="/v1/upload")
@ResponseBody
public Object upload(@RequestParam("fileName") MultipartFile file,HttpServletRequest request) {
String remark = request.getParameter("remark");//备注信息
String filename = file.getOriginalFilename();//获取文件名称
String suffixname = filename.substring(filename.lastIndexOf("."));//后缀
filename = UUID.randomUUID() + suffixname;//文件上传后重命名数据库存储
File dest = new File(filePath,filename);
Map<String, String> data = new HashMap<String, String>();
data.put("filename", filename);
data.put("备注 ", remark);
try {
//MultipartFile对象的transferTo方法用于文件的保存(效率和操作比原来用的FileOutputStream方便和高效)
file.transferTo(dest);//拷贝文件到指定路径储存
return new Result(0, data, "上传成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(-1, data, "上传失败");
}
}
}

4、启动项目

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)

5、访问upload.html上传文件

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

7、点击上传,上传失败

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

package cn.xiaobing.demo;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class XiaoBingApplication {
public static void main(String[] args) {
SpringApplication.run(XiaoBingApplication.class,args);
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("10240KB");//设置上传单个文件最大10M
factory.setMaxRequestSize("102400KB");//设置上传文件总数据最大100M
return factory.createMultipartConfig();
}
}

9、maven打包执行

(1) pom.xml引入依赖

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.874 s
[INFO] Finished at: 2020-06-29T23:44:45+08:00
[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. 接口管理工具swagger

    swagger,一款致力于解决接口规范化.标准化.文档化的开源库,一款真正的开发神器. swagger三大部分 Editor https://swagger.io/tools/swagger-edit ...

  2. 浅析Java中的static关键字

    关键点 <Java编程思想>对static方法的描述:"static方法就是没有this的方法.在static方法内部不能调用非静态方法,反过来是可以的.而且可以在没有创建对象的 ...

  3. genymotion从本地拖拽apk到模拟器失败,报错“An error occured while deploying the file……”-解决方案

    前两篇已经讲过genymotion的安装了,但genymotion构建的安卓模拟器的界面比较简洁,什么软件都没.那么我们进行测试之前,先将需要测试的apk安装到模拟器中,一般来说,直接将apk文件从本 ...

  4. 鸿蒙内核源码分析(异常接管篇) | 社会很单纯 , 复杂的是人 | 百篇博客分析OpenHarmony源码 | v39.03

    百篇博客系列篇.本篇为: v39.xx 鸿蒙内核源码分析(异常接管篇) | 社会很单纯,复杂的是人 | 51.c.h .o 硬件架构相关篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU ...

  5. P5056-[模板]插头dp

    正题 题目链接:https://www.luogu.com.cn/problem/P5056 题目大意 \(n*m\)的网格,求有多少条回路可以铺满整个棋盘. 解题思路 插头\(dp\)的,写法是按照 ...

  6. Douban Top 250爬虫

    # Ref: https://fishc.com.cn/forum.php?mod=viewthread&tid=101887&extra=page%3D1%26filter%3Dty ...

  7. Java8通过Function获取字段名(获取实体类的字段名称)

    看似很鸡肋其实在某些特殊场景还是比较有用的.比如你将实体类转Map或者拿到一个Map结果的时候,你是怎么获取某个map的key和value.方法一:声明 String key1="name& ...

  8. 从零入门 Serverless | 一文详解 Serverless 技术选型

    作者 | 李国强 阿里云资深产品专家 今天来讲,在 Serverless 这个大领域中,不只有函数计算这一种产品形态和应用类型,而是面向不同的用户群体和使用习惯,都有其各自适用的 Serverless ...

  9. NOIP 模拟 七十七

    100+60+95+30; T4 一个变量打错挂了40.. T1 最大或 考虑从高到低枚举的二进制位,然后和的对应二进制位进行比较.如果两 者相同,那么不论怎么选择,,答案在这个位置上的值一定和在这个 ...

  10. 题解 ABC216H Random Robots

    link Solution 考虑一个不合法方案,它一定最后位置的逆序对数不为 \(0\),而且可以发现的是,存在对称方案使得最后逆序对数奇偶性不同,所以我们如果加上 \((-1)\)^{\sigma( ...