为fastdfs工具类添加注释

This commit is contained in:
RenKaiGe-Office
2018-05-17 18:01:03 +08:00
parent 5ecb09dd0f
commit d825c96145
5 changed files with 45 additions and 32 deletions

View File

@@ -8,6 +8,8 @@ import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -15,7 +17,7 @@ import org.springframework.http.ResponseEntity;
/**
* <p>Title: FileManager.java</p>
* <p>Description: </p>
// * <p>Description: 上传文件到fdfs的工具类</p>
* <p>Company: IIE</p>
* @author rkg
* @date 2018年5月16日
@@ -25,6 +27,7 @@ import org.springframework.http.ResponseEntity;
public class FileManager extends FileManagerConfig {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(FileManager.class);
private static TrackerClient trackerClient;
private static TrackerServer trackerServer;
private static StorageServer storageServer;
@@ -39,15 +42,15 @@ public class FileManager extends FileManagerConfig {
trackerServer = trackerClient.getConnection();
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
logger.error("创建tracker|storage失败,请检查配置文件或fdfs服务,nginx服务是否正常", e);
}
}
/**
* 向fastdfs上传文件
* @param file
* @param valuePairs
* @return
* @param file 文件对象
* @param valuePairs 设置meta信息,这个可以为空,设置了好像也没有什么用
* @return 返回文件在fdfs上的http访问地址(可直接在浏览器下载),null代表上传失败
*/
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
String[] uploadResults = null;
@@ -55,25 +58,25 @@ public class FileManager extends FileManagerConfig {
// uploadResults = storageClient.upload_file(file.getContent(), file.getExt(),
// valuePairs);
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);
String groupName = uploadResults[0];// group名称:group1|group2...
String remoteFileName = uploadResults[1];// 文件路径:M00/00/01/CgAGwFr9LTiAEoSaAADz3NN2rlY365.jpg
String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR
// + trackerServer.getInetSocketAddress().getHostName()
// + SEPARATOR + TRACKER_NGNIX_PORT
+ SEPARATOR + groupName + SEPARATOR + remoteFileName;
return fileAbsolutePath;
} catch (Exception e) {
e.printStackTrace();
logger.error("上传文件{}到fastfds服务器失败,请检查配置文件或fdfs服务,nginx服务是否正常", file.getName(), e);
}
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];
String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR
// + trackerServer.getInetSocketAddress().getHostName()
// + SEPARATOR + TRACKER_NGNIX_PORT
+ SEPARATOR + groupName + SEPARATOR + remoteFileName;
return fileAbsolutePath;
return null;
}
/**
* 从fastdfs下载文件
* @param groupName
* @param remoteFileName
* @param specFileName
* @return
* @param groupName 文件所属组:group1
* @param remoteFileName 文件路径:M00/00/01/CgAGwFr9LriANfj6AADz3NN2rlY448.jpg
* @param specFileName 重命名文件:对CgAGwFr9LriANfj6AADz3NN2rlY448.jpg重命名,例如重命名为:1.jpg
* @return
*/
public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {
byte[] content = null;
@@ -84,8 +87,7 @@ public class FileManager extends FileManagerConfig {
new String(specFileName.getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("从fastfds服务器下载文件{}失败,请检查配置文件或fdfs服务,nginx服务是否正常", remoteFileName, e);
}
return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
}
@@ -101,7 +103,7 @@ public class FileManager extends FileManagerConfig {
try {
result = storageClient.delete_file(group, filePath);
} catch (Exception e) {
e.printStackTrace();
logger.error("删除文件:{}失败,所属组:{},请检查配置文件或fdfs服务,nginx服务是否正常", filePath, group, e);
}
return result;
}