1.pom配置FastDFS依赖
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.29-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
由于fastdfs-client-java jar包没有上传到中央仓库,所以需要下载源码进行maven编译再上传到项目
下载地址:点击进入下载
2.导入jar包
下载后解压打开项目并进行编译 mvn clean install
所需的jar包
打开我们的项目(idea工具)
File->Project Structure->Dependencies
上传jar包
选择并导入刚刚编译好的jar包
3.配置信息
在resources下新建fdfs-client.conf
connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FASTDFS1234567890
tracker_server = ip:22122
4.图片上传
package org.jeecg.common.util;
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;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* 上传图片到Fast
*
*/
public class FastDFSUtils {
private static final String CONF_FILENAME = "fdfs-client.conf";
private static final String serverUrl = "http://ip";
static {
String filePath = (FastDFSUtils.class.getClass().getResource("/").getPath()+CONF_FILENAME).replaceAll("//","\\");
try {
ClientGlobal.init(filePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
/**
* 功能描述:文件上传
*/
public static String uploadFile(MultipartFile file) throws Exception {
File toFile = MultipartFileToFile.multipartFileToFile(file);
FileInputStream fis = null;
NameValuePair[] meta_list = null;
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
fis = new FileInputStream(toFile);
byte[] file_buff = null;
if (fis != null) {
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
String fileId = storageClient.upload_file1(file_buff, getFileExt(file.getName()), meta_list);
String url = serverUrl+"/"+fileId;
return url;
}
/**
* 功能描述:文件下载
*/
public static InputStream downloadFile(String fileId) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
byte[] bytes = storageClient.download_file1(fileId);
InputStream inputStream = new ByteArrayInputStream(bytes);
inputStream.close();
return inputStream;
}
/**
* 功能描述:获取文件名后缀(不包含点)
*/
private static String getFileExt(String fileName) {
if (fileName == null || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点
}
}
/**
* 功能描述: 删除文件
*/
public static int deleteFile(String fileId) throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
int result = storageClient.delete_file1(fileId);
return result;
}
}
网友评论