如何用SpringBoot框架来接收multipart/form-data文件
https://blog.csdn.net/linzhiqiang0316/article/details/77016997
****************************************************************

boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判断是否是表单文件类型
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);//从request得到所有上传域的列表
for(Iterator iter = items.iterator();iter.hasNext();){
FileItem fileitem =(FileItem) iter.next();
if(!fileitem.isFormField()&&fileitem!=null){//判读不是普通表单域即是file
System.out.println("name:"+fileitem.getName());
}
}
<html>
<body>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
</form>
</body>
</html>

后台接收代码:
/**
* 文件上传具体实现方法;
*
* @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())));
System.out.println(file.getName());
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 "上传失败,因为文件是空的.";
}
}
<!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="text" name="id" /></p>
<p>文件2:<input type="text" name="name" /></p>
<p>文件3:<input type="file" name="file" /></p>
<p><input type="submit" value="上传" /></p>
</form>
</body>
</html>

后台接收代码:
@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
@ResponseBody
public String handleFileUpload(HttpServletRequest request) {
MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("file");
String name=params.getParameter("name");
System.out.println("name:"+name);
String id=params.getParameter("id");
System.out.println("id:"+id);
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";
}
如何用SpringBoot框架来接收multipart/form-data文件的更多相关文章
- 文件系统(02):基于SpringBoot框架,管理Xml和CSV文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.XML文档 XML是可扩展标记语言,是一种用于标记电子文件使其具有结构性的标记语言.标记指计算机所能理解的信息符号,通过 ...
- 文件系统(01):基于SpringBoot框架,管理Excel和PDF文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.Excel文档 Excel一款电子表格软件.直观的界面.出色的计算功能和图表工具,在系统开发中,经常用来把数据转存到Ex ...
- python 处理form/data文件上传
处理multipart/form-data 的java serverlet请求接口通过python实现 记住不要在头加:"Content-Type":"multipart ...
- ASP.NET 服务端接收Multipart/form-data文件
在网络编程过程中需要向服务器上传文件. Multipart/form-data是上传文件的一种方式. /// <summary> /// 上传工程文件 /// </summary&g ...
- html5 file upload and form data by ajax
html5 file upload and form data by ajax 最近接了一个小活,在短时间内实现一个活动报名页面,其中遇到了文件上传. 我预期的效果是一次ajax post请求,然后在 ...
- Sending forms through JavaScript[form提交 form data]
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript As in the ...
- springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...
- 极简SpringBoot指南-Chapter01-如何用Spring框架声明Bean
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- Python Web框架篇:Django Form组件
Form简介 在HTTP中,表单(form标签),是用来提交数据的,其action属性说明了其传输数据的方法:如何传.如何接收. 访问网站时,表单可以实现客户端与服务器之间的通信.例如查询,就用到了表 ...
随机推荐
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- suricata 命令行解释【转】
suricata命令行 转载地址:http://blog.sina.com.cn/s/blog_6f8edcde0101gcha.html suricata命令行选项说明 你能两种方式使用命令行选项, ...
- 重写Checkbox 改写选择框的大小
/* 作者:Starts_2000 * 日期:2009-07-30 * 网站:http://www.csharpwin.com CS 程序员之窗. * 你可以免费使用或修改以下代码,但请保留版权信息. ...
- 【Spring】spring的7个模块
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...
- 【MySQL】MySQL之MySQL5.7中文乱码
自己的MySQL服务器不能添加中文,于是自己使用 show variables like 'character%'; 查看了当前的编码格式 我又通过以下方法将其设置为utf-8 SET charact ...
- iOS 限制输入字数完美解决方案
关于限制输入字数以前也做过,网上也很多方法.但都不够完美,本方法可防止中文联想.粘贴等突破长途限制.可防止Emoji截为两半导致编码出问题. - (void)textFieldDidChange: ...
- 【C语言】字符串与整型数值之间的转换
一.将字符串转化为对应的数值 /*============================================================================= # # F ...
- 域名 ip地址 端口号
域名默认指定一个ip地址 当用域名访问网站的时候 网站会默认给个端口号80 或者自己指定 其他的 例如数据库 也是会给端口号 例如mysql 3306 域名:80 是访问iis 网站域名:3306 是 ...
- Red Hat忘记root密码了怎么办?
方法很简单: 在出现grub画面时,用上下键选中平时启动linux的那一项,然后按a键,可以进行append模式 在出来的命令行等方面加上 空格 single , 这样可以进入单用户模式,再 pass ...
- javascript中 try catch finally 的使用
例一:function message(){ try { adddlert("Welcome guest!") } catch(err) { txt="此页面存在一个错误 ...