springboot成神之——spring的文件上传
本文介绍spring的文件上传
目录结构

配置application
spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
DemoApplication
package com.springlearn.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
WebConfig
package com.springlearn.learn.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
.allowedHeaders("*");
}
}
TestController
package com.springlearn.learn.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.springlearn.learn.DemoApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class TestController {
@ResponseBody
@RequestMapping(value = "/UploadTest", method = RequestMethod.POST)
public String AuthTest(@RequestParam("file") MultipartFile files, HttpServletRequest request, HttpServletResponse response) {
String filePath = DemoApplication.class.getResource("").getPath() + "imgupload";
// Client File Name
String name = files.getOriginalFilename();
System.out.println("Client File Name = " + name);
if (name != null && name.length() > 0) {
try {
// Create the file at server
File serverFile = new File(filePath + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(files.getBytes());
stream.close();
System.out.println("Write file: " + serverFile);
} catch (Exception e) {
System.out.println("Error Write file: " + name);
}
}
return "上传成功";
}
}
前端上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<link rel="stylesheet" href="//unpkg.com/iview/dist/styles/iview.css">
<script src="//unpkg.com/iview/dist/iview.min.js"></script>
</head>
<body>
<div id="app">
<Upload
:before-upload="handleUpload"
action=""
>
<Button icon="ios-cloud-upload-outline">Upload files</Button>
</Upload>
</div>
<script>
new Vue({
el: '#app',
data: {
visible: false
},
methods: {
show: function () {
this.visible = true;
},
handleUpload(file) {
let param = new FormData();
param.append('file',file);
let config = {
headers:{'Content-Type':'multipart/form-data'}
};
debugger
axios.post('http://localhost:9001/UploadTest', param, config).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.log(error);
}).then(function () {
});
return false;
}
},
})
</script>
</body>
</html>
springboot成神之——spring的文件上传的更多相关文章
- spring实现文件上传(图片解析)
合抱之木,生于毫末,千里之行,始于足下,要想了解spring的文件上传功能,首先要知道spring是通过流的方式将文件进行解析,然后上传.那么是不是所有需要用的文件上传的地方都要写一遍文件解析器呢? ...
- SpringMVC系列(十一)把后台返回的数据转换成json、文件下载、文件上传
一.后台返回的数据转换成json 1.引入转换json需要的3个依赖 <!--json转换需要的依赖 begin --> <dependency> <groupId> ...
- spring boot文件上传、下载
主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- Spring Boot 文件上传原理
首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security.jpa.data.cloud等等,它无须关 ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- Strut2 和Spring MVC 文件上传对比
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...
随机推荐
- lightoj1138
二分 #include<map> #include<set> #include<cmath> #include<queue> #include<s ...
- 卸载MicrosoftBAF(删除C:\CommonFramework\instdata.dat)
发现有个可疑文件夹 C:\CommonFramework ,而且还会不停的删除创建 C:\CommonFramework\instdata.dat 最后被发现这是必应的一个框架程序在捣鬼,在微软论坛里 ...
- nt":false,"tarball":"http://registry.npm.taobao.org/babel-preset-stag
npm ERR! Unexpected end of input at 1:12777 npm ERR! nt":false,"tarball":"http:/ ...
- a, b交换与比较问题
1. 求a, b中较大的数,不使用if.?.switch等判断语句. 答案: 另一种思路是求两者的差,然后通过位运算判断差值的正负,不过个人觉得还是第一种各位简洁优雅. 2. 交换a, b的值,要求不 ...
- react-router-dom: 重定向默认路由
<appLayout> <Switch> <Route path='/' exact render={()=> ( <Redirect to={this.ge ...
- [Hive]HiveServer2配置
HiveServer2(HS2)是一个服务器接口,能使远程客户端执行Hive查询,并且可以检索结果.HiveServer2是HiveServer1的改进版,HiveServer1已经被废弃.HiveS ...
- 安装使用lynis扫描Linux的安全漏洞
Lynis是Linux平台上的一款安全漏洞扫描工具.它可以扫描系统的安全漏洞.收集系统信息.安装的软件信息.配置问题.没有设置密码的用户和防火墙等等. Lynis是流行可靠的安全扫描工具. 前不久,L ...
- 前端之HTML补充
一.列表 (1).无序列表<ul> <body> <ul type="disc"> <li>属性一</li> <l ...
- 《.NET程序员面试秘籍》读书笔记
1. 简述 private. protected. public. internal 修饰符的访问权限. private :设置类或类的成员为私有,在类的内部才可以访问.有时要访问私有成员时,可通过g ...
- SCARA——OpenGL入门学习一、二
参考博客:http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 简介 最近开始一个机械手臂的安装调试,平面关节型机器人又称SCA ...