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

(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 进行测试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章:
制作网站的模板软件,网站怎么建设?
制作旅游网站html,怎样注册旅游网站?
如何快速生成专业多端适配建站电话?
MySQL查询结果复制到新表的方法(更新、插入)
网站企业制作流程,用什么语言做企业网站比较好?
如何在阿里云通过域名搭建网站?
,sp开头的版面叫什么?
深圳网站制作培训,深圳哪些招聘网站比较好?
建站VPS推荐:2025年高性能服务器配置指南
视频网站制作教程,怎么样制作优酷网的小视频?
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
如何彻底删除建站之星生成的Banner?
如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何高效搭建专业期货交易平台网站?
如何在万网开始建站?分步指南解析
小型网站建站如何选择虚拟主机?
北京制作网站的公司,北京铁路集团官方网站?
西安专业网站制作公司有哪些,陕西省建行官方网站?
如何自定义建站之星模板颜色并下载新样式?
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
如何选择适合PHP云建站的开源框架?
建站之星多图banner生成与模板自定义指南
七夕网站制作视频,七夕大促活动怎么报名?
大连网站制作公司哪家好一点,大连买房网站哪个好?
专业网站制作服务公司,有哪些网站可以免费发布招聘信息?
如何通过虚拟主机快速完成网站搭建?
如何选择可靠的免备案建站服务器?
定制建站哪家更专业可靠?推荐榜单揭晓
c# Task.ConfigureAwait(true) 在什么场景下是必须的
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
TestNG的testng.xml配置文件怎么写
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
建站上传速度慢?如何优化加速网站加载效率?
建站主机选哪家性价比最高?
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?
香港服务器租用每月最低只需15元?
C#如何使用XPathNavigator高效查询XML
如何制作一个表白网站视频,关于勇敢表白的小标题?
制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?
建站之星价格显示格式升级,你的预算足够吗?
建站之星免费模板:自助建站系统与智能响应式一键生成
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
建站之星导航菜单设置与功能模块配置全攻略
教育培训网站制作流程,请问edu教育网站的域名怎么申请?
如何在IIS中新建站点并配置端口与物理路径?
如何快速搭建高效香港服务器网站?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
电商网站制作价格怎么算,网上拍卖流程以及规则?
*请认真填写需求信息,我们会在24小时内与您取得联系。