本文介绍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的文件上传的更多相关文章

  1. spring实现文件上传(图片解析)

    合抱之木,生于毫末,千里之行,始于足下,要想了解spring的文件上传功能,首先要知道spring是通过流的方式将文件进行解析,然后上传.那么是不是所有需要用的文件上传的地方都要写一遍文件解析器呢? ...

  2. SpringMVC系列(十一)把后台返回的数据转换成json、文件下载、文件上传

    一.后台返回的数据转换成json 1.引入转换json需要的3个依赖 <!--json转换需要的依赖 begin --> <dependency> <groupId> ...

  3. spring boot文件上传、下载

    主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...

  4. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  5. Spring Boot 文件上传原理

    首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security.jpa.data.cloud等等,它无须关 ...

  6. Spring MVC文件上传教程 commons-io/commons-uploadfile

    Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...

  7. 【Java Web开发学习】Spring MVC文件上传

    [Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...

  8. Spring mvc文件上传实现

    Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...

  9. Strut2 和Spring MVC 文件上传对比

    在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...

随机推荐

  1. 一些开源搜索引擎实现——倒排使用原始文件,列存储Hbase,KV store如levelDB、mongoDB、redis,以及SQL的,如sqlite或者xxSQL

    本文说明:除开ES,Solr,sphinx系列的其他开源搜索引擎汇总于此.   A search engine based on Node.js and LevelDB A persistent, n ...

  2. opencv:摄像头和视频的读取

    示例代码: #include <opencv.hpp> using namespace cv; int main() { VideoCapture Capture(); //打开默认摄像头 ...

  3. 启动代码之开iCache

    1.什么是cache,有什么用    cache是一种内存,叫高速缓存.从容量来说:CPU < 寄存器 < cache < DDR从速度来说:CPU > 寄存器 > ca ...

  4. vbs 字符串替换

    http://blog.csdn.net/flm2003/article/details/7212448 function返回值 http://www.cnblogs.com/wakey/p/5758 ...

  5. VC6工程因行尾格式无法转换到VS2015

    VC6工程因行尾格式无法转换到VS2015(金庆的专栏 2017.6)参考:https://connect.microsoft.com/VisualStudio/feedback/details/54 ...

  6. hihocoder-1483区间价值 (二分+尺取法)

    题目链接: 区间价值 给定n个数A1...An,小Ho想了解AL..AR中有多少对元素值相同.小Ho把这个数目定义为区间[L,R]的价值,用v[L,R]表示. 例如1 1 1 2 2这五个数所组成的区 ...

  7. 帮你彻底解决eclipse(myeclipse)中写struts.xml配置文件

    其实,在自己写struts.xml的时候,竟然没有代码提示功能.让我非常的烦恼,其实解决这个问题的关键还是system不知道他的dtd的规则无法提示配置信息 很简单,那就让它知道就OK了!!! 道理明 ...

  8. 【BZOJ2850】巧克力王国 KDtree

    [BZOJ2850]巧克力王国 Description 巧克力王国里的巧克力都是由牛奶和可可做成的.但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜 欢过于甜的巧克力.对于每一块巧克力,我们设 ...

  9. sqlalchemy在pythonweb中开发的使用(基于tornado的基础上)

    一.关于SQLAlchemy的安装pip install SQLAlchemy安装如果上面的方式安装不成功的情况可以使用下面的方法 百度下载window或者linux下面对应的sqlalchemy的版 ...

  10. gradle 安装试用

    1. java 环境(jdk 6 以上,最好使用8以及以上) yum install -y java-1.8.0-openjdk-devel 2. 基本配置 // path 路径 export PAT ...