项目整体
maven
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
这里把文件上传的相关属性封装在了一个接口中,需要用到文件上传的相关实体或者工具类直接实现这个接口即可:
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直接可以返回完成的文件路径,下面就是我们调用上传方法了:
package com.neuedu; import org.csource.common.NameValuePair; import java.io.FileInputStream; import java.io.IOException; /** * Hello world! * */ public class App { public static void main( String[] args ) throws IOException { // 获取文件后缀名 String ext = "jpg"; FileInputStream fileReader = new FileInputStream("d:/dog.jpg"); byte[] bytes = new byte[10000000];//10M int length = fileReader.read(bytes); FastDFSFile file = new FastDFSFile(bytes,ext); NameValuePair[] meta_list = new NameValuePair[4]; meta_list[0] = new NameValuePair("fileName", "abc"); meta_list[1] = new NameValuePair("fileLength", String.valueOf(length)); meta_list[2] = new NameValuePair("fileExt", ext); meta_list[3] = new NameValuePair("fileAuthor", "WangLiang"); String filePath = FileManager.upload(file,meta_list); System.out.println(filePath); } }
fdfs_client.conf
connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.238.130:22122