文件上传主要分以下几个步骤

(1)新建maven java project;

(2)在pom.xml加入相应依赖;

(3)新建一个表单页面(这里使用thymleaf);

(4)编写controller;

(5)测试;

(6)对上传的文件做一些限制;

(7)多文件上传实现

1)新建maven java project

新建一个名称为spring-boot-fileupload maven java项目;

2)在pom.xml加入相应依赖;

加入相应的maven依赖,具体看以下解释:

<!--

spring boot 父节点依赖,

引入这个之后相关的引入就不需要添加version配置,

spring boot会自动选择最合适的版本进行添加。

-->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<!-- spring boot web支持:mvc,aop... -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- thmleaf模板依赖. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<!-- 编译版本; -->

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

3)新建一个表单页面(这里使用thymleaf

在src/main/resouces新建templates(如果看过博主之前的文章,应该知道,templates是spring boot存放模板文件的路径),在templates下新建一个file.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>Hello World!</title>

</head>

<body>

<form method="POST" enctype="multipart/form-data" action="/upload">

<p>文件:<input type="file" name="file" /></p>

<p><input type="submit" value="上传" /></p>

</form>

</body>

</html>

4)编写controller;

编写controller进行测试,这里主要实现两个方法:其一就是提供访问的/file路径;其二就是提供post上传的/upload方法,具体看代码实现:

package com.kfit;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

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;

@Controller

publicclass FileUploadController {

//访问路径为:http://127.0.0.1:8080/file

@RequestMapping("/file")

public String file(){

return"/file";

}

/**

* 文件上传具体实现方法;

* @param file

* @return

*/

@RequestMapping("/upload")

@ResponseBody

public String handleFileUpload(@RequestParam("file")MultipartFile file){

if(!file.isEmpty()){

try {

/*

* 这段代码执行完毕之后,图片上传到了工程的跟路径;

* 大家自己扩散下思维,如果我们想把图片上传到 d:/files大家是否能实现呢?

* 等等;

* 这里只是简单一个例子,请自行参考,融入到实际中可能需要大家自己做一些思考,比如:

* 1、文件路径;

* 2、文件名;

* 3、文件格式;

* 4、文件大小的限制;

*/

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));

out.write(file.getBytes());

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

return"上传失败,"+e.getMessage();

} catch (IOException e) {

e.printStackTrace();

return"上传失败,"+e.getMessage();

}

return"上传成功";

}else{

return"上传失败,因为文件是空的.";

}

}

}

5)编写App.java然后测试

App.java没什么代码,就是Spring Boot的启动配置,具体如下:

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* Hello world!

*

*/

//其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

@SpringBootApplication

publicclass App {

publicstaticvoid main(String[] args) {

SpringApplication.run(App.class, args);

}

}

然后你就可以访问:http://127.0.0.1:8080/file 进行测试了,文件上传的路径是在工程的跟路径下,请刷新查看,其它的请查看代码中的注释进行自行思考。

6)对上传的文件做一些限制;

对文件做一些限制是有必要的,在App.java进行编码配置:

@Bean

public MultipartConfigElement multipartConfigElement() {

MultipartConfigFactory factory = new MultipartConfigFactory();

//// 设置文件大小限制 ,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;

factory.setMaxFileSize("128KB"); //KB,MB

/// 设置总上传数据总大小

factory.setMaxRequestSize("256KB");

//Sets the directory location where files will be stored.

//factory.setLocation("路径地址");

returnfactory.createMultipartConfig();

}

7)多文件上传实现

多文件对于前段页面比较简单,具体代码实现:

在src/resouces/templates/mutifile.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>Hello World!</title>

</head>

<body>

<form method="POST" enctype="multipart/form-data" action="/batch/upload">

<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>

com.kfit.FileUploadController中新增两个方法:

/**

* 多文件具体上传时间,主要是使用了MultipartHttpServletRequest和MultipartFile

* @param request

* @return

*/

@RequestMapping(value="/batch/upload", method= RequestMethod.POST)

public@ResponseBody

String handleFileUpload(HttpServletRequest request){

List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");

MultipartFile file = null;

BufferedOutputStream stream = null;

for (inti =0; i< files.size(); ++i) {

file = files.get(i);

if (!file.isEmpty()) {

try {

byte[] bytes = file.getBytes();

stream =

new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));

stream.write(bytes);

stream.close();

} catch (Exception e) {

stream =  null;

return"You failed to upload " + i + " => " + e.getMessage();

}

} else {

return"You failed to upload " + i + " because the file was empty.";

}

}

return"upload successful";

}

访问路径:http://127.0.0.1:8080/mutifile 进行测试。

Spring Boot 系列博客】

)前言【从零开始学Spring Boot】 :

http://412887952-qq-com.iteye.com/blog/2291496

)spring boot起步之Hello World【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2291500

)Spring Boot返回json数据【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blog/2291508

)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blogs/2292376

)Spring Boot普通类调用bean【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2292388

更多查看博客:http://412887952-qq-com.iteye.com/blog

(29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】的更多相关文章

  1. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  2. (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

    在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...

  3. 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】

    什么是"异步调用"? "异步调用"对应的是"同步调用",同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执 ...

  4. 16. Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52001744 在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使 ...

  5. 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...

  6. (28)SpringBoot启动时的Banner设置【从零开始学Spring Boot】

    对于使用过Spring Boot的开发者来说,程序启动的时候输出的由字符组成的Spring符号并不陌生.这个是Spring Boot为自己设计的Banner: 1.    .   ____       ...

  7. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  8. 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...

  9. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

随机推荐

  1. 【Git学习笔记】用git pull取回远程仓库某个分支的更新,再与本地的指定分支自动merge【转】

    本文转载自:http://blog.csdn.net/liuchunming033/article/details/45367629 git pull的作用是,从远程库中获取某个分支的更新,再与本地指 ...

  2. 2017 Multi-University Training Contest - Team 2 &&hdu 6050 Funny Function

    Funny Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 1213(并查集模版题)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Node.js安全清单

    前言 安全性,总是一个不可忽视的问题.许多人都承认这点,但是却很少有人真的认真地对待它.所以我们列出了这个清单,让你在将你的应用部署到生产环境来给千万用户使用之前,做一个安全检查. 以下列出的安全项, ...

  5. bzoj3436: 小K的农场(差分约束)

    3436: 小K的农场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1575  Solved: 690[Submit][Status][Discus ...

  6. MySQL-基础操作之增删改查

    1.增 (1)创建数据库dks create database dks; (2)创建名为t1的表,并指定引擎和字符集: ) not null,ages int) engine=innodb defau ...

  7. 【BZOJ4590】自动刷题机

    [思路分析] 比赛的时候想到了用二分+贪心,二分的部分与贪心的部分也写对了,但是由于数据范围未看没有开long long,且二分左端点赋值过小导致WA掉 正解:二分+贪心 二分代码的长度,贪心判断能否 ...

  8. MarkDown流程图概要

    要素 流程元素定义: 名称=>类型: 显示名称 控制流程定义: 名称1([yes,no],right)->名称2 注意事项 流程元素定义在代码上部, 流程走向定义在代码下部 名称可以取中文 ...

  9. 使用ZeppelinHub来存储和展示ZeppelinNoteBook

    0.序 说实在的这个功能太赞了 在一开始接触的时候不知道有这个功能,我尝试做一下配置,发现非常的棒. 棒的原因有两点: 可以在随时随地有互联网的地方访问自己的ZeppelinHub来查看Zeppeli ...

  10. POJ 1659 Havel-Hakimi定理

    关于题意和Havel-Hakimi定理,可以看看http://blog.csdn.net/wangjian8006/article/details/7974845 讲得挺好的. 我就直接粘过来了 [ ...