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文件上传的更多相关文章

  1. Spring Boot入门——文件上传与下载

    1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  2. spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)

    一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...

  3. spring boot实现文件上传下载

    spring boot 引入”约定大于配置“的概念,实现自动配置,节约了开发人员的开发成本,并且凭借其微服务架构的方式和较少的配置,一出来就占据大片开发人员的芳心.大部分的配置从开发人员可见变成了相对 ...

  4. Spring Boot 教程 - 文件上传下载

    在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...

  5. spring boot Tomcat文件上传找不到零时文件夹

    springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...

  6. Spring Boot RestTemplate文件上传

    @ResponseBody @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public St ...

  7. Spring boot设置文件上传大小限制

    原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...

  8. Spring Boot 在接收上传文件时,文件过大异常处理问题

    Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...

  9. Spring +SpringMVC 实现文件上传功能。。。

    要实现Spring +SpringMVC  实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...

随机推荐

  1. WPF一步步开发XMPP IM客户端2:主窗体设计

    UI设计方案: 在设计窗体UI之前,先要了解一些主要的接口和帮助类: 对于主窗的左侧列表,容器内的Item必须实现ILeftItem的接口,比如联系人.系统消息.群等,接口包含点击事件 public ...

  2. constructor 属性返回变量或对象的构造函数。判断是否为日期,数组的例子

    constructor 属性返回变量或对象的构造函数. <!DOCTYPE html> <html> <head> <meta charset="u ...

  3. Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因

    转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...

  4. EF基础知识小记一

    1.EF等ORM解决方案出现的原因 因为软件开发中分析和解决问题的方法已经接近成熟,然后关系型数据库却没有,很多年来,数据依然是保存在表行列这样的模式里,所以,在面相对象和高度标准化的数据库中产生了一 ...

  5. Java学习之路(十二):IO流<三>

    复习:序列流 序列流可以把多个字节输入整合成一个,从序列流中读取到数据时,将从被整合的第一个流开始读取,读完这个后,然后开始读取第二个流,依次向后推. 详细见上一篇文章 ByteArrayOutput ...

  6. rabbitmq实现一台服务器同时给指定部分的consumer发送消息(tp框架)(第六篇)

    previous article:  http://www.cnblogs.com/spicy/p/7989717.html 上一篇学习了,发送消息的时候用direct类型的exchange,绑定不同 ...

  7. Office自动生成目录步骤(非常实用)(图文详解)

    不多说,直接上干货! 结束 欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智能躺过的坑       同时,大家可以关注我的个人博客:    http://www.cnblogs.co ...

  8. Ruby on Rails Mountable vs. Full Engine

    原文 :https://www.travisluong.com/ruby-on-rails-mountable-vs-full-engine/一个Rails Engine 本质是一个 Rails ap ...

  9. CRF两个例子的理解

    概率计算例子: 预测例子:

  10. MySQL decimal unsigned 更新负数不报错却为0

    今天在验证接口的并发问题时,把之前通过 redis 解决的并发压力转移到 mysql 上(redis 在 set 保存数据和数据过期需要去向数据库获取时存在时延,会存在空挡造成大并发多插入数据的风险: ...