1.使用POI结合springmvc实现上传

摘要:
1.导入poi<dependency><groupId>org的依赖关系。阿帕奇。poi</groupId><artifactId>poi</artifactId><version>3.9</version><dependency><deposition><groupId>org。阿帕奇。poi</groupId><artifactId>poi ooxml</artifactId><ve

1.引入poi的依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

2.jsp页面编写上传文件

<form action="http://localhost:8085/mango/pay/lotPayThirdAccount/getFile" enctype="multipart/form-data" method="post">
    选择一个文件:
    <input type="file" name="excelFile"/>
    <br/><br/>
    <input type="submit" value="上传" />
</form>

3.后台接收文件(仅有接收步骤,详细API自己看文档)

@RequestMapping(value = "/getFile", method = RequestMethod.POST)
  public GenericResult<?> getFile(@RequestParam(value="excelFile")MultipartFile file,HttpServletRequest req) throws IOException{
    // 获取临时存储路径以及重命名文件
    String filePath = req.getSession().getServletContext().getRealPath("/")+"WEB-INF/upload";  
    String fileName = file.getOriginalFilename();
    fileName = DateUtil.dateToString(new Date(), DateUtil.YYYYMMDDHHMMSS)+fileName;  
    System.err.println("文件上传的临时文件夹:"+filePath);  
    // 校验目录,不存在则创建
    File targetFile = new File(filePath, fileName);  
    if(!targetFile.exists()){  
        targetFile.mkdirs();  
    }  
    //保存  
    try {  
      //将前台传过来的file文件写到targetFile中.  
      file.transferTo(targetFile);  
    } catch (Exception e) {  
      e.printStackTrace();  
    }
    String absolutePath = filePath + "/" + fileName;
        
    InputStream fis = new FileInputStream(absolutePath);  
    Workbook wb = null;
    try {
      wb = WorkbookFactory.create(fis);
    } catch (InvalidFormatException e) {
      e.printStackTrace();
    }
Sheet sheet
= wb.getSheetAt(0); int totalRowNum = sheet.getLastRowNum(); System.out.println(totalRowNum); return null; }

以上是简单的一个接收流程,如果WorkbookFactory.create(fis)包内存溢出的错误, eclipse的vm配置为:

-Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m

即可解决。

免责声明:文章转载自《1.使用POI结合springmvc实现上传》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MySQL同步故障:" Slave_SQL_Running:No" 两种解决办法模拟赛1029d2下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java使用POI解析Excel表格

概述 Excel表格是常用的数据存储工具,项目中经常会遇到导入Excel和导出Excel的功能。 常见的Excel格式有xls和xlsx。07版本以后主要以基于XML的压缩格式作为默认文件格式xlsx。新格式主要是使用了OpenXML标准,结合了XML与Zip压缩技术。在这里就不细说,感兴趣的读者可以自行去查找相关知识。本文将重点以这两种文件格式的解析来展...

高德地图-搜索服务-POI搜索

高德地图-搜索服务-POI搜索 之前公司项目收货地址仿饿了么的收货地址,结果发现自己实现的关键字搜索和周边搜索,搜索到的poi列表跟饿了么的并不完全一样,后来考虑了下,应该是搜索的范围、类型之类的设置的不一样。后来搜索了下,也的确是这样,,现在记录下来,留给自己也留给大家。 关键字搜索 周边搜索 区域搜索 高德地图 Android SDK 提供了千万级别...

springmvc多环境配置-profiles

1 pom.xml添加profiles标签 <profiles> <!--pro 线上环境--> <profile> <id>hdapp_pro</id> <properties> <db.main.url>jdbc...

Servlet3.0与springmvc那些事

 官方文档:https://docs.spring.io/spring/docs/5.0.2.RELEASE/spring-framework-reference/web.html#mvc-servlet-context-hierarchy 以前开发web工程:servlet、filter、listener都需要在web.xml进行注册,包括springm...

POI解析word文件,并为特定规则的key替换值

转载:https://www.aliyun.com/jiaocheng/778166.html 模板替换内容key是: ${enforcername1} package com.jsy.test.pdf; import java.io.FileOutputStream;import java.io.IOException;import java.ut...

解决java POI导入Excel超时问题

由于要导入大量数据,后台会耗费很长时间,导致超时。 本项目前端request.js中设定的超时时间为150s. const service =axios.create({ baseURL: baseUrl, withCredentials: true, timeout: 150000}); 我们的做法是: 前端导入Excel向后台发...