FIrst:POM
<dependency><groupId>net.oschina.zcx7878</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27.0.0</version>
</dependency>
second:create Maven Project 并准备配置文件(来源:导入的pom包下有)
## fastdfs-client.properties
file_server_addr = 192.168.109.132:80
connect_timeout_in_seconds = 5
network_timeout_in_seconds = 30charset = UTF-8http_anti_steal_token = true
http_secret_key = FastDFS1234567890
http_tracker_http_port = 8080tracker_server = 192.168.109.132:22122
##根据自己的环境配置
编写java操作fdfs工具类
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
//初始化实例(tracker、storage)
public class FastDfsUtil {private static TrackerClient trackerClient = null;private static TrackerServer trackerServer = null;private static StorageServer storageServer = null;private static StorageClient storageClient = null;static {try {ClientGlobal.init("fdfs_client.conf");trackerClient = new TrackerClient();trackerServer = trackerClient.getConnection();storageClient = new StorageClient(trackerServer, storageServer);} catch (Exception e) {e.printStackTrace();System.out.println("FDFS工具初始化失败!");}}
}
上传工具类方法
/*** @Title: fdfsUpload* @Description: 通过文件流上传文件* @param @param inputStream 文件流* @param @param filename 文件名称* @param @throws IOException* @param @throws MyException* @return String 返回文件在FastDfs的存储路径*/public String fdfsUpload(InputStream inputStream, String filename) throws IOException, MyException{String suffix = ""; //后缀名try{suffix = filename.substring(filename.lastIndexOf(".")+1);}catch (Exception e) {throw new RuntimeException("参数filename不正确!格式例如:a.png");}String savepath = ""; //FastDfs的存储路径ByteArrayOutputStream swapStream = new ByteArrayOutputStream();byte[] buff = new byte[1024];int len = 0;while ((len = inputStream.read(buff)) != -1) {swapStream.write(buff, 0, len);}byte[] in2b = swapStream.toByteArray();String[] strings = storageClient.upload_file(in2b, suffix, null); //上传文件for (String str : strings) {savepath += "/" + str; //拼接路径}return savepath;}/*** @Title: fdfsUpload* @Description: 本地文件上传* @param @param filepath 本地文件路径* @param @throws IOException* @param @throws MyException* @return String 返回文件在FastDfs的存储路径*/public static String fdfsUpload(String filepath) throws IOException, MyException{String suffix = ""; //后缀名try{suffix = filepath.substring(filepath.lastIndexOf(".")+1);}catch (Exception e) {throw new RuntimeException("上传的不是文件!");}String savepath = ""; //FastDfs的存储路径String[] strings = storageClient.upload_file(filepath, suffix, null); //上传文件for (String str : strings) {savepath += "/" + str; //拼接路径}return savepath;}
下载方法
/*** @Title: fdfsDownload* @Description: 下载文件到目录* @param @param savepath 文件存储路径* @param @param localPath 下载目录* @param @throws IOException* @param @throws MyException* @return boolean 返回是否下载成功*/public static boolean fdfsDownload(String savepath, String localPath) throws IOException, MyException{String group = ""; //存储组String path = ""; //存储路径try{int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置group = savepath.substring(1, secondindex); //类似:group1path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png}catch (Exception e) {throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");}int result = storageClient.download_file(group, path, localPath);if(result != 0){throw new RuntimeException("下载文件失败:文件路径不对或者文件已删除!");}return true;}/*** @Title: fdfsDownload* @Description: 返回文件字符数组* @param @param savepath 文件存储路径* @param @throws IOException* @param @throws MyException* @return byte[] 字符数组*/public static byte[] fdfsDownload(String savepath) throws IOException, MyException{byte[] bs = null;String group = ""; //存储组String path = ""; //存储路径try{int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置group = savepath.substring(1, secondindex); //类似:group1path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png}catch (Exception e) {throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");}bs = storageClient.download_file(group, path); //返回byte数组return bs;}
删除文件服务器文件
/*** @Title: fdfsDeleteFile* @Description: 删除文件* @param @param savepath 文件存储路径* @param @throws IOException* @param @throws MyException* @return boolean 返回true表示删除成功*/public static boolean fdfsDeleteFile(String savepath) throws IOException, MyException{String group = ""; //存储组String path = ""; //存储路径try{int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置group = savepath.substring(1, secondindex); //类似:group1path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png}catch (Exception e) {throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");}int result = storageClient.delete_file(group, path); //删除文件,0表示删除成功if(result != 0){throw new RuntimeException("删除文件失败:文件路径不对或者文件已删除!");}return true;}
获取文件信息
/*** @Title: fdfdFileInfo* @Description: 返回文件信息* @param @param savepath 文件存储路径* @param @throws IOException* @param @throws MyException* @return FileInfo 文件信息*/public static FileInfo fdfdFileInfo(String savepath) throws IOException, MyException{String group = ""; //存储组String path = ""; //存储路径try{int secondindex = savepath.indexOf("/", 2); //第二个"/"索引位置group = savepath.substring(1, secondindex); //类似:group1path = savepath.substring(secondindex + 1); //类似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png}catch (Exception e) {throw new RuntimeException("传入文件存储路径不正确!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");}FileInfo fileInfo = storageClient.get_file_info(group, path);return fileInfo;}