springBoot的多个图片上传

本文介绍了一个基于SpringBoot的图片批量上传控制器的实现细节,包括如何使用MultipartFile处理多文件上传,通过UUID确保文件名唯一,以及如何检查并保存图片文件。同时,文章提供了完整的代码示例和配置参数。
  1. package com.example.demo.controller;
  2. import org.apache.commons.io.FileUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.*;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import javax.servlet.http.HttpServletRequest;
  8. import java.io.BufferedOutputStream;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.UUID;
  12. @RequestMapping("/api/picture")
  13. @RestController
  14. public class PicUploadController {
  15. private static final Logger logger = LoggerFactory.getLogger(PicUploadController.class);
  16. //请求订单列表
  17. @ResponseBody
  18. @RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
  19. public String getOrderDtl(@RequestParam(value = "file") MultipartFile files[], HttpServletRequest request) {
  20. logger.info("图片批量上传,files[]=",files);
  21. // String uploadPath = request.getSession().getServletContext().getRealPath("/") + "/upload";
  22. String uploadPath = "C:\\test\\upload";
  23. File uploadDirectory = new File(uploadPath);
  24. if (uploadDirectory.exists()) {
  25. if (!uploadDirectory.isDirectory()) {
  26. uploadDirectory.delete();
  27. }
  28. } else {
  29. uploadDirectory.mkdir();
  30. }
  31. //这里可以支持多文件上传
  32. if (files != null && files.length >= 1) {
  33. BufferedOutputStream bw = null;
  34. try {
  35. for (MultipartFile file : files) {
  36. String fileName = file.getOriginalFilename();
  37. //判断是否有文件且是否为图片文件
  38. if(fileName!=null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) {
  39. //创建输出文件对象
  40. File outFile = new File(uploadPath + "/" + UUID.randomUUID().toString()+ getFileType(fileName));
  41. //拷贝文件到输出文件对象
  42. FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
  43. }
  44. }
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. } finally {
  48. try {
  49. if (bw != null) {
  50. bw.close();
  51. }
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. return "upload successful";
  58. }
  59. private Boolean isImageFile(String fileName) {
  60. String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"};
  61. if (fileName == null) {
  62. return false;
  63. }
  64. fileName = fileName.toLowerCase();
  65. for (String type : img_type) {
  66. if (fileName.endsWith(type)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * 获取文件后缀名
  74. *
  75. * @param fileName
  76. * @return
  77. */
  78. private String getFileType(String fileName) {
  79. if (fileName != null && fileName.indexOf(".") >= 0) {
  80. return fileName.substring(fileName.lastIndexOf("."), fileName.length());
  81. }
  82. return "";
  83. }
  84. }

 

 

  1. 环境为springboot2.0.6
  2. spring.servlet.multipart.enabled=true
  3. spring.servlet.multipart.max-file-size=10MB
  4. spring.servlet.multipart.max-request-size=100MB
  5. spring.servlet.multipart.resolve-lazily=false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值