使用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. try catch 异常处理

    1.捕获指定异常 2.捕获所有异常(catch(...))

  2. (转)一种开源的跨平台视频开发框架:VideoLAN - VLC media player

    VLC原先是几个法国的大学生做的项目,后来他们把VLC作为了一个开源的项目,吸引了来自世界各国的很多优秀程序员来共同编写和维护VLC,才逐渐变成了现在这个样子.至于为什么叫VideoLan Clien ...

  3. linux下使用tar命令 (转至http://www.cnblogs.com/li-hao/archive/2011/10/03/2198480.html)

    解压语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用. 主选项: c 创建新的档案文件.如果用户想备份一个目录或 ...

  4. NET Core 环境搭建和命令行CLI入门[转]

      NET Core 环境搭建和命令行CLI入门 时间:2016-07-06 01:48:19      阅读:258      评论:0      收藏:0      [点我收藏+]   标签: N ...

  5. <OC>OC新特征array literals

    类似于java5提供的autoboxing功能.以前的写法:NSNumber * number = [NSNumber numberWithInt:1];NSArray * array = [NSAr ...

  6. Python学习笔记(二)——高级特性

    知识点 切片 切片 取一个list或tuple的部分元素. 原理:调用__getitem__,__setitem__,__delitem__和slice函数. 根据官方的帮助文档(https://do ...

  7. 织梦DedeCMS使用SQL批量替换文章标题内容

    在使用织梦DedeCMS的过程中,出于伪原创或者其他的原因,我们需要对文档的内容.标题.描述等等进行同义词或者其他的替换.这个就是一个简单的织梦SQL语句操作的问题,No牛网在织梦DedeCMS常用S ...

  8. Objective-C语法之字符串NSString

    Objective-C里核心的处理字符串的类就是NSString和 NSMutableString这两个类,这两个类完成了Objective-C中字符串大部分功能的处理.这两个类的最主要的区别是NSS ...

  9. python计算时间差的方法

    本文实例讲述了python计算时间差的方法.分享给大家供大家参考.具体分析如下: 1.问题: 给定你两个日期,如何计算这两个日期之间间隔几天,几个星期,几个月,几年? 2.解决方法: 标准模块date ...

  10. 一些laravel博文

    人比人比死人系列 https://www.insp.top/tag/laravel http://www.iwanli.me/