UploadFileService.java 4.0 KB

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