FastDFS文件操作-java


未安裝fastdfs先安裝:FastDFS安裝配置(CentOS7),接下來寫個測試demo。

創建demo項目

1、pom.xml引入依賴

	<!-- fastDFS依賴就一個 -->
	<dependencies>
		<dependency>
			<groupId>net.oschina.zcx7878</groupId>
			<artifactId>fastdfs-client-java</artifactId>
			<version>1.27.0.0</version>
		</dependency>
	</dependencies>

 2、配置文件

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.1.104:22122

3、代碼

(1)FastDfsUtil.java工具類(僅供參考)

package com.fdfs.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * 
* @ClassName: FastDfsUtil 
* @Description: FastDfs文件管理簡單工具類
* @author zhoujie 
* @date 2018年11月27日 下午6:10:16 
* @version V1.0
 */
public class FastDfsUtil {
	
	private static TrackerClient trackerClient = null;
	private static TrackerServer trackerServer = null;
	private static StorageServer storageServer = null;
	private static StorageClient storageClient = null;

	static {
		try {
			ClientGlobal.init("fdfs_client.conf");
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient(trackerServer, storageServer);
		} catch (IOException | MyException e) {
			throw new RuntimeException("FastDfs工具類初始化失敗!");
		}
	}
	
	/**
	 * 
	* @Title: fdfsUpload 
	* @Description: 通過文件流上傳文件
	* @param @param inputStream 文件流
	* @param @param filename 文件名稱
	* @param @return
	* @param @throws IOException
	* @param @throws MyException 
	* @return String 返回文件在FastDfs的存儲路徑   
	* @throws
	 */
	public static String fdfsUpload(InputStream inputStream, String filename) throws IOException, MyException{
		String suffix = ""; //后綴名
		try{
			suffix = filename.substring(filename.lastIndexOf(".")+1);
		}catch (Exception e) {
			throw new RuntimeException("參數filename不正確!格式例如:a.png");
		}
		String savepath = ""; //FastDfs的存儲路徑   
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
	    byte[] buff = new byte[1024];  
		int len = 0;  
		while ((len = inputStream.read(buff)) != -1) {  
			swapStream.write(buff, 0, len);  
		}
		byte[] in2b = swapStream.toByteArray();  
		String[] strings = storageClient.upload_file(in2b, suffix, null); //上傳文件
		for (String str : strings) {
			savepath += "/" + str; //拼接路徑
		}
		return savepath;
	}
	
	/**
	 * 
	* @Title: fdfsUpload 
	* @Description: 本地文件上傳
	* @param @param filepath 本地文件路徑
	* @param @return
	* @param @throws IOException
	* @param @throws MyException    
	* @return String 返回文件在FastDfs的存儲路徑   
	* @throws
	 */
	public static String fdfsUpload(String filepath) throws IOException, MyException{
		String suffix = ""; //后綴名
		try{
			suffix = filepath.substring(filepath.lastIndexOf(".")+1);
		}catch (Exception e) {
			throw new RuntimeException("上傳的不是文件!");
		}
		String savepath = ""; //FastDfs的存儲路徑   
		String[] strings = storageClient.upload_file(filepath, suffix, null); //上傳文件
		for (String str : strings) {
			savepath += "/" + str; //拼接路徑
		}
		return savepath;
	}
	
	/**
	 * 
	* @Title: fdfsDownload 
	* @Description: 下載文件到目錄
	* @param @param savepath 文件存儲路徑
	* @param @param localPath 下載目錄
	* @param @return
	* @param @throws IOException
	* @param @throws MyException    
	* @return boolean 返回是否下載成功
	* @throws
	 */
	public static boolean fdfsDownload(String savepath, String localPath) throws IOException, MyException{
		String group = ""; //存儲組
		String path = ""; //存儲路徑
		try{
			int secondindex = savepath.indexOf("/", 2); //第二個"/"索引位置
			group = savepath.substring(1, secondindex); //類似:group1
			path = savepath.substring(secondindex + 1); //類似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
		}catch (Exception e) {
			throw new RuntimeException("傳入文件存儲路徑不正確!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
		}
		int result = storageClient.download_file(group, path, localPath);
		if(result != 0){
			throw new RuntimeException("下載文件失敗:文件路徑不對或者文件已刪除!");
		}
		return true;
	}
	
	/**
	 * 
	* @Title: fdfsDownload 
	* @Description: 返回文件字符數組
	* @param @param savepath 文件存儲路徑
	* @param @return
	* @param @throws IOException
	* @param @throws MyException    
	* @return byte[] 字符數組
	* @throws
	 */
	public static byte[] fdfsDownload(String savepath) throws IOException, MyException{
		byte[] bs = null;
		String group = ""; //存儲組
		String path = ""; //存儲路徑
		try{
			int secondindex = savepath.indexOf("/", 2); //第二個"/"索引位置
			group = savepath.substring(1, secondindex); //類似:group1
			path = savepath.substring(secondindex + 1); //類似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
		}catch (Exception e) {
			throw new RuntimeException("傳入文件存儲路徑不正確!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
		}
		bs = storageClient.download_file(group, path); //返回byte數組
		return bs;
	}
	
	/**
	 * 
	* @Title: fdfsDeleteFile 
	* @Description: 刪除文件
	* @param @param savepath 文件存儲路徑
	* @param @return
	* @param @throws IOException
	* @param @throws MyException    
	* @return boolean 返回true表示刪除成功   
	* @throws
	 */
	public static boolean fdfsDeleteFile(String savepath) throws IOException, MyException{
		String group = ""; //存儲組
		String path = ""; //存儲路徑
		try{
			int secondindex = savepath.indexOf("/", 2); //第二個"/"索引位置
			group = savepath.substring(1, secondindex); //類似:group1
			path = savepath.substring(secondindex + 1); //類似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
		}catch (Exception e) {
			throw new RuntimeException("傳入文件存儲路徑不正確!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
		}
		int result = storageClient.delete_file(group, path); //刪除文件,0表示刪除成功
		if(result != 0){
			throw new RuntimeException("刪除文件失敗:文件路徑不對或者文件已刪除!");
		}
		return true;
	}
	
	/**
	 * 
	* @Title: fdfdFileInfo 
	* @Description: 返回文件信息
	* @param @param savepath 文件存儲路徑
	* @param @return
	* @param @throws IOException
	* @param @throws MyException    
	* @return FileInfo 文件信息
	* @throws
	 */
	public static FileInfo fdfdFileInfo(String savepath) throws IOException, MyException{
		String group = ""; //存儲組
		String path = ""; //存儲路徑
		try{
			int secondindex = savepath.indexOf("/", 2); //第二個"/"索引位置
			group = savepath.substring(1, secondindex); //類似:group1
			path = savepath.substring(secondindex + 1); //類似:M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png
		}catch (Exception e) {
			throw new RuntimeException("傳入文件存儲路徑不正確!格式例如:/group1/M00/00/00/wKgBaFv9Ad-Abep_AAUtbU7xcws013.png");
		}
		FileInfo fileInfo = storageClient.get_file_info(group, path);
		return fileInfo;
	}
	
}

(2)Test.java測試

package com.fdfs.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.csource.common.MyException;

public class Test {

	public static void main(String[] args) {
		
		try {
			System.out.println("存儲路徑:"+FastDfsUtil.fdfsUpload("F:/圖片/02.jpg")); //上傳文件。。。注意路徑
			//System.out.println(FastDfsUtil.fdfdFileInfo("/group1/M00/00/00/wKgBaFv9HJeAMaseAAIWRj8eKZQ103.jpg").toString()); //文件信息
			//System.out.println(FastDfsUtil.fdfsDeleteFile("/group1/M00/00/00/wKgBaFv9HJeAMaseAAIWRj8eKZQ103.jpg")); //刪除文件
			//System.out.println(FastDfsUtil.fdfsUpload(new FileInputStream(new File("F:/圖片/02.jpg")), "02.jpg"));//通過文件流上傳文件
			//System.out.println(FastDfsUtil.fdfsDownload("/group1/M00/00/00/wKgBaFv9acaANK2oAAIWRj8eKZQ339.jpg").length); //獲取文件byte[]
			//System.out.println(FastDfsUtil.fdfsDownload("/group1/M00/00/00/wKgBaFv9acaANK2oAAIWRj8eKZQ339.jpg", "D:/logs")); //下載文件到本地(拒絕訪問)
		} catch (IOException | MyException e) {
			e.printStackTrace();
		}
		
	}
	
}

ok!

效果:

可以直接瀏覽器預覽

http://192.168.1.104/group1/M00/00/00/wKgBaFwXZd2ALGUxAAIWRj8eKZQ220.jpg

ojbk,!!!

demo下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM