安装流程:《FastDFS在Linux上的安装》,其中包括了和nginx的整合步骤。
jar包:
commons-io、commons-fileupload、fastdfs-client-java.jar
前两个是文件管理不可少的jar包,比较常见。第三个是在java中使用FastDFS时需要的一个官方jar包。也 可以下载源代码,可以将源代码添加到项目工程中,使用maven install命令将其安装到本地仓库后在项目中依 赖使用(maven中没有改jar包)。
上传流程:
1、初始化全局配置。加载一个配置文件。主要是配置tracker_server地址
2、创建一个TrackerClient对象。
3、创建一个TrackerServer对象。
4、声明一个StorageServer对象,null。
5、获得StorageClient对象。
6、直接调用StorageClient对象方法上传文件即可
具体实现:
1、添加配置文件,配置tracker_server地址,ip为tracker安装的服务器,端口号一般22122。
upload_file方法返回的结果为:组名+文件名
实例中用到的 upload_file 方法 API——
访问:ip/组名/文件名,如下图为上传的启动nginx、tracker server及storage server命令的截图。
下载实现:
实例中用到的FileOutputStream 方法API——
其他上传和下载的api可自行查询,包括批量上传等。
jar包:
commons-io、commons-fileupload、fastdfs-client-java.jar
前两个是文件管理不可少的jar包,比较常见。第三个是在java中使用FastDFS时需要的一个官方jar包。也 可以下载源代码,可以将源代码添加到项目工程中,使用maven install命令将其安装到本地仓库后在项目中依 赖使用(maven中没有改jar包)。
上传流程:
1、初始化全局配置。加载一个配置文件。主要是配置tracker_server地址
2、创建一个TrackerClient对象。
3、创建一个TrackerServer对象。
4、声明一个StorageServer对象,null。
5、获得StorageClient对象。
6、直接调用StorageClient对象方法上传文件即可
具体实现:
1、添加配置文件,配置tracker_server地址,ip为tracker安装的服务器,端口号一般22122。
//2、初始化全局配置。加载一个配置文件。主要是配置tracker_server地址
ClientGlobal.init("F:\\计算机\\Java\\07_淘淘\\projectyxf\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
//3、创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 4、创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 6、获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//7、直接调用StorageClient对象方法上传文件即可
String[] strings=storageClient.upload_file("C:\\Users\\xiaofei\\Desktop\\1.png", "png", null);
for (String string : strings) {
System.out.println(string);
}
upload_file方法返回的结果为:组名+文件名
实例中用到的 upload_file 方法 API——
upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException
upload file to storage server (by file name)
Parameters:
local_filename local filename to upload
file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
meta_list meta info array
Returns:
2 elements string array if success:
□ results[0]: the group name to store the file
□ results[1]: the new created filename
return null if fail
访问:ip/组名/文件名,如下图为上传的启动nginx、tracker server及storage server命令的截图。
下载实现:
@Test
public void Download() throws Exception{
try {
ClientGlobal.init("F:\\计算机\\Java\\07_淘淘\\projectyxf\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file("group1", "M00/00/00/wKipHlhYl8GAWpvUAAAmlNozzbU177.png");
IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".png"));
}
catch (Exception e) {
e.printStackTrace();
}
}
实例中用到的FileOutputStream 方法API——
FileOutputStream(String name) throws FileNotFoundException
Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters:
name the system-dependent filename
其他上传和下载的api可自行查询,包括批量上传等。