本篇文章,我们要来做一个Spring的文件上传功能:

1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.0.2.RELEASE</version> </dependency>
2.在webapp目录下的index.jsp文件中输入一个表单:
<html>
<body>
<form method="POST" enctype="multipart/form-data"
action="/upload">
File to upload: <input type="file" name="file"><br /> Name: <input
type="text" name="name"><br /> <br /> <input type="submit"
value="Upload"> Press here to upload the file!
</form>
</body>
</html>
这个表单就是我们模拟的上传页面。
3. 编写处理这个表单的Controller:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
4. 然后我们对上传的文件做一些限制,同时编写main方法来启动这个web :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. 然后访问http://localhost:8080/upload就可以看到页面了。
上面的例子是实现的是单个文件上传的功能,假定我们现在要实现文件批量上传的功能的话,我们只需要简单的修改一下上面的代码就行,考虑到篇幅的问题,下面只是贴出和上面不同的代码,没有贴出的说明和上面一样。:
1. 新增batchUpload.jsp文件
<html> <body> <form method="POST" enctype="multipart/form-data" action="/batch/upload"> File to upload: <input type="file" name="file"><br /> File to upload: <input type="file" name="file"><br /> <input type="submit" value="Upload"> Press here to upload the file! </form> </body> </html>
2. 新增BatchFileUploadController.java文件:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
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;
/**
* Created by wenchao.ren on 2014/4/26.
*/
@Controller
public class BatchFileUploadController {
@RequestMapping(value="/batch/upload", method= RequestMethod.POST)
public @ResponseBody
String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
for (int i =0; i< files.size(); ++i) {
MultipartFile file = files.get(i);
String name = file.getName();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + i)));
stream.write(bytes);
stream.close();
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
return "upload successful";
}
}
这样一个简单的批量上传文件的功能就ok了,是不是很简单啊。
注意:上面的代码只是为了演示而已,所以编码风格上采取了随性的方式,不建议大家模仿。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring实现文件上传
# spring文件上传
# spring4上传文件
# SpringMVC文件上传 多文件上传实例
# Spring实现文件上传(示例代码)
# springMVC配置环境实现文件上传和下载
# jquery.form.js框架实现文件上传功能案例解析(springmvc)
# Spring学习笔记2之表单数据验证、文件上传实例代码
# springMVC实现前台带进度条文件上传的示例代码
# SpringMvc导出Excel实例代码
# 详解springMVC两种方式实现多文件上传及效率比较
# Spring MVC 文件上传下载的实例
# 表单
# 上传
# 贴出
# 文件上传
# 的是
# 就行
# 很简单
# 这样一个
# 考虑到
# 做一个
# 只需要
# 我们现在
# 要来
# 方法来
# 创建一个
# 大家多多
# 就可以
# 上传文件
# 目录下
# File
相关文章:
阿里云网站制作公司,阿里云快速搭建网站好用吗?
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
如何通过PHP快速构建高效问答网站功能?
c# 服务器GC和工作站GC的区别和设置
香港网站服务器数量如何影响SEO优化效果?
音乐网站服务器如何优化API响应速度?
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
成都网站制作价格表,现在成都广电的单独网络宽带有多少的,资费是什么情况呢?
,如何利用word制作宣传手册?
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
如何高效搭建专业期货交易平台网站?
企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
魔方云NAT建站如何实现端口转发?
如何通过虚拟主机快速搭建个人网站?
如何自定义建站之星网站的导航菜单样式?
php8.4新语法match怎么用_php8.4match表达式替代switch【方法】
建站之星备案流程有哪些注意事项?
开心动漫网站制作软件下载,十分开心动画为何停播?
上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?
宝塔新建站点报错如何解决?
如何通过WDCP绑定主域名及创建子域名站点?
如何选择建站程序?包含哪些必备功能与类型?
如何选择高效可靠的多用户建站源码资源?
如何注册花生壳免费域名并搭建个人网站?
免费视频制作网站,更新又快又好的免费电影网站?
制作公司内部网站有哪些,内网如何建网站?
如何挑选最适合建站的高性能VPS主机?
公司网站制作价格怎么算,公司办个官网需要多少钱?
如何确保FTP站点访问权限与数据传输安全?
怎么将XML数据可视化 D3.js加载XML
网站制作公司,橙子建站是合法的吗?
设计网站制作公司有哪些,制作网页教程?
百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?
零服务器AI建站解决方案:快速部署与云端平台低成本实践
如何在Golang中指定模块版本_使用go.mod控制版本号
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
如何快速搭建个人网站并优化SEO?
c++怎么用jemalloc c++替换默认内存分配器【性能】
保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?
制作网站的基本流程,设计网站的软件是什么?
香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧
,sp开头的版面叫什么?
如何安全更换建站之星模板并保留数据?
建站之星如何实现网站加密操作?
如何在阿里云虚拟服务器快速搭建网站?
清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?
建站之星官网登录失败?如何快速解决?
如何批量查询域名的建站时间记录?
非常酷的网站设计制作软件,酷培ai教育官方网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。