SpringBoot之MultipartFile文件上传(6)
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"/>  
<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)的更多相关文章
- SpringBoot项目实现文件上传和邮件发送
		
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
 - Springboot如何启用文件上传功能
		
网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...
 - SpringBoot+BootStrap多文件上传到本地
		
1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...
 - SpringBoot之KindEditor文件上传
		
后端核心代码如下: package com.blog.springboot.controller; import java.io.BufferedOutputStream; import java.i ...
 - springboot+vue实现文件上传
		
https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: ...
 - SpringBoot: 6.文件上传(转)
		
1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
 - Springboot(九).多文件上传下载文件(并将url存入数据库表中)
		
一. 文件上传 这里我们使用request.getSession().getServletContext().getRealPath("/static")的方式来设置文件的存储 ...
 - springboot升级导致文件上传自动配置/tmp目录问题解决
		
1,..\web\src\main\resources\spring\web-men-applicationContext.xml 保留原有的bean配置 <bean id="mult ...
 - SpringMVC实现 MultipartFile 文件上传
		
1. Maven 工程引入所需要的依赖包 2. 页面需要开放多媒体标签 3. 配置文件上传试图解析器 4. 接收图片信息,通过 IO 流写入磁盘(调用解析其中的方法即可) 如下: 1.1 引入所依赖的 ...
 
随机推荐
- Jmeter扩展组件开发(5) - 初始化方法的作用与实现
			
CODE //URLNAME 就是在图形化界面当中显示的变量名称private static final String URLNAME = "URL";//设置界面当中默认显示的变 ...
 - 一文让你彻底理解SQL的子查询
			
什么是子查询 当一个查询是另一个查询的条件时,称之为子查询. 为什么要使用子查询 在SELECT.INSERT.UPDATE或DELETE命令中只要是表达式的地方都可以包含子查询,子查询甚至可以包含在 ...
 - js正则常用方法
			
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>W3 ...
 - 『Python』matplotlib初识
			
1. 核心原理 使用matplotlib绘图的原理,主要就是理解figure(画布).axes(坐标系).axis(坐标轴)三者之间的关系. 下面这幅图更详细: 以"美院学生张三写生画画&q ...
 - P5445-[APIO2019]路灯【set,树状数组套线段树】
			
正题 题目链接:https://www.luogu.com.cn/problem/P5445 题目大意 \(n+1\)个点,\(i\)和\(i+1\)个点之间有一条边,\(q\)个操作 断开/连接第\ ...
 - CF438E-The Child and Binary Tree【生成函数】
			
正题 题目链接:https://www.luogu.com.cn/problem/CF438E 题目大意 每个节点有\(n\)个权值可以选择,对于\(1\sim m\)中的每个数字\(k\),求权值和 ...
 - R7000 电脑调整亮度
			
R7000 电脑亮度太亮,想调整亮度,fn+F5,F6 不起作用,需要调整显卡的设置
 - 微服务架构理论&SpringCloud
			
一.什么是微服务? 微服务是一种程序架构模式,它提倡将单体应用程序划分成若干的小服务模块,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制 ...
 - Pytorch学习2020春-1-线性回归
			
线性回归 主要内容包括: 线性回归的基本要素 线性回归模型从零开始的实现 线性回归模型使用pytorch的简洁实现 线性回归的基本要素 模型 为了简单起见,这里我们假设价格只取决于房屋状况的两个因素, ...
 - Linux查找运行程序主目录
			
1.查看程序所在PID netstat -lntup 2.根据PID查找程序所在目录 ll /proc/PID/exe 3.查找程序配置路径 /proc/PID/exe -t