UploadFileService.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.keao.edu.user.service;
  2. import com.keao.edu.common.entity.UploadReturnBean;
  3. import com.keao.edu.common.exception.BizException;
  4. import com.keao.edu.thirdparty.storage.StoragePluginContext;
  5. import com.keao.edu.thirdparty.storage.provider.KS3StoragePlugin;
  6. import com.keao.edu.util.upload.UploadUtil;
  7. import org.apache.commons.io.FileUtils;
  8. import org.apache.commons.io.IOUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. /**
  18. * 上传工具服务层实现类
  19. */
  20. @Service
  21. public class UploadFileService {
  22. @Autowired
  23. private StoragePluginContext storagePluginContext;
  24. /** 最大上传大小,单位kb */
  25. @Value("${common.upload.maxSize:153600}")
  26. private int maxSize;
  27. /** 支持的扩展名 */
  28. @Value("${common.upload.supportExtensions:jpg,jpeg,gif,png,mp3,mid,midi,aac,m4a,mp4}")
  29. private String supportExtensions;
  30. /** 文件根目录 */
  31. @Value("/var/tmp/")
  32. private String fileRoot;
  33. public UploadReturnBean uploadFile(InputStream in, String ext) {
  34. UploadReturnBean uploadReturn = new UploadReturnBean("", false, "");
  35. String fileName = UploadUtil.getFileName(ext);
  36. String supportType = supportExtensions;
  37. if (!UploadUtil.validateImgFile(ext, supportType)) {
  38. uploadReturn.setMessage("上传图片格式错误,目前只支持" + supportType);
  39. return uploadReturn;
  40. }
  41. String root = fileRoot;
  42. if (StringUtils.isBlank(root)) {
  43. uploadReturn.setMessage("上传临时目录没有配置");
  44. return uploadReturn;
  45. }
  46. String staticFloder = "";
  47. String folder = UploadUtil.getFileFloder();
  48. String filePath = UploadUtil.getFilePath(root, staticFloder, folder);
  49. File file = uploadFile(in, filePath, fileName);
  50. if (maxSize > 0 && maxSize < file.length() / 1024) {
  51. FileUtils.deleteQuietly(file);
  52. uploadReturn.setMessage("超出允许的大小(" + (maxSize / 1024) + "M)限制");
  53. return uploadReturn;
  54. }
  55. String url = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME,staticFloder + folder, file);
  56. FileUtils.deleteQuietly(file);
  57. uploadReturn.setStatus(true);
  58. uploadReturn.setUrl(url);
  59. return uploadReturn;
  60. }
  61. public void setMaxSize(int maxSize) {
  62. this.maxSize = maxSize;
  63. }
  64. /**
  65. * 上传文件的工具方法
  66. * @param inputStream
  67. * @param filePath
  68. * @param fileName
  69. * @return
  70. */
  71. private File uploadFile(InputStream inputStream, String filePath, String fileName) {
  72. File file = new File(filePath + "/" + fileName);
  73. try {
  74. if (!file.getParentFile().exists()) {
  75. file.getParentFile().mkdirs();
  76. }
  77. FileOutputStream fos = new FileOutputStream(file);
  78. IOUtils.copy(inputStream, fos);
  79. if (!file.exists() || file.length() == 0) {
  80. throw new BizException("图片上传出现错误,请重新上传");
  81. }
  82. } catch (IOException e) {
  83. throw new BizException("图片上传失败", e);
  84. } finally {
  85. IOUtils.closeQuietly(inputStream);
  86. }
  87. return file;
  88. }
  89. }