如何用SpringBoot框架来接收multipart/form-data文件
2021-06-21 23:05
标签:new csdn and 千万 turn 功能 rtc 1.7 ras https://blog.csdn.net/linzhiqiang0316/article/details/77016997 **************************************************************** 后台接收代码: 后台接收代码: 如何用SpringBoot框架来接收multipart/form-data文件 标签:new csdn and 千万 turn 功能 rtc 1.7 ras 原文地址:https://www.cnblogs.com/zhao1949/p/9681701.htmlboolean 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
上一篇:Python学习之路运算符
下一篇:关于数组去重的思考
文章标题:如何用SpringBoot框架来接收multipart/form-data文件
文章链接:http://soscw.com/index.php/essay/97108.html