原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153
本篇blog主要記錄一下SpringMVC整合FastDFS的Java客戶端實現web中的文件上傳與下載。
下載編譯
在余大的GitHub上可以下載到fastdfs-client-java的源代碼:
https://github.com/happyfish100/fastdfs-client-java
如上圖,這個版本是通過JDK1.5編譯的,根據需求可以通過源碼重新編譯jar包,我這里將原項目的maven編譯插件的版本改為JDK 1.8之后重新進行了編譯,編譯安裝成功后可以在我們本地的maven倉庫看到fastdfs-client-java的jar包:
最后在我們項目的pom中添加fastdfs-client-java的坐標信息就OK了:
<!-- fastdfs-client --> <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.25</version> </dependency>
文件上傳
首先來實現文件上傳,fastdfs-client-java的上傳是通過傳入一個byte[ ]來完成的,簡單看一下源碼:
public String[] upload_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException{ final String group_name = null; return this.upload_file(group_name, file_buff, 0, file_buff.length, file_ext_name, meta_list); }
如上所示,暫且不再深入研究原理,此處我們知道需要一個byte[ ]類型的參數就可以了,而SpringMVC的文件上傳用到的MultipartFile對象可以直接通過getBytes方法得到文件的byte[ ],也就是CommonsMultipartFile類中的getBytes(),源碼如下:
@Override public byte[] getBytes() { if (!isAvailable()) { throw new IllegalStateException("File has been moved - cannot be read again"); } byte[] bytes = this.fileItem.get(); return (bytes != null ? bytes : new byte[0]); }
那么接下來我們就知道如何上傳了,當然首先需要做一些簡單的封裝,這里把文件上傳的相關屬性封裝在了一個接口中,需要用到文件上傳的相關實體或者工具類直接實現這個接口即可:
public interface FileManagerConfig extends Serializable { public static final String FILE_DEFAULT_AUTHOR = "WangLiang"; public static final String PROTOCOL = "http://"; public static final String SEPARATOR = "/"; public static final String TRACKER_NGNIX_ADDR = "192.168.0.68"; public static final String TRACKER_NGNIX_PORT = ""; public static final String CLIENT_CONFIG_FILE = "fdfs_client.conf"; }
接下來定義FastDFS文件的實體類:
package com.wl.bean; /** * <strong>類概要: FastDFS文件實體</strong> <br> * <strong>創建時間: 2016-9-27 下午10:29:25</strong> <br> * * @Project springmvc-main(com.wl.bean) * @author Wang Liang * @version 1.0.0 */ public class FastDFSFile implements FileManagerConfig { private static final long serialVersionUID = 1L; private byte[] content; private String name; private String ext; private String length; private String author = FILE_DEFAULT_AUTHOR; public FastDFSFile(byte[] content, String ext) { this.content = content; this.ext = ext; } public FastDFSFile(byte[] content, String name, String ext) { this.content = content; this.name = name; this.ext = ext; } public FastDFSFile(byte[] content, String name, String ext, String length, String author) { this.content = content; this.name = name; this.ext = ext; this.length = length; this.author = author; } public byte[] getContent() { return content; } public void setContent(byte[] content) { this.content = content; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getExt() { return ext; } public void setExt(String ext) { this.ext = ext; } public String getLength() { return length; } public void setLength(String length) { this.length = length; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
如上所示,包括上傳所必須的file_buff和file_ext_name以及在meta_list中存放的幾個文件描述屬性。接下來看一下核心工具類FileManager:
import java.io.File; import java.io.IOException; 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.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; /** * <strong>類概要: FastDFS Java客戶端工具類</strong> <br> * <strong>創建時間: 2016-9-26 上午10:26:48</strong> <br> * * @Project springmvc-main(com.wl.bean) * @author Wang Liang * @version 1.0.0 */ public class FileManager implements FileManagerConfig { private static final long serialVersionUID = 1L; private static TrackerClient trackerClient; private static TrackerServer trackerServer; private static StorageServer storageServer; private static StorageClient storageClient; static { try { String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath(); String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE; ClientGlobal.init(fdfsClientConfigFilePath); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageClient = new StorageClient(trackerServer, storageServer); } catch (Exception e) { e.printStackTrace(); } } /** * <strong>方法概要: 文件上傳</strong> <br> * <strong>創建時間: 2016-9-26 上午10:26:11</strong> <br> * * @param FastDFSFile * file * @return fileAbsolutePath * @author Wang Liang */ public static String upload(FastDFSFile file,NameValuePair[] valuePairs) { String[] uploadResults = null; try { uploadResults = storageClient.upload_file(file.getContent(),file.getExt(), valuePairs); } catch (Exception e) { e.printStackTrace(); } String groupName = uploadResults[0]; String remoteFileName = uploadResults[1]; String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR //+ trackerServer.getInetSocketAddress().getHostName() //+ SEPARATOR + TRACKER_NGNIX_PORT + SEPARATOR + groupName + SEPARATOR + remoteFileName; return fileAbsolutePath; } }
如上所示,在類初始化時加載fdfs_client.conf配置文件並構造tracker server和storage server,文件上傳是通過storageClient.upload_file
方法來實現的,而返回的uploadResults字符串數組正是文件名,固定兩個元素,uploadResults[0]是組名(group),而uploadResults[1]就是組名后面的文件全名了,最后我們的方法中有做了部分拼接使得FileManager.upload直接可以返回完成的文件路徑,下面就是我們調用上傳方法的controller中的方法了:
@RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@Validated User user, BindingResult br,MultipartFile attach, HttpServletRequest request) throws IOException, MyException { if (br.hasErrors()) { return "user/add"; } // 獲取文件后綴名 String ext = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".")+1); FastDFSFile file = new FastDFSFile(attach.getBytes(),ext); NameValuePair[] meta_list = new NameValuePair[4]; meta_list[0] = new NameValuePair("fileName", attach.getOriginalFilename()); meta_list[1] = new NameValuePair("fileLength", String.valueOf(attach.getSize())); meta_list[2] = new NameValuePair("fileExt", ext); meta_list[3] = new NameValuePair("fileAuthor", "WangLiang"); String filePath = FileManager.upload(file,meta_list); user.setFilePath(filePath); users.put(user.getUsername(), user); return "redirect:/user/users"; }
如上所示,首先通過字符串截取得到上傳文件的后綴名,然后通過文件后綴和文件的byte[ ]構造FastDFSFile對象,接着構造meta_list的NameValuePair[] 數組,這里主要是對文件的可選性描述信息,最后通過FileManager.upload即可完成上傳並返回該文件的絕對訪問路徑,可以根據需要存入DB或文件等等,沒有報異常就說明文件上傳成功,接下來看看文件下載。
文件下載
fastdfs-client-java提供的文件下載的api需要兩個參數,分別是group_name(組名)和remote_filename(文件名),源碼如下:
/** * download file from storage server * @param group_name the group name of storage server * @param remote_filename filename on storage server * @return file content/buff, return null if fail */ public byte[] download_file(String group_name, String remote_filename) throws IOException, MyException { final long file_offset = 0; final long download_bytes = 0; return this.download_file(group_name, remote_filename, file_offset, download_bytes); }
所以我們僅需在這里得到group_name和remote_filename即可,因為之前我們在文件上傳時候已經保存了圖片的絕對路徑(user.setFilePath(filePath)
),所以在此處僅需要獲取到絕對路徑並進行字符串的拆分截取即可,接下來先看一下封裝在FileManager中的下載方法:
/** * <strong>方法概要: 文件下載</strong> <br> * <strong>創建時間: 2016-9-26 上午10:28:21</strong> <br> * * @param String * groupName * @param String * remoteFileName * @return returned value comment here * @author Wang Liang */ public static ResponseEntity<byte[]> download(String groupName, String remoteFileName,String specFileName) { byte[] content = null; HttpHeaders headers = new HttpHeaders(); try { content = storageClient.download_file(groupName, remoteFileName); headers.setContentDispositionFormData("attachment", new String(specFileName.getBytes("UTF-8"),"iso-8859-1")); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED); }
如上所示,17行調用fastdfs-client-java提供的下載方法,下載成功后返回的是一個byte[ ],剛好結合SpringMVC官方推薦的構造HttpEntity的方式即可實現文件下載,這個download方法我多指定了一個參數specFileName目的是保證給客戶端看到的下載后的文件名是通過程序來自定義的,而不是fastdfs服務器上的那一長串默認字符串,18行指定了utf-8編碼使得我們自定義的文件名支持中文,這一點很重要,否則將無法正確下載我們重命名后的包含中文的文件。最后在看一下controller中的下載方法:
@RequestMapping(value = "/{username}/download", method = RequestMethod.GET) public ResponseEntity<byte[]> download(@PathVariable String username, Model model,HttpServletResponse response) throws IOException, MyException { User u = users.get(username); String filePath = u.getFilePath(); String substr = filePath.substring(filePath.indexOf("group")); String group = substr.split("/")[0]; String remoteFileName = substr.substring(substr.indexOf("/")+1); String specFileName = username + substr.substring(substr.indexOf(".")); return FileManager.download(group, remoteFileName,specFileName); }
同我們之前的想法一樣,截取文件的絕對路徑分別得到group_name以及file_name,而傳入的specFileName我們這里自定義為用戶名(username)+截取后的文件后綴名,看一下效果:
如上圖,點擊【下載附件】,即可正確下載以及重命名文件,至此SpringMVC結合fastdfs的文件上傳下載就已全部結束了。