FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码
1、下载FastDFS的API FastDFS提供Java和PHP等语言的客户端API。可以到FastDFS在Google Code的项目主页 http://code.google.com/p/fastdfs/downloads/list 下载。本文以Java API为例。
2、调用API的上传接口 通过Servlet得到InputStream、文件名称和文件长度,然后通过调用FastDFS提供的Java API把文件上传到FastDFS服务器。下段代码中的getFileBuffer可参考本博客上一篇博文。(by Poechant)
- /**
- * Upload File to DFS.
- * @param fileBuff, file to be uploaded.
- * @param uploadFileName, the name of the file.
- * @param fileLength, the length of the file.
- * @return the file ID in DFS.
- * @throws IOException
- */
- public String uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException {
- byte[] fileBuff = getFileBuffer(inStream, fileLength);
- String fileId = "";
- String fileExtName = "";
- if (uploadFileName.contains(".")) {
- fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
- } else {
- logger.warn("Fail to upload file, because the format of filename is illegal.");
- return fileId;
- }
- //建立连接
- TrackerClient tracker = new TrackerClient();
- TrackerServer trackerServer = tracker.getConnection();
- StorageServer storageServer = null;
- StorageClient1 client = new StorageClient1(trackerServer, storageServer);
- //设置元信息
- NameValuePair[] metaList = new NameValuePair[3];
- metaList[0] = new NameValuePair("fileName", uploadFileName);
- metaList[1] = new NameValuePair("fileExtName", fileExtName);
- metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
- //上传文件
- try {
- fileId = client.upload_file1(fileBuff, fileExtName, metaList);
- } catch (Exception e) {
- logger.warn("Upload file \"" + uploadFileName + "\"fails");
- }
- trackerServer.close();
- return fileId;
- }
/** * Upload File to DFS. * @param fileBuff, file to be uploaded. * @param uploadFileName, the name of the file. * @param fileLength, the length of the file. * @return the file ID in DFS. * @throws IOException */ public String uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException { byte[] fileBuff = getFileBuffer(inStream, fileLength); String fileId = ""; String fileExtName = ""; if (uploadFileName.contains(".")) { fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1); } else { logger.warn("Fail to upload file, because the format of filename is illegal."); return fileId; } //建立连接 TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient1 client = new StorageClient1(trackerServer, storageServer); //设置元信息 NameValuePair[] metaList = new NameValuePair[3]; metaList[0] = new NameValuePair("fileName", uploadFileName); metaList[1] = new NameValuePair("fileExtName", fileExtName); metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength)); //上传文件 try { fileId = client.upload_file1(fileBuff, fileExtName, metaList); } catch (Exception e) { logger.warn("Upload file \"" + uploadFileName + "\"fails"); } trackerServer.close(); return fileId; }
3、调用方式详解 (1)客户端与Tracker Server通信 根据《FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS》一文中提供的FastDFS的工作原理,结合上面的代码,首先通过TrackerClient构造函数从全局配置中获取Tracker Servers的IP和端口初始化一个TrackerClient对象tracker,并与其建立连接,我们可以从API的源码中看到:
- /**
- * constructor with global tracker group
- */
- public TrackerClient()
- {
- this.tracker_group = ClientGlobal.g_tracker_group;
- }
- /**
- * constructor with specified tracker group
- * @param tracker_group the tracker group object
- */
- public TrackerClient(TrackerGroup tracker_group)
- {
- this.tracker_group = tracker_group;
- }
/** * constructor with global tracker group */ public TrackerClient() { this.tracker_group = ClientGlobal.g_tracker_group; } /** * constructor with specified tracker group * @param tracker_group the tracker group object */ public TrackerClient(TrackerGroup tracker_group) { this.tracker_group = tracker_group; }
上述代码中ClientGlobal是一个提供很多静态成员供外部读取的类。通过tracker这个TrackerClient建立的与Tracker Server的连接,实例化了一个trackerServer对象。
(2)客户端与Storage Server通信
通过trackerServer取得某一个可用的Storage Server的地址并用其实例化一个StorageClient1对象。这样就完成了FastDFS的客户端调用上传、下载、删除等所有操作的前期建立连接的工作。
(3)调用文件操作API 这些操作包括upload、download、append、delete等。上例中提供的是上传的实例。