SpringBoot------文件上传
1.pom.xml引入依赖配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.ytheng</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.添加文件控制器FileController
package top.ytheng.demo.controller; import java.io.File;
import java.io.IOException;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
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; import top.ytheng.demo.entity.JsonData; @Controller
@PropertySource({"classpath:application.properties"})
public class FileController {
@Value("${web.file.path}")
private String filePath; @RequestMapping(value="/api/v1/file")
public Object index() {
return "index";
} @RequestMapping("/upload")
@ResponseBody
public Object upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) {
//判断文件为空
//file.isEmpty();
//判断文件大小
//file.getSize(); String name = request.getParameter("name");
System.out.println("用户名:" + name);
//获取文件名称
String fileName = file.getOriginalFilename();
System.out.println("上传的文件名称:" + fileName); //获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
System.out.println("上传文件后缀名:" + suffixName); //文件上传后的路径
fileName = UUID.randomUUID() + suffixName;
System.out.println("新的文件名称:" + fileName);
File dest = new File(filePath + fileName); try {
file.transferTo(dest);
return new JsonData(0, null, fileName);
} catch(IllegalStateException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} return new JsonData(-1, "上传失败");
}
}
3.添加application.properties配置文件

#自定义文件上传路径
web.file.path=C:\\Users\\tianheng\\eclipse-workspace\\springboot-demo\\src\\main\\resources\\static\\images\\ #端口号
server.port=8090
4.添加upload.html界面
<!DOCTYPE html>
<html>
<head>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript" src="/js/test.js" ></script> <title>Insert title here</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
文件:<input type="file" name="head_img"/>
姓名:<input type="text" name="name"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
5.添加启动类
package top.ytheng.demo; import javax.servlet.MultipartConfigElement; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory; @SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} //文件大小配置
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB");
//设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}
}
6.测试地址
http://localhost:8090/upload.html
SpringBoot------文件上传的更多相关文章
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- SpringBoot 文件上传临时文件路径问题
年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...
- springboot文件上传下载简单使用
springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- Springboot 文件上传(带进度条)
1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
- SpringBoot学习6:springboot文件上传
1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
随机推荐
- python 基础笔记
1,去掉了C语言中的大括号,用空格来对齐语句块.(空格一般用2个或4个,但没有限制) 2,要在py文件代码中使用中文,需要在第一行加入下面的代码: # -*- coding: utf-8 -*- 或者 ...
- 在Linux下用make指令编译进度条程序。
首先建立一个新的文件,touch progress_bar.c 运行该vim progress_bar.c命令.写进度条的程序. 写进一个进度条程序: #include<stdio.h> ...
- python多线程同步机制condition
#!/usr/bin/env python# -*- coding: utf-8 -*- import threadingimport time def customer(cond): t = thr ...
- 微信支付WxpayAPI_php_v3(三)支付成功回调
接收回调通知后的业务处理都在NotifyProcess做,$data包含了微信返回给你的数据. Service: <?php /** * Created by PhpStorm. * User: ...
- 小米路由器刷Xiaomi Mi WiFi Mini openwrt
Current Stable Release - OpenWrt 18.06.1,released on August, 18th 2018. there is also PandoraBox fir ...
- Android Studio生成keystore签名文件步骤讲解
Android App打包时要用到签名文件,Android Studio生成签名文件步骤如下: Build---Generate Signed Apk...如图: 如果你的project中有2个或者2 ...
- (笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境)
参考了前人的成果,结合自己实践,arm-linux-gcc-4.4.3.tar.gz的下载地址为:http://ishare.iask.sina.com.cn/f/13836544.html?from ...
- Java如何格式化月份?
在Java中,如何以MMMM格式格式化时间? 这个示例使用SimpleDateFormat('MMMM')构造函数和SimpleDateFormat类的sdf.format(date)方法来格式化月份 ...
- http 状态码 40x
400 无法解析此请求. 401.1 未经授权:访问由于凭据无效被拒绝. 401.2 未经授权: 访问由于服务器配置倾向使用替代身份验证方法而被拒绝. 401.3 未经授权:访问由于 ACL 对所请求 ...
- (转)谈谈RTP传输中的负载类型和时间戳
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ticktick.blog.51cto.com/823160/350142 最近被 ...