配置文件
配置fdfs_client.conf配置文件
# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000
tracker_server = 47.98.159.15:22122
导入依赖
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> </dependency>
配置客户端
package panda.data.Utils; import org.csource.common.MyException; import org.csource.fastdfs.*; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.stream.Stream; /** * @Auther: 罗罗 * @Description: */ public class FastDFSClient { private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class); public static String uploadImage(MultipartFile multipartFile) throws IOException, MyException { // 1、初始化全局配置。加载一个配置文件。 String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath(); ClientGlobal.init(filePath); // 2、创建一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); // 3、创建一个TrackerServer对象。 TrackerServer trackerServer = trackerClient.getConnection(); // 4、声明一个StorageServer对象,null。 StorageServer storageServer = null; // 5、获得StorageClient对象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); byte[] bytes = multipartFile.getBytes();// 获得上传的二进制对象 String[] strings = storageClient.upload_file(bytes, "jpg", null); Stream.of(strings).forEach(System.out::println); String imagurl = "http://47.98.159.35/"+strings[0]+"/"+strings[1]; //返回值0代表删除成功 //int result = storageClient.delete_file("group1", "M00/00/00/wKgAaFyM3fSANCgXAABMnNm0e54098.jpg"); //System.out.println("result="+result); return imagurl; } }
package panda.data.controller; import org.csource.common.MyException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import panda.data.ResultData.ImgAttr; import panda.data.ResultData.ResultData; import panda.data.Utils.FastDFSClient; import java.io.IOException; import java.util.ArrayList; @Controller public class ImgController { @Autowired private ResultData resultData; //必须是post方式 @PostMapping("/fileUpload") @ResponseBody public ResultData fileUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException, MyException { String url = FastDFSClient.uploadImage(multipartFile); if (url.contains("group")){ resultData.setCode(0); ImgAttr imgAttr = new ImgAttr(url); ArrayList<Object> list = new ArrayList<>(); list.add(imgAttr); resultData.setData(list); }else { resultData.setCode(0); resultData.setData(null); } return resultData; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/198966/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)