fastdfs java客户端操作

https://github.com/happyfish100/fastdfs-client-java

到此处下载下来demo

这里采用maven的方式

mvn clean install

上传到本地仓库

mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.29-SNAPSHOT -Dpackaging=jar -Dfile=fastdfs-client-java-1.29-SNAPSHOT.jar

引入文件上传依赖

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29-SNAPSHOT</version>
</dependency>


项目结构

 

 代码如下:

 

@RestController
public class AttachmentController {

@RequestMapping("/upload")
public String fdfsUpload(@RequestParam MultipartFile file){
try {
ClientGlobal.initByProperties("fdfs-client.properties");
System.out.println("初始化信息:"+ClientGlobal.configInfo());
// 链接FastDFS服务器,创建tracker和Stroage
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient storageClient = new StorageClient(trackerServer,storageServer);
String tempFileName = file.getOriginalFilename();
String fileExtName = tempFileName.substring(tempFileName.lastIndexOf(".")+1);
//上传文件,只需要文件字节数组,第二个参数是文件扩展名,第三个参数是元数据
String[] strings = storageClient.upload_file(file.getBytes(), fileExtName, null);
String result="";
for (int i=0;i<strings.length;i++){

if(i==strings.length-1){
result+=strings[i];
}else{
result+=strings[i]+"/";
}

}
System.out.println(result);


} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* FastDFS实现文件下载
*
* @param filePath
*/
@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public void fdfsDownload(String filePath) {
try {
ClientGlobal.initByProperties("fdfs-client.properties");

// 链接FastDFS服务器,创建tracker和Stroage
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient storageClient = new StorageClient(trackerServer,storageServer);
byte[] b = storageClient.download_file("group1", filePath);
if (b == null) {
throw new IOException("文件" + filePath + "不存在");
}

String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
FileOutputStream fileOutputStream = new FileOutputStream("E://" + fileName);
IOUtils.write(b, fileOutputStream);
fileOutputStream.close();
System.out.println("文件下载成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* FastDFS实现文件删除
*
* @param filePath
*/
@RequestMapping(value = "/delete", method = RequestMethod.GET)
@ResponseBody
public void fdfsDelete(String filePath) {
try {
ClientGlobal.initByProperties("fdfs-client.properties");

TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient storageClient = new StorageClient(trackerServer,storageServer);
int deleteResult = storageClient.delete_file("group1", filePath);

if (deleteResult== 0) {
System.out.println("文件删除成功");
}else {
System.out.println("文件删除失败");
}



} catch (Exception e) {
e.printStackTrace();
}
}


}


亲测可用
posted @ 2020-04-26 16:47  change丶fl  阅读(831)  评论(0编辑  收藏  举报