文件上传(FastDfs的基本使用)

以上传图片为例

步骤一:编写工具类

package com.qf.fayuan.utils;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.IOException;

public class FastDfsUtils {

    private TrackerClient trackerClient;
    private TrackerServer trackerServer;
    private StorageServer storageServer;


    public FastDfsUtils(String configlocation) throws IOException, MyException {

        if(configlocation.startsWith("classpath:")){
            configlocation = configlocation.replace("classpath:", this.getClass().getResource("/").getPath());
        }
        ClientGlobal.init(configlocation);
        trackerClient = new TrackerClient();
        trackerServer = trackerClient.getConnection();
    }

    public String fileUpLoad(byte[] bytes,String file_end) throws IOException, MyException {
        return  fileUpLoad(bytes,file_end,null);
    }

    public String fileUpLoad(byte[] bytes, String file_end, NameValuePair[] nameValuePairs) throws IOException, MyException {
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
        String[] strings = storageClient1.upload_file(bytes, file_end, nameValuePairs);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < strings.length; i++) {
            stringBuffer.append(strings[i]);
            if(i==0){
                stringBuffer.append("/");
            }
        }
        return stringBuffer.toString();


    }

}

步骤二:编写配置文件类,在服务器启动的时候加载工具类

package com.qf.fayuan.config;

import com.qf.fayuan.utils.FastDfsUtils;
import org.csource.common.MyException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class CommsConfig {

    @Bean
    public FastDfsUtils getFastDfsUtils() throws IOException, MyException {
        FastDfsUtils fastDfsUtils = new FastDfsUtils("classpath:config.properties");
        return fastDfsUtils;
    }
}

步骤三:使用测试

package com.qf.fayuan.notice.controller;


import com.qf.fayuan.bean.ResultBean;
import com.qf.fayuan.notice.pojo.Notice;
import com.qf.fayuan.notice.service.NoticeService;
import com.qf.fayuan.user.service.UserService;
import com.qf.fayuan.utils.CheckNotNull;
import com.qf.fayuan.utils.ErrorCodeInterface;
import com.qf.fayuan.utils.FastDfsUtils;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/notice")
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private FastDfsUtils fastDfsUtils;

    @RequestMapping("/addnotice")
    public ResultBean addNotice(Notice notice, MultipartFile file){

        String filename = file.getOriginalFilename();
        String substring = filename.substring(filename.lastIndexOf(".") + 1);
        try {
            String s = fastDfsUtils.fileUpLoad(file.getBytes(), substring);
            String check = CheckNotNull.check(notice);
            if(check!=null){
                String[] s1 = check.split(" ");
                return  ResultBean.setError(Integer.valueOf(s1[0]),s1[1],null);
            }
            notice.setImg(s);
            noticeService.addNotice(notice);
            return ResultBean.setOk("success");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return ResultBean.setError(ErrorCodeInterface.QINGXUANZEFAYUAN,"添加失败",null);
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值