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. Java爬虫——Gecco简单入门程序(根据下一页一直爬数据)

    为了完成作业,所以学习了一下爬虫Gecco,这个爬虫集合了以往所有的爬虫的特点,但是官方教程中关于Gecco的教程介绍的过于简单,本篇博客是根据原博客的地址修改的,原博客中只有程序的截图,而没有给出一 ...

  2. webpack处理url资源的配置

    webpack处理url资源的配置 1.安装 npm i url-loader -D 2.修改webpack.config.js const path = require('path'); // 启用 ...

  3. ES6 箭头函数 this 指向

    ES6 箭头函数 this 指向 箭头函数有几个使用注意点: 函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. 不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个 ...

  4. C/C++ -- Gui编程 -- Qt库的使用 -- 对话框QDialog

    模态对话框 -----源文件main.cpp(工程QtDialog)----- #include "qtdialog.h" #include <QApplication> ...

  5. 4-nginx-反向代理到tomcat及负载均衡

    反向代理相比于正向代理, 比如使用搬瓦工时, 就是位于客户端的正想代理, 而反向代理则是服务器端的代理, 主要用于实现请求分发, 负载均衡等功能 正向代理推荐一个: 搬瓦工, 比较好用.. 反向代理主 ...

  6. C#的SubString(int start,int end);

    C#的SubString(); 例子: string str = "i am a girl"; string temp = str.Substring(2,2);//从索引2开始, ...

  7. nodeJs的npm报错问题

    1. Failed at the phantomjs-prebuilt@2.1.14 install script 'node install.js'. 解决办法: npm install phant ...

  8. cordova打包APK,SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...

    javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...

  9. sql多行合并成一行用逗号隔开,多表联合查询中子查询取名可重复

    简单版的 SELECT a.CreateBy,Name =stuff((select ','+Name FROM SG_Client WHERE CreateBy = a.CreateBy for x ...

  10. sql中非存储过程定义参数并使用

    DECLARE @dt datetime SET @dt=GETDATE()--1.短日期格式:yyyy-m-d SELECT REPLACE(CONVERT(varchar(10),@dt,120) ...