文章目录
pom引入fdfs_client.confDfsUtil.java文件上传、下载、删除、获取元数据
pom引入
<dependencies>
<dependency>
<groupId>org.csource
</groupId>
<artifactId>fastdfs-client-java
</artifactId>
<version>1.27
</version>
</dependency>
<dependency>
<groupId>commons-io
</groupId>
<artifactId>commons-io
</artifactId>
<version>2.6
</version>
</dependency>
</dependencies>
fdfs_client.conf
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8111
tracker_server = 192.168.20.252:22122
DfsUtil.java
package com
.javasm
;
import org
.csource
.common
.NameValuePair
;
import org
.csource
.fastdfs
.*
;
import java
.io
.IOException
;
import java
.net
.URL
;
import java
.util
.HashMap
;
import java
.util
.Map
;
public class DfsUtil {
private static TrackerClient client
=null
;
public static final String FILE_SIZE
="file_size";
public static final String UPLOAD_TIME
="upload_time";
public static final String REL_NAME
="rel_name";
static{
URL resource
= DfsUtil
.class.getClassLoader().getResource("fdfs_client.conf");
String confFileName
= resource
.getPath();
try {
ClientGlobal
.init(confFileName
);
client
= new TrackerClient();
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static String
upload(byte[] bytes
,long size
,String fileName
){
String result
= null
;
TrackerServer ts
= null
;
StorageServer ss
= null
;
try {
ts
= client
.getConnection();
StorageClient1 storageClient1
= new StorageClient1(ts
,ss
);
String extName
= fileName
.substring(fileName
.lastIndexOf(".")+1);
NameValuePair
[] meta_list
= new NameValuePair[]{
new NameValuePair(FILE_SIZE
,size
+""),
new NameValuePair(REL_NAME
,fileName
),
new NameValuePair(UPLOAD_TIME
,System
.currentTimeMillis()+"")
};
result
= storageClient1
.upload_file1(bytes
, extName
, meta_list
);
return result
;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
if(ss
!=null
){
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if(ts
!=null
){
try {
ts
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
public static byte[] downLoad(String filePath
) {
TrackerServer ts
= null
;
StorageServer ss
= null
;
try {
ts
= client
.getConnection();
StorageClient1 storageClient1
= new StorageClient1(ts
,ss
);
byte[] bytes
= storageClient1
.download_file1(filePath
);
return bytes
;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
if(ss
!=null
){
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if(ts
!=null
){
try {
ts
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
public static long getUploadTime(String filePath
){
return Long
.parseLong(getMetaDatas(filePath
).get(UPLOAD_TIME
));
}
public static String
getFileSize(String filePath
){
return getMetaDatas(filePath
).get(FILE_SIZE
);
}
public static String
getFileName(String filePath
){
return getMetaDatas(filePath
).get(REL_NAME
);
}
public static Map
<String,String> getMetaDatas(String filePath
) {
TrackerServer ts
= null
;
StorageServer ss
= null
;
try {
ts
= client
.getConnection();
StorageClient1 storageClient1
= new StorageClient1(ts
,ss
);
Map
<String,String> metas
= new HashMap<>();
NameValuePair
[] metadata1
= storageClient1
.get_metadata1(filePath
);
if(metadata1
!=null
){
for(NameValuePair nv
:metadata1
){
metas
.put(nv
.getName(),nv
.getValue());
}
return metas
;
}
} catch (Exception e
) {
e
.printStackTrace();
} finally {
if(ss
!=null
){
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if(ts
!=null
){
try {
ts
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
public static boolean delFile(String filePath
) {
TrackerServer ts
= null
;
StorageServer ss
= null
;
try {
ts
= client
.getConnection();
StorageClient1 storageClient1
= new StorageClient1(ts
,ss
);
int result
= storageClient1
.delete_file1(filePath
);
return result
==0?true:false;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
if(ss
!=null
){
try {
ss
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
if(ts
!=null
){
try {
ts
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
return false;
}
public static String
getHttpUrl(String filePath
) {
TrackerServer ts
= null
;
try {
ts
= client
.getConnection();
String hostName
= ts
.getInetSocketAddress().getHostName();
return "http://"+hostName
+":"+ClientGlobal
.getG_tracker_http_port()+"/"+filePath
;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
if(ts
!=null
){
try {
ts
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
return null
;
}
}
文件上传、下载、删除、获取元数据
package com
.javasm
;
import org
.apache
.commons
.io
.FileUtils
;
import java
.io
.File
;
import java
.io
.IOException
;
import java
.util
.Map
;
public class App
{
public static void main( String
[] args
)
{
getHttpUrl();
}
public static void getHttpUrl(){
String filePath
= "group1/M00/00/12/wKgU_F808UmAJLTzAAD_2Eqxn3g488.png";
String httpurl
= DfsUtil
.getHttpUrl(filePath
);
System
.out
.println(httpurl
);
}
public static void delFile(){
String filePath
= "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx";
boolean isDel
= DfsUtil
.delFile(filePath
);
}
public static void getMetaDatas(){
String filePath
= "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx";
String fileName
= DfsUtil
.getFileName(filePath
);
System
.out
.println(fileName
);
}
public static void down(){
String filePath
= "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx";
byte[] bytes
= DfsUtil
.downLoad(filePath
);
File file
= new File("D:/xxx.xlsx");
try {
FileUtils
.writeByteArrayToFile(file
,bytes
);
} catch (IOException e
) {
e
.printStackTrace();
}
}
public static void upload(){
String str
= "D:/无标题.png";
File file
= new File(str
);
String name
= file
.getName();
long size
= file
.length();
try {
byte[] bytes
= FileUtils
.readFileToByteArray(file
);
String upload
= DfsUtil
.upload(bytes
, size
, name
);
System
.out
.println(upload
);
} catch (IOException e
) {
e
.printStackTrace();
}
}
}