FastDFS:Java客户都实现文件的上传、下载、修改、删除

FastDFS:Java客户都实现文件的上传、下载、修改、删除


  4.0.0
  cn.fastdfs.demo
  fastDFSdemo
  0.0.1-SNAPSHOT
  
  
  	
		org.csource.fastdfs
		fastdfs
		1.2
	
   
     commons-logging
     commons-logging
     1.1.3
   
	
	    commons-io
	    commons-io
	    2.4
	
	
	
	    commons-fileupload
	    commons-fileupload
	    1.3
	
	
	
	    log4j
	    log4j
	    1.2.17
	
	
	
	    org.apache.commons
	    commons-lang3
	    3.3.1
	
	
	
	    org.slf4j
	    slf4j-api
	    1.5.8
	
	
	
	    org.slf4j
	    slf4j-log4j12
	    1.5.8
	    test
	


  
  
  
fastdfs_client.conf
tracker_server=localhost:22122
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
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.TrackerGroup;
import org.csource.fastdfs.TrackerServer;

public class FastDFSClientUtils {

    private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("fastdfs_client.conf").getPath();

    private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);

    
    private static TrackerClient trackerClient;

    
    //加载文件
    static {
        try {
            ClientGlobal.init(CONF_FILENAME);
            TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
            trackerClient = new TrackerClient(trackerGroup);
        } catch (Exception e) {
            logger.error(e);
        }
    }
    
    /**
     * 方法名称:上传方法
* 概要说明:
* @param file 文件 * @param path 路径 * @return 上传成功返回id,失败返回null */ public static String upload(File file, String path) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; FileInputStream fis = null; try { NameValuePair[] meta_list = null; // new NameValuePair[0]; fis = new FileInputStream(file); byte[] file_buff = null; if (fis != null) { int len = fis.available(); file_buff = new byte[len]; fis.read(file_buff); } trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer); storageClient1 = new StorageClient1(trackerServer, storageServer); String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list); return fileid; } catch (Exception ex) { logger.error(ex); return null; }finally{ if (fis != null){ try { fis.close(); } catch (IOException e) { logger.error(e); } } if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 方法名称:上传方法
* 概要说明:
* @param data 数据 * @param path 路径 * @return 上传成功返回id,失败返回null */ public static String upload(byte[] data, String extName) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { NameValuePair[] meta_list = null; // new NameValuePair[0]; trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer); storageClient1 = new StorageClient1(trackerServer, storageServer); String fileid = storageClient1.upload_file1(data, extName, meta_list); return fileid; } catch (Exception ex) { logger.error(ex); return null; }finally{ if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 方法名称:下载方法
* 概要说明:通过文件id进行下载
* @param fileId 文件id * @return 返回InputStream */ public static InputStream download(String groupName, String fileId) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer); byte[] bytes = storageClient1.download_file1(fileId); InputStream inputStream = new ByteArrayInputStream(bytes); return inputStream; } catch (Exception ex) { logger.error(ex); return null; } finally { if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 方法名称:删除方法
* 概要说明:根据id来删除一个文件
* @param fileId 文件id * @return 删除成功返回0,非0则操作失败,返回错误代码 */ public static int delete(String groupName, String fileId) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer); int result = storageClient1.delete_file1(fileId); return result; } catch (Exception ex) { logger.error(ex); return 0; } finally { if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 方法名称:
* 概要说明:
* @param oldFileId 旧文件id * @param file 新文件 * @param path 新文件路径 * @return 上传成功返回id,失败返回null */ public static String modify(String oldGroupName, String oldFileId, File file, String path) { String fileid = null; try { // 先上传 fileid = upload(file, path); if (fileid == null) { return null; } // 再删除 int delResult = delete(oldGroupName, oldFileId); if (delResult != 0) { return null; } } catch (Exception ex) { logger.error(ex); return null; } return fileid; } /** * 方法名称:获取文件后缀名
* 概要说明:获取文件后缀名
* @param fileName * @return 如:"jpg"、"txt"、"zip" 等 */ private static String getFileExt(String fileName) { if (StringUtils.isBlank(fileName) || !fileName.contains(".")) { return ""; } else { return fileName.substring(fileName.lastIndexOf(".") + 1); } } }
import java.io.File;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;



public class FastDFSTest {
    
    /**
     * 上传
     */
    public static void upload() throws Exception {
        String path = "C:\\Users\\Leon.sun\\Desktop\\Temp\\img\\56.jpg";
        File file = new File(path);
        String fileId = FastDFSClientUtils.upload(file, path);
        // 本地文件:C:\Users\Leon.sun\Desktop\Temp\img\56.jpg,上传成功! 文件ID为:group1/M00/00/00/rBH2Clw3EpeAbO70AAB5yKpbklA306.jpg
        System.out.println("本地文件:" + path + ",上传成功! 文件ID为:" + fileId);
    }
    
    /**
     * 下载
     */
    public static void download() throws Exception {
        String fileId = "group1/M00/00/00/rBH2Clw3EpeAbO70AAB5yKpbklA306.jpg";
        InputStream inputStream = FastDFSClientUtils.download("group1", fileId);
        System.out.println(inputStream.available());
        String path = "C:\\Users\\Leon.sun\\Desktop\\Temp\\img\\66.jpg";
        System.out.println(path);
        FileUtils.copyInputStreamToFile(inputStream,  new File(path));
    }

    /**
     * 删除
     */
    public static void delete() throws Exception {
        String fileId = "group1/M00/00/00/rBH2Clw3EpeAbO70AAB5yKpbklA306.jpg";
        int result = FastDFSClientUtils.delete("group1", fileId);
        System.out.println(result == 0 ? "删除成功" : "删除失败:" + result);
    }


    
    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    

//        upload();
//        download();
//        Thread.sleep(10000);
//        download();
//        Thread.sleep(10000);
//        download();
        delete();

    }

}

 

你可能感兴趣的:(FastDFS:Java客户都实现文件的上传、下载、修改、删除)