基于spring boot的图片上传接口及生成32为数据库的id工具类
import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; 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 java.io.File; import java.util.ArrayList; import java.util.List; /** * 2019/8/4 * 图片上传控制器 */ @RestController @RequestMapping("/client/image") public class ImageUploadController { @Value("${basicsUploadPath}") String base; @PostMapping("/upload") public MsgResponse upload(@RequestParam MultipartFile images[]){ File pFile = new File(base); if (!pFile.exists()) { pFile.mkdirs(); } MsgResponse msgResponse=new MsgResponse(); List<String> imageList = new ArrayList<>(); //这里可以支持多文件上传 if (images != null && images.length >= 1) { String f; String f1; try { for (MultipartFile file : images) { String fileName = file.getOriginalFilename(); // System.out.println("---fileName"+fileName); //判断是否有文件且是否为图片文件 if(fileName!=null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) { //创建输出文件对象 f1=IdGen.uuid()+ getFileType(fileName); f=base + "/" + f1; // System.out.println("f"+f+"\tgetFileType"+getFileType(fileName)); File outFile = new File(f); //拷贝文件到输出文件对象 FileUtils.copyInputStreamToFile(file.getInputStream(), outFile); imageList.add(f1); } } } catch (Exception e) { return msgResponse.setMsg("上传图片失败!"); } return msgResponse.setSuccess(true).setMsg("图片上传成功").setData(imageList); } return msgResponse.setMsg("请选择图片"); } /** * 判断文件是否为图片 * @param fileName * @return */ private Boolean isImageFile(String fileName) { String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp","svg",".dib",".tif",".ico", ".tiff"}; if (fileName == null) { return false; } fileName = fileName.toLowerCase(); for (String type : img_type) { //endsWith() 方法用于测试字符串是否以指定的后缀结束。 参考 https://www.runoob.com/java/java-string-endswith.html if (fileName.endsWith(type)) { return true; } } return false; } /** * 获取文件的后缀名 * @param fileName * @return */ private String getFileType(String fileName) { if (fileName != null && fileName.indexOf(".") >= 0) { return fileName.substring(fileName.lastIndexOf("."), fileName.length()); } return ""; } }
application.properties配置
文件上传配置
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
spring.servlet.multipart.resolve-lazily=false
生成32位数据库id字段的工具类
import org.apache.shiro.session.Session; import org.apache.shiro.session.mgt.eis.SessionIdGenerator; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.io.Serializable; import java.security.SecureRandom; import java.util.UUID; /** * 封装各种生成唯一性ID算法的工具类. * @author ThinkGem * @version 2013-01-15 */ @Service @Lazy(false) public class IdGen implements SessionIdGenerator { private static SecureRandom random = new SecureRandom(); /** * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割. */ public static String uuid() { return UUID.randomUUID().toString().replaceAll("-", ""); } /** * 使用SecureRandom随机生成Long. */ public static long randomLong() { return Math.abs(random.nextLong()); } /** * 基于Base62编码的SecureRandom随机生成bytes. */ public static String randomBase62(int length) { byte[] randomBytes = new byte[length]; random.nextBytes(randomBytes); return Encodes.encodeBase62(randomBytes); } @Override public Serializable generateId(Session session) { return IdGen.uuid(); } public static void main(String[] args) { System.out.println(IdGen.uuid()); System.out.println(IdGen.uuid().length()); for (int i=0; i<1000; i++){ System.out.println(IdGen.randomLong() + " " + IdGen.randomBase62(5)); } } }

浙公网安备 33010602011771号