FileManager.java 6.6 KB

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