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. js delete删除对象属性,delete删除不了变量及原型链中的变量

    js delete删除对象属性,delete删除不了变量及原型链中的变量 一.delete删除对象属性 function fun(){ this.name = 'gg'; } var obj = ne ...

  2. 使用TopShelf做windows服务

    class Program { static void Main(string[] args) { HostFactory.Run(x => { x.RunAsLocalSystem(); x. ...

  3. Java之BigDecimal

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6963836.html 1.前言 编程人员都应该知道计算机计算浮点数是不精确的,结果是近似数值,当然具体值还是和计 ...

  4. dubbo学习(1)--简单的入门搭建实例

    转载请注明源文出处:http://www.cnblogs.com/lighten/p/6828026.html 1 简介 dubbo是一个分布式服务框架,由阿里巴巴的工程师开发,致力于提供高性能和透明 ...

  5. 【Java并发编程】:并发新特性—障碍器CyclicBarrier

    CyclicBarrier(又叫障碍器)同样是Java5中加入的新特性,使用时需要导入Java.util.concurrent.CylicBarrier.它适用于这样一种情况:你希望创建一组任务,它们 ...

  6. Postman—添加断言和检查点

    前言 postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 断言会在请求返回之后,运行,并根据断言的pass\fail情况体现在最终测试结果中. 一.断言步骤 ...

  7. 转载:Spring学习总结

    地址:http://www.cnblogs.com/best/tag/Spring/

  8. HUE配置文件hue.ini 的hdfs_clusters模块详解(图文详解)(分HA集群和非HA集群)

    不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...

  9. 一款高效视频播放控件的设计思路(c# WPF版)

    因工作的需要,开发了一款视频播放程序.期间也经历许多曲折,查阅了大量资料,经过了反复测试,终于圆满完成了任务. 我把开发过程中的一些思路.想法写下来,以期对后来者有所帮助. 视频播放的本质 就是连续的 ...

  10. sersync+rsync=实时异步备份

    环境准备 服务器两台 rsync-server:192.168.1.8  (备份服务器) sersync-node1:192.168.1.9 (需要备份的服务器) 系统 CentOS7.4 关闭防火墙 ...