In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to operate FastDFS in Java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
If you want to use Java to operate fastdfs, you need to first obtain the package that can operate it. The version of the fastdfs development package used this time is: fastdfs_client_v1.20.jar. At the same time, in order to facilitate configuration, a special client configuration file is usually configured when developing fastdfs, so this configuration file is usually saved in the classpath location, so in order to facilitate reading Use Spring to read resources directly
2.1. Upload files
1. Create a new project: FastProject, and configure the development package into the project
2. Create a configuration file for fdfs_client.conf under src, with the following contents:
Tracker_server=192.168.122.198:22122
3. Write a program to upload:
Package cn.mldn.demo;import java.io.File;import java.util.Arrays;import org.csource.common.NameValuePair;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.StorageClient;import org.csource.fastdfs.StorageServer;import org.csource.fastdfs.TrackerClient;import org.csource.fastdfs.TrackerServer;import org.springframework.core.io.ClassPathResource;publicc lassFDFSUploadDemo {public static void main (String [] args) throws Exception {/ / 1, set a path for uploading files File imgFile=new File ("D:" + File.separator+ "pic-c.jpg"); / / get the file extension String fileExtName=imgFile.getName (). Substring (imgFile.getName (). LastIndexOf (".") + 1); / / 2. Read the uploaded configuration file, which is ClassPathResource res=newClassPathResource ("fdfs_client.conf") under the CLASSPATH path; / / 3. Initialize the environment for FastDFS upload ClientGlobal.init (res.getClassLoader (). GetResource ("fdfs_client.conf"). GetPath ()) / / 4. Establish the client connection of Tracker TrackerClient tracker=new TrackerClient (); TrackerServert rackerServer=tracker.getConnection (); / / get the server connection / / 5, who is really responsible for data preservation is Storage StorageServer storageServer=null;//6, create StorageClient client=new StorageClient for StoragerClient (trackerServer,storageServer); / / 7, create the file metadata object NameValuePair [] metaList=new NameValuePair [3]; metaList [0] = newNameValuePair ("fileName", imgFile.getName ()); metaList [1] = newNameValuePair ("fileExtName", fileExtName) MetaList [2] = newNameValuePair ("fileLength", String.valueOf (imgFile.length (); / / 8. Upload the file and return the path name of the file String [] fileId=client.upload_file (imgFile.getPath (), fileExtName,metaList); System.out.println (Arrays.toString (fileId)); trackerServer.close ();}}
The uploaded file information returned at this time contains two items: the name of the saved group and the path of the saved image. Access path: http://192.168.122.198/group2/M00/00/00/wKh7ylg7zDCAZt1VAAL6dz7aOB0285.jpg, but it is too troublesome to upload. The funniest thing is that a bunch of methods are provided in FastDFS if you want to upload:
StorageClient1 client=newStorageClient1 (trackerServer,storageServer); StringfileId=client.upload_file1 (imgFile.getPath (), fileExtName,metaList)
As for choosing which kind of upload, it depends on your own preference.
2.2. Get document information
The file has been uploaded, so the relevant information of the file can also be obtained through the Fastdfs client. Example: get file information
Packagecn.mldn.demo;importorg.csource.fastdfs.ClientGlobal;importorg.csource.fastdfs.FileInfo;importorg.csource.fastdfs.StorageClient1;importorg.csource.fastdfs.StorageServer;importorg.csource.fastdfs.TrackerClient;importorg.csource.fastdfs.TrackerServer;importorg.springframework.core.io.ClassPathResource;publicclassFDFSInfoDemo {
Public static void main (String [] args) throwsException {/ / 1. Read the uploaded configuration file, which is ClassPathResource res=newClassPathResource ("fdfs_client.conf") under the CLASSPATH path; / / 2. Initialize the environment for FastDFS upload ClientGlobal.init (res.getClassLoader (). GetResource ("fdfs_client.conf"). GetPath ()); / / 3, establish a client connection to Tracker TrackerClient tracker=new TrackerClient (); TrackerServer trackerServer=tracker.getConnection () / / get the connection to the server / / 4. The one who is really responsible for saving the data is Storage StorageServerstorageServer=null;//5, create the StoragerClient StorageClient1client=newStorageClient1 (trackerServer,storageServer); / / 6, get the relevant information about the file StringfileName= "group2/M00/00/00/wKh7yVg7zQSAFqYWAAL6dz7aOB0479.jpg"; FileInfo fi=client.get_file_info1 (fileName); System.out.println ("get file size:" + fi.getFileSize ()); System.out.println ("creation date:" + fi.getCreateTimestamp ()) System.out.println ("Real Save Host:" + fi.getSourceIpAddr ()); trackerServer.close ();}}
Now all it gets is the file information that is actually saved on the storage server.
2.3. Delete files
Be sure to delete the contents of the corresponding picture when some information is no longer in use, so let's take a look at the deletion of the file. Example: delete a file.
Importorg.csource.fastdfs.ClientGlobal;importorg.csource.fastdfs.StorageClient1;importorg.csource.fastdfs.StorageServer;importorg.csource.fastdfs.TrackerClient;importorg.csource.fastdfs.TrackerServer;importorg.springframework.core.io.ClassPathResource
PublicclassFDFSDeleteDemo {publicstaticvoidmain (String [] args) throwsException {/ / 1. Read the uploaded configuration file, which is ClassPathResource res=newClassPathResource ("fdfs_client.conf") under the CLASSPATH path; / / 2. Initialize the environment for FastDFS upload ClientGlobal.init (res.getClassLoader (). GetResource ("fdfs_client.conf"). GetPath (); / / 3, establish a client connection to Tracker TrackerClient tracker=newTrackerClient (); TrackerServer trackerServer=tracker.getConnection () / / get the connection to the server / / 4. The one who is really responsible for saving the data is Storage StorageServer storageServer=null;//5, creating StorageClient1 client=newStorageClient1 (trackerServer,storageServer) for StoragerClient; / / 6. Get the relevant information about the file StringfileName= "group2/M00/00/00/wKh7yVg7zQSAFqYWAAL6dz7aOB0479.jpg"; System.out.println (client.delete_file1 (fileName)); trackerServer.close ();}
After deletion, 0 is returned if the deletion is successful, and 2 is returned if the deletion fails. At this time, in your future development process, if the so-called e-commerce system is involved, or the news client. You must be clear that the two major data of the file are saved separately in a file server, and then referenced.
After reading the above, have you mastered the method of how to operate FastDFS in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.