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>