004 springboot文件上传
关于文件上传,在spring cloud会再经过配置文件的处理,在spring boot则不需要,在这里写一个文件上传的接口。
单文件上传,如果以后写多文件上传再进行补充。
1.文件目录

2.控制器程序
package com.jun.file.controller; import com.jun.file.service.UpLoadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; @RestController
public class UpLoadFileController {
@Autowired
private UpLoadFileService upLoadFileService;
@PostMapping("/upload/img")
public String uploadImg(@RequestParam("file") MultipartFile file){
int count=upLoadFileService.upLoadImg(file);
return "success";
} }
3.服务方法
package com.jun.file.service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.io.IOException;
import java.util.UUID; /**
* 这个类用于文件的上传
*/
@Service
public class UpLoadFileService {
public int upLoadImg(MultipartFile file){
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
String name = UUID.randomUUID().toString();
fileName = name+suffixName;
String path = "E:/javaImg/test/";
String path_img = path+fileName;
File dest = new File(path_img);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
4.测试

004 springboot文件上传的更多相关文章
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- SpringBoot 文件上传临时文件路径问题
年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...
- springboot文件上传下载简单使用
springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- Springboot 文件上传(带进度条)
1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
随机推荐
- [nginx] nginx源码分析--SNI性能分析
概念 我们已经知道什么是SNI,以及如何为用户配置SNI. [nginx] nginx使用SNI功能的方法 问题 通过观察配置文件,可以发现,针对每一个SSL/TLS链接, nginx都会动态的查找( ...
- CentOS7编译安装httpd-2.4.41 php7.3
CentOS7编译安装httpd-2.4.41 php7.3 安装参考环境: CentOS Linux release 7.5.1804 (Core) 一.安装依赖包 httpd安装的依赖包 # yu ...
- Windows运维之Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机
摘要:本文主要向大家介绍了Windows运维之Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机,通过具体的内容向大家展现,希望对大家学习Windows运维有所帮助. 本 ...
- 2013.4.23 - KDD第五天
今天晚上郭宇航师兄从外面回来问我那天找他什么事,然后我们就开始讨论KDD的第一个题目,其实第一个题目跟郭师兄的课题不太相关,本来想问他关于语义消 岐的那道题(第二道),不过第二题的内容我给忘了,然后我 ...
- 思考---(科研99% )VS (产品75%)
转目前人脸识别技术的挑战是什么? - 知乎 标签: | 发表时间:-- : | 作者: 出处:https://www.zhihu.com 也是放假太闲,上知乎来锻炼一下手指. 在回答题主的问题的时候, ...
- vue-(过滤器,钩子函数,路由)
1.局部过滤器 在当前组件内部使用过滤器,修饰一些数据 //声明 filters:{ '过滤器的名字':function(val,a,b){ //a 就是alax ,val就是当前的数据 } } // ...
- 用1 x 2的多米诺骨牌填满M x N矩形的方案数(完美覆盖)
题意 用 $1 \times 2$ 的多米诺骨牌填满 $M \times N$ 的矩形有多少种方案,$M \leq 5,N < 2^{31}$,输出答案模 $p$. 分析 当 $M=3$时,假设 ...
- com.alibaba.fastjson把JSONObject转换为Map<String, String>对象
https://www.cnblogs.com/fomeiherz/p/6351287.html JSONObject obj = new JSONObject();{obj.put("ke ...
- [Schematics] 1. Copy and Manipulate Template
1. Create a src/my-component/files/src/app directory to hold your templates. mkdir -p src/my-compone ...
- Problem 4 dp
$des$ 小 $Y$ 十分喜爱光学相关的问题, 一天他正在研究折射.他在平面上放置了 $n$ 个折射装置, 希望利用这些装置画出美丽的折线.折线将从某个装置出发, 并且在经过一处装置时可以转向, 若 ...