2、SpringBoot接口Http协议开发实战8节课(7-8)
7、SpringBoot2.x文件上传实战
简介:讲解HTML页面文件上传和后端处理实战
1、讲解springboot文件上传 MultipartFile file,源自SpringMVC
1)静态页面直接访问:localhost:8080/index.html
注意点:
如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面
2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)
访问路径 http://localhost:8080/images/cdd6ab24-acfd-48b0-a043-6e22812c7afd.jpg
代码示例:
FileController.java:
package net.xdclass.demo.controller; import java.io.File;
import java.io.IOException;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import net.xdclass.demo.domain.JsonData; 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; /**
* 功能描述:文件测试
*
* <p> 创建时间:Apr 22, 2018 11:22:29 PM </p>
*/
@Controller
public class FileController { @RequestMapping(value = "/api/v1/gopage")
public Object index() {
return "index";
} private static final String filePath = "L:/Workspaces/Eclipse_Neon/txkt/SpringBootClass/xdclass_springboot/src/main/resources/static/images/";//末尾需要加/,这样才能写进来 @RequestMapping(value = "upload")
@ResponseBody
public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { // file.isEmpty(); 判断图片是否为空
// file.getSize(); 图片大小进行判断 String name = request.getParameter("name");
System.out.println("用户名:" + name); // 获取文件名
String fileName = file.getOriginalFilename();
System.out.println("上传的文件名为:" + fileName); // 获取文件的后缀名,比如图片的jpeg,png
String suffixName = fileName.substring(fileName.lastIndexOf("."));
System.out.println("上传的后缀名为:" + suffixName); // 文件上传后的路径
fileName = UUID.randomUUID() + suffixName;
System.out.println("转换后的名称:" + fileName); File dest = new File(filePath + fileName); try {
file.transferTo(dest); return new JsonData(0, fileName);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new JsonData(-1, "fail to save ", null);
} }
JsonData.java:
package net.xdclass.demo.domain;
import java.io.Serializable;
public class JsonData implements Serializable {
private static final long serialVersionUID = 1L;
//状态码,0表示成功,-1表示失败
private int code;
//结果
private Object data;
//错误描述
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public JsonData(int code, Object data) {
super();
this.code = code;
this.data = data;
}
public JsonData(int code, String msg,Object data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
}
上传成功

8、jar包方式运行web项目的文件上传和访问处理(核心知识)
简介:讲解SpingBoot2.x使用 java -jar运行方式的图片上传和访问处理
1、文件大小配置,启动类里面配置
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("1024000KB");
return factory.createMultipartConfig();
}
2、打包成jar包,需要增加maven依赖
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果没加相关依赖,执行maven打包,运行后会报错:no main manifest attribute, in XXX.jar
GUI:反编译工具,作用就是用于把class文件转换成java文件
3、文件上传和访问需要指定磁盘路径
application.properties中增加下面配置
1) web.images-path=/Users/jack/Desktop(存储路径)
2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
4、文件服务器:fastdfs,阿里云oss,nginx搭建一个简单的文件服务器
2、SpringBoot接口Http协议开发实战8节课(7-8)的更多相关文章
- 2、SpringBoot接口Http协议开发实战8节课(1-6)
1.SpringBoot2.xHTTP请求配置讲解 简介:SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧 1.@RestController and @RequestMapping是 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_6、SpringBoot2.xHTTP请求配置讲解
1.SpringBoot2.xHTTP请求配置讲解 简介:SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧 1.@RestController and @RequestMapping是 ...
- 小D课堂【SpringBoot】接口Http协议开发实战
---恢复内容开始--- ====================2.SpringBoot接口Http协议开发实战 ============================= 1.SpringBoot ...
- centos mysql 实战 第一节课 安全加固 mysql安装
centos mysql 实战 第一节课 安全加固 mysql安装 percona名字的由来=consultation 顾问+performance 性能=per con a mysql ...
- 11、Logback日志框架介绍和SpringBoot整合实战 2节课
1.新日志框架LogBack介绍 简介:日志介绍和新日志框架Logback讲解 1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等 ...
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- SpringBoot微服务电商项目开发实战 --- api接口安全算法、AOP切面及防SQL注入实现
上一篇主要讲了整个项目的子模块及第三方依赖的版本号统一管理维护,数据库对接及缓存(Redis)接入,今天我来说说过滤器配置及拦截设置.接口安全处理.AOP切面实现等.作为电商项目,不仅要求考虑高并发带 ...
- SpringBoot微服务电商项目开发实战 --- Redis缓存雪崩、缓存穿透、缓存击穿防范
最近已经推出了好几篇SpringBoot+Dubbo+Redis+Kafka实现电商的文章,今天再次回到分布式微服务项目中来,在开始写今天的系列五文章之前,我先回顾下前面的内容. 系列(一):主要说了 ...
随机推荐
- Saddle Point ZOJ - 3955(求每个值得贡献)
题意: 给出一个矩阵,删掉一些行和列之后 求剩下矩阵的鞍点的总个数 解析: 对于每个点 我们可以求出来 它所在的行和列 有多少比它大的 设为a 有多少比它小的 设为b 然后对于那些行和列 都有两种操 ...
- day22 os模块
# 主要用于和当前操作系统做对接的时候获取系统参数用的 import os # os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 # os.chdir("dir ...
- 【HDU5778】abs(数学)
BUPT2017 wintertraining(16) #4 C HDU - 5778 题意 给定x,找出使|y-x|最小,且每个质因子都出现两次的y(\(y\le 2\))50组测试数据,\(1\l ...
- 神奇的操作——线段树合并(例题: BZOJ2212)
什么是线段树合并? 首先你需要动态开点的线段树.(对每个节点维护左儿子.右儿子.存储的数据,然后要修改某儿子所在的区间中的数据的时候再创建该节点.) 考虑这样一个问题: 你现在有两棵权值线段树(大概是 ...
- [CF1132F]Clear the String
题意 给你一个串s,每次可以花费1的代价删去一个子串,要求子串的每一位为同一个字符. 求删去整个串的最小代价. 分析 这是一道非常简单的区间\(DP\)问题,我们定义\(f[i][j]\)表示删去子串 ...
- wireshark配合jmeter测试webservice接口
1.首先,获取本地和接口的ip,以便设置过滤 2.wireshark设置过滤 ip.dst==192.168.0.101 and ip.src==61.147.124.120 and http 3.执 ...
- scp 的用法
scp用于在linux下远程拷贝文件, 与rsync相比,scp不占资源,不会提高多少系统负荷,虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本 ...
- EFCodeFirst快速搭建入门
EFCodeFirst快速搭建入门 1.新建Model类库项目. 添加EntityFramework.dll的引用. 编写实体类Course,Student. namespace EFCodeFirs ...
- A1012. The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- models.DateTimeField(auto_now_add=True) 与 models.DateTimeField(auto_now=True)
DateTimeField和DateField和TimeField存储的内容分别对应着datetime(),date(),time()三个对象. 对于auto_now=False和auto_now_a ...