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 ...
随机推荐
- LC-BLSTM结构快速解读
参考文献如下: (1) A Context-Sensitive-Chunk BPTT Approach to Training Deep LSTM/BLSTM Recurrent Neural Net ...
- POJ 2376
#include<iostream>//by chengdacaizi. #include<stdio.h> #define MAXN 25005 using namespac ...
- POJ 1102
#include<iostream>// cheng da cai zi 11.14 using namespace std; int main() { int i; int j; int ...
- 使用图片预加载,解决断网后无法从后台获取提示网络异常的logo图片的问题
项目中有需求,断网后,显示小提示窗,里面包含网络异常提示语和异常小logo图片. 在实际操作时,遇到,断网后,无法从后台获取异常小logo图片. 我是才用图片预加载的方法解决这个问题的,解决方法如下: ...
- Postman 发送 Bearer token
Bearer Token (RFC 6750) 用于OAuth 2.0授权访问资源,任何Bearer持有者都可以无差别地用它来访问相关的资源,而无需证明持有加密key.一个Bearer代表授权范围.有 ...
- 课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 2、10个测验题
1.What does the analogy “AI is the new electricity” refer to? (B) A. Through the “smart grid”, AI i ...
- Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因
转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...
- Explorer内存占用偶尔变高导致卡顿
症状: 打开 "这台电脑",加载缓慢.此时查看任务管理器,explorer内存可能飙升到几G.cpu也很高 创建和删除文件缓慢,删除单个文件也会出现进度条.此时查看任务管理器,会出 ...
- heroku 部署ruby项目后 未连接数据库显示(We're sorry, but something went wrong. If you are the application owner )
如何部署请参照: http://blog.csdn.net/xz360717118/article/details/62422741 部署后如果发现显示:We're sorry, but someth ...
- JAVA 垃圾笔记一溜堆
精度只能从低精度 转到高精度.例如:float = 3.4;错误 默认小数在JAVA中是double. 即:从double高精度转到floag低精度错误!!将字符char加减乘除,默认对ASCII码运 ...