分散ファイルストレージシステムFastDFSの使用
9581 ワード
一、資料の参考
二、具体的なコード1 pomファイル構成(centerライブラリにはfastdfsのコードライブラリがないためgithubのコードをダウンロードしmavenローカルライブラリにアップロードする必要がある)
②Controller
③Beanクラス
④工具類
https://www.sohu.com/a/224411047_465959
https://blog.csdn.net/lonelymanontheway/article/details/80777849
https://www.cnblogs.com/hujunwei/p/11341614.html
https://www.cnblogs.com/ityouknow/p/8240976.html
https://github.com/happyfish100/fastdfs/wiki
https://www.cnblogs.com/handsomeye/p/9451568.html
springboot fastDFS
https://www.cnblogs.com/felixzh/p/10721964.html
https://www.cnblogs.com/hujunwei/p/11341614.html
二、具体的なコード1 pomファイル構成(centerライブラリにはfastdfsのコードライブラリがないためgithubのコードをダウンロードしmavenローカルライブラリにアップロードする必要がある)
org.csource
fastdfs-client-java
1.29-SNAPSHOT
②Controller
@RestController
@CrossOrigin
@RequestMapping(value = "sde/work/fastdfs/")
public class FileController {
/**
* @param groupName :group1( traker, ?????)
* @param remoteFileName :M00/00/00/Cqo6oF9GMf2AIEd9AABzjFPTGsI80..jpg
* @return
*/
@GetMapping("/query")
public FileInfo query(String groupName, String remoteFileName) {
try {
return FastDFSClient.getFile(groupName, remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
String path = "";
try {
path = FastDFSClient.saveFile(file);
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
}
③Beanクラス
public class FastDFSFile {
//
private String name;
//
private byte[] content;
//
private String ext;
// MD5
private String md5;
//
private String author;
public FastDFSFile(String name, byte[] content, String ext,
String author) {
super();
this.name = name;
this.content = content;
this.ext = ext;
this.author = author;
}
public FastDFSFile(String name, byte[] content, String ext) {
super();
this.name = name;
this.content = content;
this.ext = ext;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
this.ext = ext;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
④工具類
public class FastDFSClient {
private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
/***
* FastDFS TrackerServer
*/
static {
try {
Properties props = new Properties();
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.170.58.160:22122");
ClientGlobal.initByProperties(props);
logger.info("FastDFS Client Init success!");
} catch (Exception e) {
logger.error("FastDFS Client Init Fail!", e);
}
}
/**
* MultipartFile
* @param multipartFile
* @return
* @throws IOException
*/
public static String saveFile(MultipartFile multipartFile) throws IOException {
//1.
String fileName = multipartFile.getOriginalFilename();
//2.
byte[] content = multipartFile.getBytes();
//3.
String ext = "";
if (fileName != null && !"".equals(fileName)) {
ext = fileName.substring(fileName.lastIndexOf("."));
}
//4.
FastDFSFile fastDFSFile = new FastDFSFile(fileName, content, ext,"lbing");
//5.
String[] uploadResults = FastDFSClient.upload(fastDFSFile);
//6. , uploadResults[0] , uploadResults[1]
String path = FastDFSClient.getTrackerUrl() + uploadResults[0] + "/" + uploadResults[1];
//7.
return path;
}
/***
*
* @param file
* @return
*/
public static String[] upload(FastDFSFile file) {
//
NameValuePair[] meta_list = new NameValuePair[1];
meta_list[0] = new NameValuePair("author", file.getAuthor());
//
String[] uploadResults = null;
StorageClient storageClient = null;
try {
// StorageClient
storageClient = getTrackerClient();
/***
*
* 1)
* 2)
* 3)
*/
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
} catch (Exception e) {
logger.error("Exception when uploadind the file:" + file.getName(), e);
}
if (uploadResults == null && storageClient != null) {
logger.error("upload file fail, error code:" + storageClient.getErrorCode());
}
//
String groupName = uploadResults[0];
//
String remoteFileName = uploadResults[1];
return uploadResults;
}
/***
*
* @param groupName:
* @param remoteFileName:
* @return
*/
public static FileInfo getFile(String groupName, String remoteFileName) {
try {
StorageClient storageClient = getTrackerClient();
return storageClient.get_file_info(groupName, remoteFileName);
} catch (Exception e) {
logger.error("Exception: Get File from Fast DFS failed", e);
}
return null;
}
/***
*
* @param groupName
* @param remoteFileName
* @return
*/
public static InputStream downFile(String groupName, String remoteFileName) {
try {
// StorageClient
StorageClient storageClient = getTrackerClient();
//
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
InputStream ins = new ByteArrayInputStream(fileByte);
return ins;
} catch (Exception e) {
logger.error("Exception: Get File from Fast DFS failed", e);
}
return null;
}
/***
*
* @param groupName
* @param remoteFileName
* @throws Exception
*/
public static void deleteFile(String groupName, String remoteFileName)
throws Exception {
// StorageClient
StorageClient storageClient = getTrackerClient();
//
int i = storageClient.delete_file(groupName, remoteFileName);
}
/***
* Storage
* @param groupName
* @return
* @throws IOException
*/
public static StorageServer[] getStoreStorages(String groupName)
throws IOException {
// TrackerClient
TrackerClient trackerClient = new TrackerClient();
// TrackerServer
TrackerServer trackerServer = trackerClient.getTrackerServer();
// Storage
try {
return trackerClient.getStoreStorages(trackerServer, groupName);
} catch (MyException e) {
e.printStackTrace();
return null;
}
}
/***
* Storage ,IP
* @param groupName
* @param remoteFileName
* @return
* @throws IOException
*/
public static ServerInfo[] getFetchStorages(String groupName,
String remoteFileName) throws IOException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
try {
return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
} catch (MyException e) {
e.printStackTrace();
return null;
}
}
/***
* Tracker
* @return
* @throws IOException
*/
public static String getTrackerUrl() throws IOException {
return "http://" + getTrackerServer().getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port() + "/";
}
/***
* Storage
* @return
* @throws IOException
*/
private static StorageClient getTrackerClient() throws IOException {
TrackerServer trackerServer = getTrackerServer();
StorageClient storageClient = new StorageClient(trackerServer, null);
return storageClient;
}
/***
* Tracker
* @return
* @throws IOException
*/
private static TrackerServer getTrackerServer() throws IOException {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
return trackerServer;
}
}