最近做的項目需要把相關的錄音文件上傳到FastDFS服務器,因爲之前沒有做過,所以都是在網上找一些資源做參考,最後經過調試,終於可以上傳成功了,接下來我來和大家分享我寫的相關代碼,其他就不講解了,希望能做到大家參考着也能配置成功,在項目中使用:
1、首先添加maven的依賴
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
2、添加配置文件:
文件內容:
fastdfs.connect_timeout_in_seconds=30
fastdfs.network_timeout_in_seconds=60
fastdfs.charset=UTF-8
fastdfs.http_tracker_http_port=8888
fastdfs.tracker_servers=127.0.01:22122
3、程序讀取:
首先yml配置相關的配置項:
fdfs:
trackerweb: http://127.0.0.1:8888/
storage:
serverip: 127.0.0.2
serveripport: 23000
storagepath: 0
env: -test
configFile: /data/fdfs_client-test.properties
然後添加相關的配置類:
@Configuration
@Slf4j
public class FastDFSConfig {
@Value("${fdfs.storage.serverip}")
private String storageServerIp;
@Value("${fdfs.storage.serveripport}")
private Integer storageServerPort;
@Value("${fdfs.storage.storagepath}")
private Integer storageServerPath;
@Value("${fdfs.env}")
private String env;
@Value("${fdfs.configFile}")
private String configFile;
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
@PostConstruct
public void init() throws Exception {
// String path = "fdfs_client" + env + ".properties";
// Resource resource = new ClassPathResource(path);
// String conf = resource.getFile().getCanonicalPath();
// ClientGlobal.init(conf);
Properties properties = new Properties();
Resource resource = new FileSystemResource(configFile);
properties.load(resource.getInputStream());
ClientGlobal.initByProperties(properties);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = new StorageServer(storageServerIp,storageServerPort,storageServerPath);
storageClient = new StorageClient1(trackerServer, storageServer);
log.info("------ FastDFS配置加載完畢 ------");
}
@Bean
public StorageClient storageClient(){
return storageClient;
}
}
其中加載配置文件分環境和讀取文件系統兩種方式,項目是springboot,我採用讀取文件系統的方式讀取配置文件,根據不同的項目架構來調整;
4、程序中來調用:
@Service
@Slf4j
public class FastDfsService {
@Resource
private StorageClient1 storageClient;
/**
* 上傳文件
* @return
*/
public String uplaodFile(String hlFileUrl) {
log.info(" ------ 語音文件路徑:【{}】,執行上傳操作 ------",hlFileUrl);
String fileName = FilenameUtils.getName(hlFileUrl);
String extensionName = FilenameUtils.getExtension(hlFileUrl);
log.info(" ------ 語音文件名稱:【{}】, 後綴名稱:【{}】 ------",fileName,extensionName);
InputStream inputStream = null;
try {
URL url = new URL(hlFileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
inputStream = conn.getInputStream();
byte[] fileContent = IOUtils.toByteArray(inputStream);
String fastFullPath = storageClient.upload_file1(fileContent,extensionName,null);
// String fastFullPath = null;
log.info(" ------ 語音文件:【{}】,上傳FastDfs路徑:【{}】 ------",fileName,fastFullPath);
log.info(" ------ 語音文件:【{}】,上傳成功 ------",fileName);
return fastFullPath;
}catch (Exception e){
e.printStackTrace();
log.info(" ------ 語音文件:【{}】,上傳fastDfs異常:{} ------",fileName,e);
}finally {
try {
if (inputStream != null){
inputStream.close();
}
}catch (IOException e){
}
}
return null;
}
}
這樣就完成了相關的上傳功能,還有其他的一些下載刪除等,可以到網上搜索相關的API,本文不做更多的代碼分享,希望我的內容給大家一些思路。