Spring Boot—04文件上传
package com.smartmap.sample.ch1.controller.view; import java.io.File;
import java.io.IOException; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import com.smartmap.sample.ch1.service.UserService; @Controller
@RequestMapping("/system/page/user")
public class UserViewController {
@Autowired
UserService userService; @Autowired
private Environment environment; @GetMapping("/list.html")
public String userList(Model model) {
String osName = System.getProperty("os.name");
model.addAttribute("name", "hello world");
model.addAttribute("host", osName);
return "user/list";
} @GetMapping("/userdetail.html")
public String userdetail(String id) {
return "admin/userInfo.jsp";
} /**
* 文件上传
*
* curl -XPOST 'http://127.0.0.1:8080/system/page/user/upload.html' -F
* "multipartFile=@/D/Project/JavaWeb/SpringBoot/01HelloSpringBoot/readme.txt"
* -F "name=123456789"
*
* curl -XPOST 'http://127.0.0.1:8080/system/page/user/upload.html' -F
* "multipartFile=@readme.txt;type=application/octet-stream" -F "name=123456789"
*
* @param name
* @param multipartFile
* @return
* @throws IllegalStateException
* @throws IOException
*/
@PostMapping("/upload.html")
@ResponseBody
public String handleFormUpload(String name, MultipartFile multipartFile) throws IllegalStateException, IOException {
String resultMessage = "{success: false, message: 'upload fail'}";
System.out.println(multipartFile);
if (!multipartFile.isEmpty()) {
String fileName = multipartFile.getOriginalFilename();
// InputStream is = multipartFile.getInputStream();
String fileSavePath = environment.getProperty("file.upload.path", "");
if (!fileSavePath.equals("")) {
File file = new File(fileSavePath + java.io.File.separator + fileName);
if (file.exists()) {
file.delete();
}
multipartFile.transferTo(file);
resultMessage = "{success: true, message: 'upload success'}";
}
}
return resultMessage;
} }
application.properties
file.upload.path=D:/Project/JavaWeb/SpringBoot/01HelloSpringBoot/fileUpLoad spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.location=D:/Project/JavaWeb/SpringBoot/01HelloSpringBoot/temp
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.resolve-lazily=false
Spring Boot—04文件上传的更多相关文章
- Spring Boot入门——文件上传与下载
1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)
一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...
- spring boot实现文件上传下载
spring boot 引入”约定大于配置“的概念,实现自动配置,节约了开发人员的开发成本,并且凭借其微服务架构的方式和较少的配置,一出来就占据大片开发人员的芳心.大部分的配置从开发人员可见变成了相对 ...
- Spring Boot 教程 - 文件上传下载
在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...
- spring boot Tomcat文件上传找不到零时文件夹
springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...
- Spring Boot RestTemplate文件上传
@ResponseBody @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public St ...
- Spring boot设置文件上传大小限制
原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...
- Spring Boot 在接收上传文件时,文件过大异常处理问题
Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...
- Spring +SpringMVC 实现文件上传功能。。。
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
随机推荐
- iOS-UIScrollView内容复用【实现两个试图的复用】
前言 这里说的内容复用,是指添加到 ScrollView 里面的试图是同一个模型:比如,我需要在 ScrollView 上添加100个 xkView(其他封装好的VC.UIView),每次滑动 Scr ...
- liunx相关指令
修改网卡命名规范 a 如何进入到救援模式 修改网卡 1.修改配置文件名称 /etc/sysconfig/network-scripts/ 名称为:ifcfg-xxx 2.修改配置文件内的 dev ...
- 解决ssh/scp报错:Someone could be eavesdropping on you right now (man-in-the-middle attack)!
主要现象:ssh/scp 失败,host key verification failed. # scp /home/iso/********.iso root@192.168.1.***:/home/ ...
- 【Java并发编程】:并发新特性—信号量Semaphore
在操作系统中,信号量是个很重要的概念,它在控制进程间的协作方面有着非常重要的作用,通过对信号量的不同操作,可以分别实现进程间的互斥与同步.当然它也可以用于多线程的控制,我们完全可以通过使用信号量来自定 ...
- Fiddler Web Debugger的代理功能(图文详解)
不多说,直接上干货! Fiddler的大部分功能都是在其作为本地代理的基础上实现的,如上面介绍的原理图一样,如果想实现数据包截断功能必须要设置为代理,它的代理功能设置比较简单,Fiddler版本2以后 ...
- 微信小程序开发-概述
微信小程序开发-概述 一.小程序申请&APPID 登录微信平台申请成为小程序开发者,小程序不可直接使用服务号或订阅号的AppID,需要登录微信公众平台管理后台,在网站的"设置&quo ...
- Mysql 断电数据损毁恢复
error log: Database page corruption on disk or a failed 处理: /etc/my.cnf 设置 innodb_force_recovery = 6 ...
- springweb flux websocket
直接上代码: import org.springframework.stereotype.Component; import org.springframework.web.reactive.sock ...
- docker 创建jdk镜像
基于上一个创建的基础镜像, wenbronk/centos Dockerfile ############################################ # version : we ...
- jQuery操纵cookie(原生javascript处理cookie)
jQuery也是可以操作cookie的 1.首先下载jQuery.js 以及 jquery.cookie.js 这两个文件 2.安装(其实就是引用) <html> <he ...