fastdfs文件上传

fastdfs集成spring mvc

1.安装jar包至本地仓库

install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastjson -Dversion=1.2 -Dfile=C:\Windows\System32\fastdfs_client_v1.20.jar -Dpackaging=jar

jar包下载地址

链接:https://pan.baidu.com/s/1cgVLFb8zqNu9Vq0zOS9kEA
提取码:7eyo

2.导入依赖

	//fastdfs
	<dependency>
   		<groupId>org.csource.fastdfs</groupId>
    	<artifactId>fastdfs</artifactId>
		<version>1.2</version>
	</dependency>

	//文件上传组件
	<dependency>
	  <groupId>commons-fileupload</groupId>
	  <artifactId>commons-fileupload</artifactId>
	  <version>1.3.1</version>
	</dependency>

3.上传文件代码

tracker配置文件 文件名 tracker.conf

//tracker客户端地址
tracker_server=192.168.94.3:22122

spring mvc配置文件

<!-- 配置文件解析器对象,要求id名称必须是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--最大文件的大小-->
    <property name="maxUploadSize" value="10485760"/>
</bean>

UploadUtil类

package com.pingyougou.common.utils;

import org.csource.fastdfs.*;


public class UploadUtil {

    //本地文件上传
    public static String[] upload(String confFileName,String filePath) throws Exception {
        ClientGlobal.init(confFileName);
        //创建tracker客户端
        TrackerClient trackerClient = new TrackerClient();
        //链接tracker服务
        TrackerServer trackerServer = trackerClient.getConnection();
        //通过链接tracker获得Storage信息并创建一个Storage服务端和客户端
        StorageClient storageClient = new StorageClient(trackerServer, null);

        /***
         * 通过客户端想storage上传图片
         * buffer:文件的字节数组
         * subfix:文件的后缀
         * 上传响应信息
         * fileinfos[0]:文件存储的组名
         * fileinfos[1]:文件的其他存储区间名和文件名的组合
         */
        String[] strings = storageClient.upload_file(filePath, null, null);
        return strings;
    }

    //远程文件上传
    public static String upload(String confFileName,byte[] buffer,String suffix) throws Exception {
        //获取配置文件路径
        confFileName = getConfig(confFileName);
        //初始化tracker服务
        ClientGlobal.init(confFileName);
        //创建tracker客户端
        TrackerClient trackerClient = new TrackerClient();
        //链接tracker服务
        TrackerServer trackerServer = trackerClient.getConnection();
        //通过链接tracker获得Storage信息并创建一个Storage服务端和客户端
        StorageClient storageClient = new StorageClient(trackerServer, null);



        /***
         * 通过客户端想storage上传图片
         * buffer:文件的字节数组
         * subfix:文件的后缀
         * 上传响应信息
         * fileinfos[0]:文件存储的组名
         * fileinfos[1]:文件的其他存储区间名和文件名的组合
         */
        String[] strings = storageClient.upload_file(buffer, suffix, null);
        if (strings != null) {
            return "192.168.94.3" + "/" +strings[0] + "/" + strings[1];
        }
        return null;
    }
    /***
     * 获取配置文件路径
     * @param conf
     * @return
             */
    public static String getConfig(String conf){
        if (conf.contains("classpath:")) {
            conf = conf.replace("classpath:",UploadUtil.class.getResource("/").getPath());
        }
        return conf;
    }

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\Java_Web\\hm49\\code\\pingyougou-parent\\pingyougou-common\\src\\main\\resources\\capture_20200426215004454.jpeg";
        String tracker = "D:\\Java_Web\\hm49\\code\\pingyougou-parent\\pingyougou-common\\src\\main\\resources\\tracker.conf";

        String[] upload = upload(tracker,filePath);

        for (String s : upload) {
            System.out.println(s);
        }
    }
}

UploadController

package com.pingyougou.shop.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.pingyougou.common.utils.UploadUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("upload")
public class UploadController {

    @PostMapping("image")
    public ResponseEntity<String> uploadImage(@RequestParam("file")MultipartFile file) throws Exception {
        String url = UploadUtil.upload("classpath:tracker.conf", file.getBytes(), file.getOriginalFilename());
        if (StringUtils.isBlank(url)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }

        return ResponseEntity.status(HttpStatus.CREATED).body(url);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文本格式不能很好显示,请见谅(附件里有比较齐整的excel表格统计) 大小类型 传输类型 api方法 文件大小 花费时间 速率byte/ms 速率mb/s 缓存数组 次数 备注 大文件 下载 download_file(group_name, remote_filename, localFile) 1073741824(约1G) 28343ms 37883 36.12804413 无 1 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 29195ms 36778 35.07423401 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 24352ms 44092 42.04940796 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 24831ms 43241 41.23783112 2K 1 实现DownloadCallback,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 25922ms 41422 39.50309753 8K 1 实现DownloadCallback,BufferedOutputStream 普通文件 下载 download_file(group_name, remote_filename, localFile) 59113472(约56M) 382ms 154747 147.5782394 无 1 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约57M) 369ms 160199 152.7776718 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约58M) 499ms 118702 113.2030487 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 59113472(约59M) 592ms 99853 95.22724152 2K 1 实现DownloadCallback,BufferedOutputStream 下载建议:100M内数据使用fastDFS提供的DownloadStream;大于1G的数据,使用BufferedOutputStream和DowloadStream

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值