上传图片到fastDFS

在fastDFS已配置好的情况下,关于图片上传的业务代码

1.首先在pom文件中导入相关依赖

 <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.1-RELEASE</version>
        </dependency>

2.之后需要在application.yml文件中配置

fdfs:
  tracker-list:
    -192.168.1.6:22122 #tracker服务的地址 可以配置多个trackerserver
  so-timeout: 3000   #读取文件超时时间
  connect-timeout: 3000 #连接超时时间
  thumb-image: #缩略图
    height: 200
    width: 200

fasthome:http://192.168.1.6:8080/

3.然后需要在主启动类上增加两个注解

//FastDFS的两个注解需要配置
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

4.最后就是Controller层的相关代码

package com.baisui.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: baiLu
 * @Date: 2021/09/14/15:54
 * @Description:
 */
@Controller
@RequestMapping("/ruController")
public class ResourcesUploadController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    //application.yml文件中配置的地址
    @Value("${fasthome}")
    private String dfsHome;

    @RequestMapping("/uploadPng")
    public String uploadPng(MultipartFile file){
        //把一个文件上传到FastDFS上
        try{
            //获取文件后缀名
            String fileExtname = FilenameUtils.getExtension(file.getOriginalFilename());

            StorePath StorePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(),file.getSize(),fileExtname,null);

            //获取文件上传的路径
            String fullPath = StorePath.getFullPath();
            //把路径返回给客户端
            return dfsHome+fullPath;

        }catch (IOException e){
            e.printStackTrace();
        }

        return "ok";

    }
}

5.打完收工!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,将ByteArrayInputStream上传到FastDFS时,可能会导致图片丢失一部分。这是因为在上传过程中,可能会出现字节流读取不完整或者写入不完整的情况。为了解决这个问题,可以使用缓冲区来确保字节流的完整性。 以下是一个示例代码,演示了如何将ByteArrayInputStream上传到FastDFS并确保图片完整性: ```java import org.csource.fastdfs.*; import java.io.ByteArrayInputStream; import java.io.IOException; public class FastDFSUploader { public static void main(String[] args) { try { // 加载FastDFS配置文件 ClientGlobal.init("fastdfs.conf"); // 创建Tracker客户端 TrackerClient trackerClient = new TrackerClient(); TrackerServer trackerServer = trackerClient.getConnection(); // 获取可用的Storage服务器 StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); // 创建Storage客户端 StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer); // 生成文件字节数组 byte[] fileBytes = getFileBytes(); // 将字节数组转换为ByteArrayInputStream ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes); // 上传文件 String[] uploadResult = storageClient.upload_file(inputStream, "jpg", null); // 打印上传结果 System.out.println("Upload result: " + uploadResult[0] + "/" + uploadResult[1]); // 关闭Tracker连接 trackerServer.close(); } catch (Exception e) { e.printStackTrace(); } } private static byte[] getFileBytes() { // 从文件或其他来源获取字节数组 // 这里仅作示例,直接返回一个固定的字节数组 return new byte[]{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0}; } } ``` 请注意,上述代码中的`fastdfs.conf`是FastDFS的配置文件,需要根据实际情况进行配置。另外,`getFileBytes()`方法用于获取文件的字节数组,你可以根据实际需求替换为从其他来源获取字节数组的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值