使用java操作FastDFS上傳,下載,刪除檔案
阿新 • • 發佈:2018-11-19
經過我的測試,直接把程式碼copy就能用了!
前提條件:將原始碼生成為jar包,install到本地maven倉庫
1.從github上down下原始碼:https://github.com/happyfish100/fastdfs-client-java在myeclipse中建立maven專案,然後把解壓的內容複製到maven專案中
2.你也可以直接下載我整的maven工程,解壓匯入myeclipse中直接使用,下載地址:http://download.csdn.net/detail/qq_34021712/9812335
執行maven install 將程式碼打成jar到本地maven倉庫
在maven中依賴jar包
<!-- fastdfs上傳下載圖片 路徑和上面的pom中對應 -->
<dependency>
<groupId>org.csource.fastdfs-client-java</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.25</version>
</dependency>
建立FastDFSClient類
import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLDecoder; import org.csource.common.MyException; 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:")) { String path = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(),"UTF-8"); path=path.substring(6); conf = conf.replace("classpath:",URLDecoder.decode(path,"UTF-8")); } 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) { String result=null; try { result = storageClient.upload_file1(fileName, extName, metas); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } /** * 上傳檔案,傳fileName * @param fileName 檔案的磁碟路徑名稱 如:D:/image/aaa.jpg * @return null為失敗 */ public String uploadFile(String fileName) { return uploadFile(fileName, null, null); } /** * * @param fileName 檔案的磁碟路徑名稱 如:D:/image/aaa.jpg * @param extName 檔案的副檔名 如 txt jpg等 * @return null為失敗 */ public String uploadFile(String fileName, String extName) { 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) { String result=null; try { result = storageClient.upload_file1(fileContent, extName, metas); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } /** * 上傳檔案 * @param fileContent 檔案的位元組陣列 * @return null為失敗 * @throws Exception */ public String uploadFile(byte[] fileContent) throws Exception { return uploadFile(fileContent, null, null); } /** * 上傳檔案 * @param fileContent 檔案的位元組陣列 * @param extName 檔案的副檔名 如 txt jpg png 等 * @return null為失敗 */ public String uploadFile(byte[] fileContent, String extName) { return uploadFile(fileContent, extName, null); } /** * 檔案下載到磁碟 * @param path 圖片路徑 * @param output 輸出流 中包含要輸出到磁碟的路徑 * @return -1失敗,0成功 */ public int download_file(String path,BufferedOutputStream output) { //byte[] b = storageClient.download_file(group, path); int result=-1; try { byte[] b = storageClient.download_file1(path); try{ if(b != null){ output.write(b); result=0; } }catch (Exception e){} //使用者可能取消了下載 finally { if (output != null) try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 獲取檔案陣列 * @param path 檔案的路徑 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg * @return */ public byte[] download_bytes(String path) { byte[] b=null; try { b = storageClient.download_file1(path); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return b; } /** * 刪除檔案 * @param group 組名 如:group1 * @param storagePath 不帶組名的路徑名稱 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg * @return -1失敗,0成功 */ public Integer delete_file(String group ,String storagePath){ int result=-1; try { result = storageClient.delete_file(group, storagePath); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } /** * * @param storagePath 檔案的全部路徑 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg * @return -1失敗,0成功 * @throws IOException * @throws Exception */ public Integer delete_file(String storagePath){ int result=-1; try { result = storageClient.delete_file1(storagePath); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } }
建立Test測試類
package com.taotao.controller;
import java.util.List;
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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.util.FastDFSClient;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
public class Test {
/*@org.junit.Test
public void testPageHelper(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
TbItemMapper tbItemMapper = applicationContext.getBean(TbItemMapper.class);
//執行查詢,並分頁
TbItemExample example = new TbItemExample();
//分頁處理
PageHelper.startPage(2, 10);
List<TbItem> list = tbItemMapper.selectByExample(example);
//取商品列表
for (TbItem tbItem : list) {
System.out.println(tbItem.getTitle());
}
//取分頁資訊
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
long total = pageInfo.getTotal();
System.out.println("共有商品:"+ total);
}
@org.junit.Test
public void testUpload() throws Exception {
// 1、把FastDFS提供的jar包新增到工程中
// 2、初始化全域性配置。載入一個配置檔案。
ClientGlobal.init("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf");
// 3、建立一個TrackerClient物件。
TrackerClient trackerClient = new TrackerClient();
// 4、建立一個TrackerServer物件。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、宣告一個StorageServer物件,null。
StorageServer storageServer = null;
// 6、獲得StorageClient物件。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 7、直接呼叫StorageClient物件方法上傳檔案即可。
String[] strings = storageClient.upload_file("D:\\image\\aaa.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
}*/
/**
* 測試上傳
* @throws Exception
*/
@org.junit.Test
public void testFastDfsClient() throws Exception {
FastDFSClient client = new FastDFSClient("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf");
String uploadFile = client.uploadFile("D:\\image\\aaa.jpg", "jpg");
System.out.println(uploadFile);
}
/**
* 測試刪除
* @throws Exception
*/
@org.junit.Test
public void testFastDfsClientDelete() throws Exception {
FastDFSClient client = new FastDFSClient("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf");
//String uploadFile = client.uploadFile("D:\\image\\aaa.jpg", "jpg");
//Integer delete_file = client.delete_file("group1","M00/00/00/wKgRsVjtwg-ACOlSAAAweEAzRjw844.jpg");
Integer delete_file = client.delete_file("group1/M00/00/00/wKgRsVjtyWqAdpG9AAAweEAzRjw047.jpg");
System.out.println(delete_file);
}
}
原文連結:https://blog.csdn.net/qq_34021712/article/details/70149604?utm_source=blogxgwz0