FileManager.java 6.5 KB

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