以上传图片为例
步骤一:编写工具类
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);
}
}