上传附件:
@Test public void uploadFile() throws Exception { //1、向工程中添加jar包 //2、创建一个配置文件。配置tracker服务器地址 //3、加载配置文件,配置文件中tracker_server=192.168.66.129:22122 ClientGlobal.init("D:/taotaoworkspace/taotao-manager-web/src/main/resources/resource/client.conf"); //4、创建一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); //5、使用TrackerClient对象获得trackerserver对象。 TrackerServer trackerServer = trackerClient.getConnection(); //6、创建一个StorageServer的引用null就可以。 StorageServer storageServer = null; //7、创建一个StorageClient对象。trackerserver、StorageServer两个参数。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //8、使用StorageClient对象上传文件。 String[] strings = storageClient.upload_file("D:/test.png", "png", null); for (String string : strings) { System.out.println(string); } }
执行结果为:
group1 M00/00/00/wKhCgVkoQjSAUIkfAALlhUPEEGo827.png
返回个数组,string[0]位group号,string[1]位路径
上传时一定要把fdfs服务器的22122端口和storage的端口23000打开,否则会报socket连不上
新建个上传工具类FastDFSClient如下:
@Test public void testFastDfsClient() throws Exception { FastDFSClient fastDFSClient = new FastDFSClient("D:/taotaoworkspace/taotao-manager-web/src/main/resources/resource/client.conf"); String string = fastDFSClient.uploadFile("D:/test.png"); System.out.println(string); }
输出:
group1/M00/00/00/wKhCgVkoQsSANBGXAALlhUPEEGo756.png
FastDfsClient类:
package com.taotao.utils; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient1; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) { conf = conf.replace("classpath:", this.getClass().getResource("/").getPath()); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileName 文件全路径 * @param extName 文件扩展名,不包含(.) * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileName, extName, metas); return result; } public String uploadFile(String fileName) throws Exception { return uploadFile(fileName, null, null); } public String uploadFile(String fileName, String extName) throws Exception { return uploadFile(fileName, extName, null); } /** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileContent 文件的内容,字节数组 * @param extName 文件扩展名 * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas); return result; } public String uploadFile(byte[] fileContent) throws Exception { return uploadFile(fileContent, null, null); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return uploadFile(fileContent, extName, null); } }
文件下载:
storage里配置了nginx,可以通过nginx直接访问,nginx配置文件nginx.conf
server { listen 8080; listen localhost; location ~/group([0-9])/M00 { ngx_fastdfs_module; } }
可以url直接通过如下访问
192.168.66.129:8080/group1/M00/00/00/wKhCgVkoQsSANBGXAALlhUPEEGo756.png
如果tracker中设置了nginx,负载均衡,nginx.conf配置
upstream fdfs_group1 { server 192.168.224.25:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.224.26:8080 weight=1 max_fails=2 fail_timeout=30s; } upstream fdfs_group2 { server 192.168.224.28:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.224.29:8080 weight=1 max_fails=2 fail_timeout=30s; } server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /group1/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group1; expires 30d; } location /group2/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group2; expires 30d; } location ~/purge(/.*) { allow 127.0.0.1; allow 192.168.224.0/24; deny all; proxy_cache_purge http-cache $1$is_args$args; }
时间: 2024-11-05 14:50:46