fastdfs-client-java的依赖包里面没有相关方法的解决方案

fastdfs-client-java的依赖包里面没有相关方法的解决方案

前提:使用FastDfs-client之前,要先在服务器里配置好fastdfs和nginx,若没有配置好的可以参考[https://blog.csdn.net/qq_41946543/article/details/102811191]

作者已经很长时间没有更新这个依赖包的内容了,我们可以从下面作者的库里面,克隆他的源码下来,自行安装

https://github.com/happyfish100/fastdfs-client-java

右击下载下来的源项目,选择Run Maven > clean install

CB9VMoGnNsX8RdY

安装完成后,在自己项目的pom.xml里面写上

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27-SNAPSHOT</version>
</dependency>

在spring-mvc.xml配置文件里面写上

<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="defaultEncoding" value="UTF-8"/>
 <!--文件上传的大小 单位byte 20Mb -1是不限制大小-->
 <property name="maxUploadSize" value="20971520"/>
 </bean>

创建一个资源文件 名字自定义,这里我是fastdfs-client.properties

T3NEAFDtBalbQZC

fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false
fastdfs.http_secret_key=FastDFS1234567890
fastdfs.http_tracker_http_port=80
fastdfs.tracker_servers=192.168.31.128:22122

创建一个FastDfs工具类

public class FastDfsUtils {

    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageClient1 storageClient1;

    static {
        try {
            // 初始化资源文件
            ClientGlobal.initByProperties("config/db/fastdfs-client.properties");
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     *
     * @param fileBytes 文件的字节数组
     * @param suffix    文件的后缀
     * @return
     */
    public static String upload(byte[] fileBytes, String suffix) {
        try {
            return storageClient1.upload_file1(fileBytes, suffix, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return null;
    }

创建一个File工具类

public class FileUtils {
        public static String getFileSuffix(String originalFilename) {
        // 获取文件的后缀   xx.png
        int index = originalFilename.lastIndexOf(".");
        return originalFilename.substring(index + 1);
    }
}

创建LoginController类

@Controller
 public class LoginController {
 	@GetMapping("/login")
 	public String login() {
 		return "login";
 	}
 }

创建一个FileUploadController类

@Controller
public class FileUploadController{
        @PostMapping("/uploadFile1")
    public String uploadFile1(MultipartFile file, Model model) throws IOException {
        // 文件上传
        String fileName = FastDfsUtils.upload(file.getBytes(), FileUtils.getFileSuffix(file.getOriginalFilename()));
        model.addAttribute("fileName", fileName);
        return "img2";
    }
}

分别创建两个jsp页面进行测试

创建第一个页面 login.jsp,用来上传图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


<form action="/uploadFile1" method="post" enctype="multipart/form-data" >
    <input type="file" name="file" />
    <input type="submit" value="提交">
</form>
</body>
</html>

创建第二个页面 img2.jsp,用来查看上传的图片

<%@page contentType="text/html; charset=UTF-8" language="java"%>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片展示</title>
</head>
<body>
<%--这里的ip为你自己虚拟机的ip--%>
<img src="http://192.168.31.128:8888/${fileName}" alt="上传图片"/>
</body>
</html>

最后启动tomcat,访问login页面,选择上传文件,提交

dhP1T9DWIvw8YyO

显示出图片,利用FastDfs上传文件成功~

9GrAMdiOtS1vjoH

总结:根据作者写的文档还没办法把效果实现出来,工具类需要自己去写,但也不难,只需要看看他的几段源码就能够知道来龙去脉了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值