使用thymleaf模板,自行导入依赖!

一、单文件上传

1、编写单文件上传页面singleFile.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>单文件上传</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/singleUpload">
<p>选择文件:<input type="file" name="file"/></p>
<p><input type="submit" th:value="上传"/></p>
</form>
</body>
</html>

2、编写FileUploadController.java

package com.rick.apps.controller;

import com.rick.common.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream; /**
* Desc : 文件上传
* User : RICK
* Time : 2017/8/23 9:36
*/ @Controller
public class FileUploadController { /**
* Desc : 跳转单文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/ @RequestMapping("/singleFile")
public String singleFile(){
System.out.println("-------------------");
return"/singleFile";
} /**
* Desc : 单文件上传
* 注意:不指定上传目录,默认是上传到项目的根目录
* User : RICK
* Time : 2017/8/23 9:40
*/
@ResponseBody
@PostMapping("/singleUpload")
public ResultJson singleUpload(@RequestParam("file")MultipartFile file){
if (!file.isEmpty()){
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
} else {
return ResultJson.buildFailInstance("上传失败,文件为空!");
}
return ResultJson.buildSuccessInstance();
}
}

3、编写文件上传的设置

package com.rick;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean; import javax.servlet.MultipartConfigElement; @SpringBootApplication
@EnableConfigurationProperties
@ServletComponentScan
public class SpringbootEdu01Application { public static void main(String[] args) {
SpringApplication.run(SpringbootEdu01Application.class, args);
} /**
* Desc : 设置文件上传的基本配置
* User : RICK
* Time : 2017/8/23 10:11
*/ @Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置文件大小限制 ,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
factory.setMaxFileSize("1MB");//KB,MB
//设置总上传数据总大小
factory.setMaxRequestSize("10MB");////KB,MB
//设置文件存放位置
// factory.setLocation("d:\\files");
return factory.createMultipartConfig();
}
}

4、启动项目测试

访问http://localhost:8080/singleFile出现文件上传页面

选择要上传的文件,点击上传

上传成功,到项目根目录下查看文件是否存在

项目清单:

二、多文件上传

1、编写多文件上传页面multFile.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>多文件上传</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/multUpload">
<p>文件1:<input type="file" name="file" /></p>
<p>文件2:<input type="file" name="file" /></p>
<p>文件3:<input type="file" name="file" /></p>
<p><input type="submit" value="上传" /></p>
</form>
</body>
</html>

2、编写多文件上传后台代码FileUploadController.java

package com.rick.apps.controller;

import com.rick.common.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List; /**
* Desc : 文件上传
* User : RICK
* Time : 2017/8/23 9:36
*/ @Controller
public class FileUploadController { /**
* Desc : 跳转单文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/
@RequestMapping("/singleFile")
public String singleFile(){
return"/singleFile";
} /**
* Desc : 单文件上传
* 注意:不指定上传目录,默认是上传到项目的根目录
* User : RICK
* Time : 2017/8/23 9:40
*/
@ResponseBody
@PostMapping("/singleUpload")
public ResultJson singleUpload(@RequestParam("file")MultipartFile file){
if (!file.isEmpty()){
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
} else {
return ResultJson.buildFailInstance("上传失败,文件为空!");
}
return ResultJson.buildSuccessInstance();
} /**
* Desc : 跳转多文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/
@RequestMapping("/multFile")
public String multFile(){
return"/multFile";
} /**
* Desc : 多文件上传
* 主要是使用了MultipartHttpServletRequest和MultipartFile
* User : RICK
* Time : 2017/8/23 10:17
*/
@ResponseBody
@PostMapping("/multUpload")
public ResultJson multUpload(HttpServletRequest request){
try {
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i=0;i<files.size();i++){
file = files.get(i);
if(!file.isEmpty()){
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
stream.write(bytes);
stream.flush();
stream.close();
}
}
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
return ResultJson.buildSuccessInstance();
} }

3、启动项目测试,http://localhost:8080/multFile

选择文件

点击上传

到项目根目录下查看文件是否上传成功

项目清单:

170706、springboot编程之文件上传的更多相关文章

  1. Windows环境下用C#编程将文件上传至阿里云OSS笔记

    Windows环境下用C#编程将文件上传至阿里云OSS笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/detai ...

  2. SpringBoot项目实现文件上传和邮件发送

    前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...

  3. Springboot如何启用文件上传功能

    网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...

  4. iOS-网络编程(二)文件上传和断点离线下载

    一. iOS中发送HTTP请求的方案 在iOS中,我们常用发送HTTP请求的方案有苹果原生(自带)NSURLConnection:用法简单,最古老最经典最直接的一种方案 (iOS 9.0弃用)NSUR ...

  5. SpringBoot+BootStrap多文件上传到本地

    1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...

  6. SpringBoot之KindEditor文件上传

    后端核心代码如下: package com.blog.springboot.controller; import java.io.BufferedOutputStream; import java.i ...

  7. springboot+vue实现文件上传

    https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: ...

  8. SpringBoot: 6.文件上传(转)

    1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. Springboot(九).多文件上传下载文件(并将url存入数据库表中)

    一.   文件上传 这里我们使用request.getSession().getServletContext().getRealPath("/static")的方式来设置文件的存储 ...

随机推荐

  1. 【转】MFC CreateFont 用法

    中国人自古就有自右至左.从上到下书写汉字的习惯.而当我们在自己所编写的应用程序中使用输出函数输出的总是自左至右的横排文字.有没有可能在我们的应用程序中实现竖写汉字的效果呢?笔者偶然发现了一种利用VC实 ...

  2. UIView的几个枚举定义

    UIView是iOS开发最主要的视图,非常多控件都是继承它,掌握当中的几个基本枚举定义,有利益理解视图的载入和參数差别. 一.UIViewAnimationCurve UIView的基本动画变化规律 ...

  3. 关于多层for循环迭代的效率优化问题

    关于多层for循环迭代的效率优化问题 今天笔试的时候遇到这么一道题目  说有上面这么循环嵌套  .问怎么优化 并说明原因.     for(int i = 0 ; i < 1000 ;i++){ ...

  4. CentOS简单命令学习:date cal bc

    简单的shell指令: 1.日期的格式化显示: 2.日历的显示: 3.bc计算器: 使用Tab指令自动补全:

  5. informatica 中的workflow连接mysql数据配置DSN

    先要下载mysqlodbc 一步步安装之后 ,再从管理工具里面找到ODBC,最后选择系统DSN,添加mysql驱动之后,进入添加编辑: 在workflow里面的配置连接字符串就写刚刚配置的连接名称 比 ...

  6. 基于Nginx反向代理及负载均衡

    基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...

  7. 我的javascript心跳机

    li { list-style: none!important; padding:0; } .list_num{ list-style-type:decimal; } .list_inline{ ma ...

  8. C# 哈希加密

    1.方法一: [c-sharp] view plaincopy //适用于C#语言 //使用前需导入以下命名空间:using System.Web.Security; //第一个参数为需加密的字符串, ...

  9. 一、NHibernate配置所支持的属性

    属性名 用途 dialect 设置NHibernate的Dialect类名 - 允许NHibernate针对特定的关系数据库生成优化的SQL 可用值: full.classname.of.Dialec ...

  10. 导入google地图

    一直报地图页面的 java.lang.incompatibleclasschangeerror 想来想去,应该是包不兼容的原因,原本以为,在 build.gradle 里面 compileSdkVer ...