1
0

FileManager.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.kmall.common.fileserver.util;
  2. import com.kmall.common.fileserver.common.NameValuePair;
  3. import com.kmall.common.fileserver.fastdfs.*;
  4. import com.kmall.common.fileserver.minio.MinIOUtils;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.apache.log4j.Logger;
  8. import org.springframework.http.HttpHeaders;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import java.io.IOException;
  14. import java.io.Serializable;
  15. import java.util.UUID;
  16. public class FileManager implements Serializable {
  17. private static final long serialVersionUID = -9042217554595446459L;
  18. public static final String PROTOCOL = "http://";
  19. public static final String SEPARATOR = "/";
  20. public static final String COLON = ":";
  21. /**
  22. * 配置文件名字
  23. */
  24. public static final String CLIENT_CONFIG_FILE = "conf/fastdfs.properties";
  25. private static Log logger = LogFactory.getLog(FileManager.class);
  26. private static TrackerClient trackerClient;
  27. private static TrackerServer trackerServer;
  28. private static StorageServer storageServer;
  29. private static StorageClient storageClient;
  30. static {
  31. System.out.println("FileManager");
  32. try {
  33. String fdfsClientConfigFilePath = CLIENT_CONFIG_FILE;
  34. ClientGlobal.init(fdfsClientConfigFilePath);
  35. trackerClient = new TrackerClient();
  36. trackerServer = trackerClient.getConnection();
  37. storageClient = new StorageClient(trackerServer, storageServer);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * <strong>方法概要: 文件上传</strong> <br>
  44. * <strong>创建时间: 2016-9-26 上午10:26:11</strong> <br>
  45. *
  46. * @param attach file
  47. * @return fileAbsolutePath
  48. * @author Wang Liang
  49. */
  50. public static String upload(MultipartFile attach) {
  51. String[] uploadResults = null;
  52. String ext = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".") + 1);
  53. try {
  54. FastDFSFile file = new FastDFSFile(attach.getBytes(), ext);
  55. NameValuePair[] meta_list = new NameValuePair[4];
  56. meta_list[0] = new NameValuePair("fileName", attach.getOriginalFilename());
  57. meta_list[1] = new NameValuePair("fileLength", String.valueOf(attach.getSize()));
  58. meta_list[2] = new NameValuePair("fileExt", ext);
  59. meta_list[3] = new NameValuePair("fileAuthor", ClientGlobal.file_author);
  60. synchronized (storageClient) {
  61. uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
  62. }
  63. } catch (Exception e1) {
  64. e1.printStackTrace();
  65. }
  66. String groupName = uploadResults[0];
  67. String remoteFileName = uploadResults[1];
  68. String httpAddr = ClientGlobal.http_tracket_nginx_addr;
  69. String httpPort = ClientGlobal.http_tracket_server_port;
  70. String fileAbsolutePath = PROTOCOL + httpAddr + COLON + httpPort + SEPARATOR + groupName + SEPARATOR + remoteFileName;
  71. return fileAbsolutePath;
  72. }
  73. /**
  74. * <strong>方法概要: 文件上传</strong> <br>
  75. * <strong>创建时间: 2016-9-26 上午10:26:11</strong> <br>
  76. *
  77. * @param attach file
  78. * @return fileAbsolutePath
  79. * @author Wang Liang
  80. */
  81. public static String uploadToMinIO(MultipartFile attach) {
  82. try {
  83. return MinIOUtils.upload(attach).getUri();
  84. } catch (IOException e) {
  85. logger.error(e);
  86. return "";
  87. }
  88. }
  89. /**
  90. * <strong>方法概要: 文件上传</strong> <br>
  91. * <strong>创建时间: 2016-9-26 上午10:26:11</strong> <br>
  92. *
  93. * @return fileAbsolutePath
  94. * @author Wang Liang
  95. */
  96. public static String upload(String originalFileName, byte[] content, String fileLength) {
  97. String[] uploadResults = null;
  98. String ext = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
  99. try {
  100. FastDFSFile file = new FastDFSFile(content, ext);
  101. NameValuePair[] meta_list = new NameValuePair[4];
  102. meta_list[0] = new NameValuePair("fileName", originalFileName);
  103. meta_list[1] = new NameValuePair("fileLength", fileLength);
  104. meta_list[2] = new NameValuePair("fileExt", ext);
  105. meta_list[3] = new NameValuePair("fileAuthor", ClientGlobal.file_author);
  106. synchronized (storageClient) {
  107. uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
  108. }
  109. } catch (Exception e1) {
  110. e1.printStackTrace();
  111. }
  112. String groupName = uploadResults[0];
  113. String remoteFileName = uploadResults[1];
  114. String httpAddr = ClientGlobal.http_tracket_nginx_addr;
  115. String httpPort = ClientGlobal.http_tracket_server_port;
  116. String fileAbsolutePath = PROTOCOL + httpAddr + COLON + httpPort + SEPARATOR + groupName + SEPARATOR + remoteFileName;
  117. return fileAbsolutePath;
  118. }
  119. /**
  120. * <strong>方法概要: 文件下载</strong> <br>
  121. * <strong>创建时间: 2016-9-26 上午10:28:21</strong> <br>
  122. *
  123. * @return returned value comment here
  124. * @author Wang Liang
  125. */
  126. public static ResponseEntity<byte[]> download(String filePath, String fileName) {
  127. byte[] content = null;
  128. HttpHeaders headers = new HttpHeaders();
  129. String substr = filePath.substring(filePath.indexOf("group"));
  130. String groupName = substr.split("/")[0];
  131. String remoteFileName = substr.substring(substr.indexOf("/") + 1);
  132. String specFileName = substr.substring(substr.indexOf("."));
  133. try {
  134. if (fileName == null || fileName.trim().equals("")) {
  135. fileName = UUID.randomUUID() + specFileName;
  136. } else {
  137. fileName = fileName + specFileName;
  138. }
  139. content = storageClient.download_file(groupName, remoteFileName);
  140. headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
  141. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. }
  145. return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
  146. }
  147. public static FileInfo getFile(String filePath) {
  148. String substr = filePath.substring(filePath.indexOf("group"));
  149. String groupName = substr.split("/")[0];
  150. String remoteFileName = substr.substring(substr.indexOf("/") + 1);
  151. try {
  152. return storageClient.get_file_info(groupName, remoteFileName);
  153. } catch (IOException e) {
  154. logger.error("IO Exception: Get File from Fast DFS failed", e);
  155. } catch (Exception e) {
  156. logger.error("Non IO Exception: Get File from Fast DFS failed", e);
  157. }
  158. return null;
  159. }
  160. }