java上传图片工具类

矫情吗;* 2022-11-21 04:11 290阅读 0赞
  1. package com.findmiThree.utils.uploadimg;
  2. import java.io.File;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import org.springframework.web.multipart.MultipartFile;
  6. /**
  7. * 上传图片工具类
  8. *
  9. * @author Administrator
  10. *
  11. */
  12. public class TaoGouUpLoadImgUtils {
  13. /**
  14. *
  15. * @param filePath //上传文件磁盘路径
  16. * @param files //文件流
  17. * @return
  18. */
  19. public static String uploadImg(String filePath, MultipartFile files, String typePath) {
  20. String imgUrl = "";
  21. try {
  22. // 获取文件原始名称
  23. String fileName = files.getOriginalFilename();
  24. // 时间戳重新命名
  25. String dataTimes = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
  26. fileName = dataTimes + "_" + fileName;
  27. String savePath = filePath + typePath + "/" + dataTimes;
  28. File myfilePath = new File(savePath);
  29. if (!myfilePath.exists()) {
  30. myfilePath.mkdirs();
  31. }
  32. savePath = savePath + "/" + fileName;
  33. File targetFile = new File(savePath);
  34. files.transferTo(targetFile);
  35. imgUrl = "/" + typePath + "/" + dataTimes + "/" + fileName;
  36. if (!targetFile.exists()) {
  37. targetFile.mkdir();
  38. } else {
  39. return imgUrl;
  40. }
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. return imgUrl;
  45. }
  46. }

发表评论

表情:
评论列表 (有 0 条评论,290人围观)

还没有评论,来说两句吧...

相关阅读