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

(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依赖,具体看以下解释:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-fileupload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
spring boot 父节点依赖,
引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(3)新建一个表单页面(这里使用thymleaf)
在src/main/resouces新建templates,在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方法,具体看代码实现:
@Controller
public class 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 "上传失败,因为文件是空的.";
}
}
// 访问路径为:http://127.0.0.1:8080/file
@RequestMapping("/mutifile")
public String mutifile() {
return "/mutifile";
}
(5)编写Application.java然后测试
Application没什么代码,就是Spring Boot的启动配置,具体如下:
package com.example.controller;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
然后你就可以访问:http://127.0.0.1:8080/file 进行测试了,文件上传的路径是在工程的跟路径下,请刷新查看,其它的请查看代码中的注释进行自行思考
(6)对上传的文件做一些限制;
对文件做一些限制是有必要的,在Application.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("路径地址");
return factory.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>
FileUploadController中新增两个方法:
// 访问路径为:http://127.0.0.1:8080/mutifile
@RequestMapping("/mutifile")
public String mutifile() {
return "/mutifile";
}
/**
* 多文件具体上传时间,主要是使用了MultipartHttpServletRequest和MultipartFile
*
* @param request
* @return
*/
@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
@ResponseBody
public String handleFileUpload(HttpServletRequest request) {
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()) {
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 进行测试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章:
如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法
桂林网站制作公司有哪些,桂林马拉松怎么报名?
建站之星代理平台如何选择最佳方案?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
建站主机核心功能解析:服务器选择与网站搭建流程指南
盘锦网站制作公司,盘锦大洼有多少5G网站?
高端云建站费用究竟需要多少预算?
建站之星安装后界面空白如何解决?
常州自助建站:操作简便模板丰富,企业个人快速搭建网站
如何自定义建站之星模板颜色并下载新样式?
如何在服务器上三步完成建站并提升流量?
表情包在线制作网站免费,表情包怎么弄?
如何快速搭建FTP站点实现文件共享?
成都网站制作报价公司,成都工业用气开户费用?
香港服务器如何优化才能显著提升网站加载速度?
如何快速搭建高效可靠的建站解决方案?
如何高效生成建站之星成品网站源码?
南京网站制作费用,南京远驱官方网站?
建站之星展会模版如何一键下载生成?
如何制作一个表白网站视频,关于勇敢表白的小标题?
开心动漫网站制作软件下载,十分开心动画为何停播?
制作营销网站公司,淘特是干什么用的?
阿里云网站搭建费用解析:服务器价格与建站成本优化指南
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
黑客入侵网站服务器的常见手法有哪些?
c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗
建站之星如何配置系统实现高效建站?
建站主机是否等同于虚拟主机?
湖州网站制作公司有哪些,浙江中蓝新能源公司官网?
建站主机选哪种环境更利于SEO优化?
如何使用Golang table-driven基准测试_多组数据测量函数效率
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
建站之星Pro快速搭建教程:模板选择与功能配置指南
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
宝华建站服务条款解析:五站合一功能与SEO优化设置指南
小型网站制作HTML,*游戏网站怎么搭建?
中山网站推广排名,中山信息港登录入口?
建站ABC备案流程中有哪些关键注意事项?
建站之星如何快速更换网站模板?
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?
哈尔滨网站建设策划,哈尔滨电工证查询网站?
电脑免费海报制作网站推荐,招聘海报哪个网站多?
单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?
宁波自助建站系统如何快速打造专业企业网站?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
建站之星如何实现PC+手机+微信网站五合一建站?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
利用JavaScript实现拖拽改变元素大小
上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?
*请认真填写需求信息,我们会在24小时内与您取得联系。