当前位置:网站首页>[utils] fastdfs tool class
[utils] fastdfs tool class
2022-07-28 14:10:00 【One duck, one duck】
One 、 Import dependence
<dependencies>
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
Two 、resources Create profile under
fdfs_client.conf
connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8888
tracker_server = 192.168.40.21:22122
3、 ... and 、 Tool class
package com.ego.commons.utils;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.*;
public class FastDFSClient {
//private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "fdfs_client.conf";
// Relative paths
private static final String CONF_FILENAME = "fdfs_client.conf";
private static StorageClient storageClient = null;
/** * Load only once . */
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * * @param inputStream * Input stream of uploaded files * @param fileName * The original name of the uploaded file * @return */
public static String[] uploadFile(InputStream inputStream, String fileName) {
try {
// Metadata of the file
NameValuePair[] meta_list = new NameValuePair[2];
// The first set of metadata , Original name of the file
meta_list[0] = new NameValuePair("file name", fileName);
// The second set of metadata
meta_list[1] = new NameValuePair("file length", inputStream.available()+"");
// Prepare byte array
byte[] file_buff = null;
if (inputStream != null) {
// Check the length of the file
int len = inputStream.available();
// Create a byte array of corresponding length
file_buff = new byte[len];
// The contents of bytes in the input stream , Read into byte array .
inputStream.read(file_buff);
}
// Upload files . Parameter meaning : The contents of the file to be uploaded ( Pass... Using a byte array ), Type of file uploaded ( Extension ), Metadata
String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list);
return fileids;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/** * * @param file * file * @param fileName * file name * @return return Null Is a failure */
public static String[] uploadFile(File file, String fileName) {
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);
}
String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list);
return fileids;
} catch (Exception ex) {
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/** * Delete a file based on the group name and remote file name * * @param groupName * for example "group1" If the value is not specified , The default is group1 * @param remoteFileName * for example "M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg" * @return 0 For success , Not 0 For failure , Specific error code */
public static int deleteFile(String groupName, String remoteFileName) {
try {
int result = storageClient.delete_file(groupName == null ? "group1" : groupName, remoteFileName);
return result;
} catch (Exception ex) {
return 0;
}
}
/** * Modify an existing file * * @param oldGroupName * Old group name * @param oldFileName * Old file name * @param file * A new file * @param fileName * New file name * @return If it returns null, it means failure */
public static String[] modifyFile(String oldGroupName, String oldFileName, File file, String fileName) {
String[] fileids = null;
try {
// Upload first
fileids = uploadFile(file, fileName);
if (fileids == null) {
return null;
}
// And then delete
int delResult = deleteFile(oldGroupName, oldFileName);
if (delResult != 0) {
return null;
}
} catch (Exception ex) {
return null;
}
return fileids;
}
/** * File download * * @param groupName Volume name * @param remoteFileName file name * @return Back to a stream */
public static InputStream downloadFile(String groupName, String remoteFileName) {
try {
byte[] bytes = storageClient.download_file(groupName, remoteFileName);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
return null;
}
}
public static NameValuePair[] getMetaDate(String groupName, String remoteFileName){
try{
NameValuePair[] nvp = storageClient.get_metadata(groupName, remoteFileName);
return nvp;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
/** * Get file suffix ( No point ). * * @return Such as :"jpg" or "". */
private static String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1); // Without the last point
}
}
}
边栏推荐
- 彻底掌握二分查找
- 30 day question brushing training (I)
- 一文读懂如何部署具有外部数据库的高可用 K3s
- R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
- 修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
- A label_ File download (download attribute)
- 【翻译】盐业公司来Linkerd公司是为了负载平衡,留下来是为了效率、可靠性和性能。...
- 【Utils】JsonUtil
- Socket类关于TCP字符流编程的理解学习
- Multi level cache scheme
猜你喜欢

Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning

Multithreading and high concurrency (III) -- source code analysis AQS principle

Redis sentinel mechanism
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)

在centos中安装mysql5.7.36

深度学习基础----GNN谱域和空域 (不断完善更新积累)

LeetCode 0142.环形链表 II

Clickhouse架构与设计

Socket class understanding and learning about TCP character stream programming

Product Manager: job responsibility table
随机推荐
[lvgl events] event code
30 day question brushing plan (IV)
Algorithm --- different paths (kotlin)
Multithreading and high concurrency (III) -- source code analysis AQS principle
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
阿里、京东、抖音:把云推向产业心脏
安全保障基于软件全生命周期-Istio的认证机制
Redis sentinel mechanism
Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
算法---不同路径(Kotlin)
vite在项目中配置路径别名
一文读懂如何部署具有外部数据库的高可用 K3s
qml 图片预览
记一次COOKIE的伪造登录
Websocket chat
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning
DXF读写:对齐尺寸标注文字居中、上方的位置计算
第六章 支持向量机
Three cases of thread blocking.