File upload + Fastdfs

1、Fastdfs

concept:

  • Is a high-performance FastDFSFile server clusterHe provides file upload, download and other services. (Uniform treatment of the files with him,In general micro-services, we put it to the picture uniform treatmentHe is a single Micro service)

1.1 linux operating system installation do (Configuration) FastDFS

Fastdfs are generally used to do gymnastics performed in linux (equivalent fastdfs installed on linux, and then upload pictures to fastdfs)

  • Configuration fastdfs (cmd performed on the terminal linux, execute the following command)

1. Modify client.conf

vi /etc/fdfs/client.conf -> to modify the current virtual machine ip ip

2. Modify storage.conf

vi /etc/fdfs/storage.conf -> to modify the current virtual machine ip ip

3. Start fastdfs

# Note that two should start

service fdfs_trackerd start
service fdfs_storaged start

4. Check whether a successful start

netstat -unltp | grep FDF

5. Review the monitoring information

/ Usr / bin / fdfs_monitor /etc/fdfs/storage.conf

6. Start Nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

7. Turn off the firewall / firewall open port 80

service iptables stop

Or execution:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

/etc/rc.d/init.d/iptables save

Note: The network using bridge mode

8. Test Nginx

Inside the virtual machine: curl http://127.0.0.1:80

In the host Test: browser access http://127.0.0.1:80

9. Test upload (upload to fastdfs)

fdfs_test /etc/fdfs/client.conf upload / root / virtual machine inside the picture .jpg

1.2 Test simple java code of Hello

1 jar packaging

  • Manual to the Internet to download the source code and the source code package to fight jar maven repository

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

  • After downloading this source into the root directory and then execute the following command cmd can automatically package

Use maven install from source: mvn clean install

2, the leader packet

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

3. Add Profilefdfs_client.conf , Which will be set as the server addressVirtual machine ip.22122 (the latter is not a port)
// ...
tracker_server = 192.168.25.133: 22122
// ...

 // 1、加载配置文件,配置文件中的内容就是 tracker 服务的地址。
		ClientGlobal.init("D:/maven_work/fastDFS-demo/src/fdfs_client.conf");
		// 2、创建一个 TrackerClient 对象。直接 new 一个。
		TrackerClient trackerClient = new TrackerClient();
		// 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
		TrackerServer trackerServer = trackerClient.getConnection();
		// 4、创建一个 StorageServer 的引用,值为 null
		StorageServer storageServer = null;
		// 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		// 6、使用 StorageClient 对象上传图片。
		//扩展名不带“.”
		String[] strings = storageClient.upload_file("D:/pic/benchi.jpg", "jpg",
				null);
		// 7、返回数组。包含组名和图片的路径。
		for (String string : strings) {
			System.out.println(string);
		}

Console output the following results:

group1
M00/00/00/wKgZhVkMP4KAZEy-AAA-tCf93Fo973.jpg

In the browser, type:

http://192.168.25.133/group1/M00/00/00/wKgZhVkMP4KAZEy-AAA-tCf93Fo973.jpg

Published 20 original articles · won praise 4 · Views 2088

Guess you like

Origin blog.csdn.net/weixin_45028726/article/details/104579427