springboot把照片从A服务器传到B文件服务器上

首先利用maven导入jar包


com.github.tobato
fastdfs-client
1.26.5

其次: 写好文件上传工具类:
package com.xxtx.web.noticeCenter.util;

import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Set;

/**

  • Fastdfs工具类
    */
    @Component
    public class FastdfsClientUtil {

    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private ThumbImageConfig thumbImageConfig;

    //上传文件
    public String uploadImage(MultipartFile myfile) throws Exception{
    //文件名
    String originalFilename = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf(".") + 1);
    // 文件扩展名
    String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());

    StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(myfile.getInputStream(), myfile.getSize(),originalFilename , null);

    String path = storePath.getFullPath();

    return path;
    }

    /**

    • 根据文件流上传文件
    • @return
      */
      public String uploadExcel(InputStream inputStream, long fileSize, String fileExtName){
      StorePath store = storageClient.uploadFile(inputStream, fileSize, fileExtName, null);
      return store.getFullPath();
      }

    /**

    • 删除文件
    • @Param fileUrl 文件访问地址
      */
      public void deleteFile(String fileUrl) {
      if (StringUtils.isEmpty(fileUrl)) {
      return;
      }
      try {
      StorePath storePath = StorePath.parseFromUrl(fileUrl);
      storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
      } catch (FdfsUnsupportStorePathException e) {
      e.printStackTrace();
      }
      }
      }

一些自定义配置类
package com.xxtx.web.noticeCenter.util;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {

}

在yml中,配置上传文件服务器的路径,吧ip换成自己的就可以了
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 200
height: 200
tracker-list: #TrackerList参数,支持多个
- ${FDFS.TRACKER.LIST:192.168.0.105:22122}

到此就结束啦,当然你也可以在B文件服务器启动个服务,写个Controller来接收A服务器传来的数据。。 这个就看个人爱好啦

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值