spring boot file上传
用Spring Boot写读取Excel文件小工具的时候遇到的一些小坑已经填平,复制即可满足普通的文件上传功能
POI方面只需一个包,其他通用包工程中一般都会带
TIPS:前端为了扩展我用ajax异步请求,表单提交也支持,form表单enctype="multipart/form-data"始终需要
注:不要导入多余的 MultipartFile 相关配置,spring boot已自带文件上传功能
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
package sz.tools.controller; import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
import java.io.*;import java.util.Iterator;/**
* 组合商品产品小工具
*
* @author LiuZhibo
* @date 2018年7月21日15:17:13
*/
@RestController
public class CombinationController { @RequestMapping("/combination")
public String combination(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException { Assert.isTrue(file != null, "file is null");
Assert.isTrue(file.getSize() > 0, "file is null"); File f = null;
InputStream ins = file.getInputStream();
f = new File(file.getOriginalFilename());
inputStreamToFile(ins, f); Assert.isTrue(f != null, "file is null");
Assert.isTrue(f.length() > 0, "file is null"); FileInputStream fileInputStream = new FileInputStream(f); Workbook workbook = getWorkbook(f.getName(), fileInputStream); fileInputStream.close();
Sheet sheet = workbook.getSheetAt(0); // 获取到第一个sheet
Iterator<Row> iterator = sheet.rowIterator();
if (iterator.hasNext()) {
iterator.next(); // 跳过第一列
} while (iterator.hasNext()) {
try {
Row row = iterator.next();
Cell baseCell = row.getCell(1);
} catch (Exception e) {
e.printStackTrace();
break;
}
}
return null;
} private static Workbook getWorkbook(String fileName, FileInputStream fileInputStream) throws IOException {
Workbook workbook;
String extString = fileName.substring(fileName.lastIndexOf("."));
if (".xls".equals(extString)) {
workbook = new HSSFWorkbook(fileInputStream);
} else if (".xlsx".equals(extString)) {
workbook = new XSSFWorkbook(fileInputStream);
} else {
workbook = null;
}
return workbook;
} public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
--------------------------------------以下为HTML部分--------------------------------------------------------------
<html>
<head>
<title>Title</title>
</head>
<body>
<form id="fileForm" action="combination" enctype="multipart/form-data">
<input type="file" name="file" value="上传Excel"/>
<a href="javascript:;" id="submit">提交</a>
</form>
<div id="showContent"></div>
</body>
<script type="text/javascript" src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript">
$("#submit").click(function () {
var formData = new FormData(document.getElementById("fileForm"));//表单id
$.ajax({
type:"post",
data:formData,
url: "combination",
processData:false,
contentType:false,
success:function(data){
$("#showContent").html("");
var json = JSON.parse(data);
$.each(json, function(idx, obj) {
$("#showContent").append(obj + "<br/>")
});
alert("请求成功!");
},
error:function(e){
alert("错误!!");
console.log(data);
}
});
});
</script>
</html>
spring boot file上传的更多相关文章
- Spring Boot 文件上传原理
首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security.jpa.data.cloud等等,它无须关 ...
- spring boot文件上传、下载
主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...
- Spring框架学习笔记(7)——Spring Boot 实现上传和下载
最近忙着都没时间写博客了,做了个项目,实现了下载功能,没用到上传,写这篇文章也是顺便参考学习了如何实现上传,上传和下载做一篇笔记吧 下载 主要有下面的两种方式: 通过ResponseEntity实现 ...
- Spring Boot:上传文件大小超限制如何捕获 MaxUploadSizeExceededException 异常
Spring Boot 默认上传文件大小限制是 1MB,默认单次请求大小是 10MB,超出大小会跑出 MaxUploadSizeExceededException 异常 spring.servlet. ...
- spring boot(十七)上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...
- 使用Spring boot + jQuery上传文件(kotlin)
文件上传也是常见的功能,趁着周末,用Spring boot来实现一遍. 前端部分 前端使用jQuery,这部分并不复杂,jQuery可以读取表单内的文件,这里可以通过formdata对象来组装键值对, ...
- Spring Boot 文件上传
其实网上已经有很多这样的文章了.为什么我还要记录一下呢?原因是在工作中对接外系统时,碰到了他们调取我们文件上传接口确存在着http请求头部规范的情况,从而导致用传统方法获取不到参数.今天就来整理下Sp ...
- spring boot 文件上传大小限制
错误信息 : Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes. 解决方法一:在启动类添加如 ...
- 从.Net到Java学习第十篇——Spring Boot文件上传和下载
从.Net到Java学习系列目录 图片上传 Spring Boot中的文件上传就是Spring MVC中的文件上传,将其集成进来了. 在模板目录创建一个新的页面 profile/uploadPage. ...
随机推荐
- <在此处打开命令窗口>替换为PowerShell打开模式
Windows7中Shift+右键"在此处打开命令窗口"默认是采用cmd的方式打开. 把cmd替换为PowerShell的方式打开. 1. Ctrl + R 输入regedit进入 ...
- 迅为电子4.3寸CAN总线工业平板电脑简介
型号:iTOP-HMI043-C 4.3寸CAN总线工业平板电脑支持CAN通讯显示器,显示:显示尺寸:4.3英寸:分辨率:480×272 TFT液晶 65536色 :接口:支持CAN 2.0B:USB ...
- 【转】Google Chrome浏览器调试
作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.ht ...
- 纯CSS3来自定义单选框radio与复选框checkbox
单选框(radio)自定义样式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- 安装Yellowfin报错——No such file or directory: '/tmp/pip-build-jykvuD/YellowFin/README.md'
https://blog.csdn.net/quqiaoluo5620/article/details/80608474 在Pycharm中安装Yellowfin时一直报错"no such ...
- 诊断:CLSRSC-400: A system reboot is required to continue installing.
Linux7.5安装Grid Infrastructure 12.2.0.1时,在root.sh时会报错 2018/01/30 09:19:28 CLSRSC-330: Adding Clusterw ...
- bootstrap不兼容ie8如何解决
说起bootstrap大家一定都不陌生,可以说是目前最受欢迎的前端框架,简洁.直观.强悍.移动设备优先的前端开发框架,让web开发更迅速.简单. 但是在实际运用中也会遇到各种各样的问题,比如最近项目中 ...
- jquery toggle()设置
很多朋友对jquery toggle()比较熟练,甚至经常用到,而且对toggle的三个参数也比较了解$(selector).toggle(speed,callback,switch).但是当你设置$ ...
- MySQL异常:Caused by: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
Caused by: com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or cl ...
- Linux思维导图之网络管理
查漏补缺,理解概念,及时总结,欢迎拍砖. IP地址和MAC地址: 1.设计形态不同.IP地址是基于网络拓扑设计出来的,可以人为改动:而MAC地址是制造商烧录好的不能改动,网卡决定了MAC地址,是固定的 ...