一、案例  

  1.1  配置application.properties

#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev
server.port=8888 logging.config=classpath:log4j2-dev.xml
spring.mvc.view.prefix: /WEB-INF/templates/
spring.mvc.view.suffix: .jsp
  • spring.http.multipart.enabled=true #默认支持文件上传.
  • spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘.
  • spring.http.multipart.location= # 上传文件的临时目录
  • spring.http.multipart.max-file-size=1Mb # 最大支持文件大小
  • spring.http.multipart.max-request-size=10Mb # 最大支持请求大小

  1.2  编写IndexController

  • 该控制器只用于处理 “/” 和“/index”请求,使之跳转到index.jsp页面
package com.shyroke.controller;

import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/")
public class IndexController { @RequestMapping(value="index")
public String index() {
return "index";
} @RequestMapping()
public String index2() {
return "index";
}
}

  1.3  编写index.jsp

<body>
<form method="POST" enctype="multipart/form-data" action="/file/upload">
文件:<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
</body>

  1.4  编写FileController

package com.shyroke.controller;

import java.io.File;
import java.io.IOException; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping(value = "/file")
public class FileController { private static final Logger logger = LoggerFactory.getLogger(FileController.class); @RequestMapping(value = "upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "文件为空";
} // 获取文件名
String fileName = file.getOriginalFilename();
logger.info("上传的文件名为:" + fileName); // 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
logger.info("上传的后缀名为:" + suffixName); // 文件上传路径
String filePath = "E://"; // 解决中文问题,liunx下中文路径,图片显示问题
// fileName = UUID.randomUUID() + suffixName; File dest = new File(filePath + fileName); // 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
} try {
file.transferTo(dest);
return "上传成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败";
} }

  1.5  结果

(十一)SpringBoot之文件上传以及的更多相关文章

  1. SpringBoot图文教程4—SpringBoot 实现文件上传下载

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  2. SpringBoot 整合文件上传 elment Ui 上传组件

    SpringBoot 整合文件上传 elment Ui 上传组件 本文章记录 自己学习使用 侵权必删! 前端代码 博主最近在学 elment Ui 所以 前端使用 elmentUi 的 upload ...

  3. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

  4. SpringBoot(3) 文件上传和访问

    springboot文件上传 MultipartFile file,源自SpringMVC MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutS ...

  5. SpringBoot的文件上传

    先在src/main/resources下新建一个static目录用以存放html页面,简单的html页面如下 <!DOCTYPE html> <html> <head& ...

  6. springBoot的文件上传功能

    知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/det ...

  7. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  8. Angular14 利用Angular2实现文件上传的前端、利用springBoot实现文件上传的后台、跨域问题

    一.angular2实现文件上传前端 Angular2使用ng2-file-upload上传文件,Angular2中有两个比较好用的上传文件的第三方库,一个是ng2-file-upload,一个是ng ...

  9. springboot 修改文件上传大小限制

    springboot 1.5.9文件上传大小限制spring:http:multipart:maxFileSize:50MbmaxRequestSize:50Mb springboot 2.0文件上传 ...

随机推荐

  1. JavaWeb基础知识

    一.WEB基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web ...

  2. arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634

    学习:http://zhihu.esrichina.com.cn/article/634使用光标和内存中的要素类将数据加载到要素集 import arcpy arcpy.env.overwriteOu ...

  3. 你了解 Virtual DOM 吗?解释一下它的工作原理

    Virtual DOM 是一个轻量级的 JavaScript 对象,它最初只是 real DOM 的副本.它是一个节点树,它将元素.它们的属性和内容作为对象及其属性. React 的渲染函数从 Rea ...

  4. LC 990. Satisfiability of Equality Equations

    Given an array equations of strings that represent relationships between variables, each string equa ...

  5. Ionic4.x Theming(主题) 增加内置主题 颜色 修改内置组件默认样式 修改底部 Tabs 背景颜色以及按钮颜色

    1.Ionic4.x Theming(主题) Ionic4.x 修改主题颜色的话需要在 src/theme/variables.scss 文件中修改. https://ionicframework.c ...

  6. Bitmap之extractAlpha函数抽取alpha值

    package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...

  7. 41 Flutter 仿京东商城项目签名验证 增加收货地址、显示收货地址 事件广播

    加群452892873 下载对应41课文件,运行方法,建好项目,直接替换lib目录 AddressAdd.dart import 'package:dio/dio.dart'; import 'pac ...

  8. SSM配置基于注解AOP

    pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...

  9. 一百四十四:CMS系统之评论布局和功能二

    在base页加一个登录标识符 给加页面两个id,方便取值 js $(function () { //初始化ueditor var ue = UE.getEditor('editor', { 'serv ...

  10. 123457123457---com.threeapp.ShuiShiYanLiWang01----谁是眼力王

    com.threeapp.ShuiShiYanLiWang01----谁是眼力王