这篇“怎么使用Java+Easyexcel实现文件上传功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用Java+E
这篇“怎么使用Java+Easyexcel实现文件上传功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用Java+EasyExcel实现文件上传功能”文章吧。
页面中当用户将excel表格上传到服务器后,将该excel文件保存在本地然后在服务器中将excel中的数据读取出来然后存入数据库
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.4</version></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastJSON</artifactId> <version>1.2.75</version></dependency> <!-- 糊涂工具类--><dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.11</version></dependency>
文件上传的路径:用户传来的文件存放在哪
# 文件上传spring: servlet: multipart: max-file-size: 5MB max-request-size: 50MB## 文件上传路径savepath: C:\Users\86186\Desktop\pp
文件上传时需要使用到的一些方法
public class FileUploadUtil { public static String getFileSuffix(String filename){ if(filename == null || filename.isEmpty()){ throw new RuntimeException("文件名不能为空,filename:"+filename); } return filename.substring(filename.lastIndexOf(".")); } public static String randomFilename(){ return UUID.randomUUID().toString().replaceAll("-",""); } public static String randomFilename2(){ return System.currentTimeMillis()+""; } public static String randomFilename3(){ return System.currentTimeMillis()+randomFilename(); } public static void mkdir(String path){ File file = new File(path); if(!file.exists()){ //不存在 file.mkdirs(); } } public static String getTimeFilename(){ SimpleDateFORMat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd-HH-mm-ss"); Date date = new Date(System.currentTimeMillis()); return simpleDateFormat.format(date)+"-"+randomFilename2(); }}
需要接受前端返回回来的文件
@Autowired private FileUploadService fileUploadService; @PostMapping("excelImport") public ResponseData excelImport(MultipartFile file) throws Exception { // 由于easyExcel所以需要传入fileUploadService对象 String upload = fileUploadService.upload(file,fileUploadService); return ResponseDataUtil.buildOk(upload); }
@Service("fileUploadService")@Slf4jpublic class FileUploadServiceImpl implements FileUploadService {// 注入environment来获取在配置文件中文件保存的路径 @Autowired private Environment environment; // 注入数据层的对象 @Autowired private productMapper productMapper; @Override public String upload(MultipartFile file, FileUploadService fileUploadService) throws Exception { if (file == null) { throw new Exception("文件不可以为空"); } //得到上传的保存路径 String savePath = environment.getProperty("savepath"); //创建目录 FileUploadUtil.mkdir(savePath); String dbPath = ""; //得到上传的原文件名 String originalFilename = file.getOriginalFilename(); String suffix = FileUploadUtil.getFileSuffix(originalFilename); String filename = FileUploadUtil.getTimeFilename() + suffix; dbPath += filename; //保存 file.transferTo(new File(savePath, filename)); dbPath = savePath +"\\"+ dbPath; //调用方法进行读取 EasyExcel.read(dbPath, ExcelDTO.class, new PageReadListener<ExcelDTO>(dataList -> { for (ExcelDTO demoData : dataList) { log.info("读取到一条数据{}", jsON.toJSONString(demoData)); insert(demoData); } })).sheet().doRead(); return dbPath; }// 插入数据到数据库中 @Override public void insert(ExcelDTO excelDTO) { // 使用hutool工具类将excelDTO类转换成product类,因为product类对应的是数据库中的字段 Product product = BeanUtil.copyProperties(excelDTO, Product.class); productMapper.insert(product); }}
@Datapublic class ExcelDTO { @ExcelProperty("药品名称") private String pname; @ExcelProperty("药品价格") private BigDecimal pprice; @ExcelProperty("药品数量") private String pcount; @ExcelProperty("药品描述") private String pdes; @ExcelProperty("药品类别") private Integer ptype;}
ExcelDTO = excel对应的类
fileUploadService = service对象
@Slf4j@Componentpublic class DataListener extends AnalysisEventListener<ExcelDTO> { public FileUploadService fileUploadService; public DataListener(FileUploadService fileUploadService) { this.fileUploadService = fileUploadService; } public DataListener() { } //读取excel内容,一行一行读取 @Override public void invoke(ExcelDTO excelDTO, AnalysisContext analysisContext) { if (excelDTO == null) { try { throw new Exception("文件数据为空"); } catch (Exception e) { e.printStackTrace(); } } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { }}
以上就是关于“怎么使用Java+EasyExcel实现文件上传功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。
--结束END--
本文标题: 怎么使用Java+EasyExcel实现文件上传功能
本文链接: https://www.lsjlt.com/news/349976.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0