<span style="color:#cc0000">首先引入FastDFS依赖,中央仓库中没有,自己下载反向安装maven到本地仓库</span>
import java.io.IOException;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private TrackerServer trackerServer;
// 配置文件路径
public FastDFSClient() {
}
public FastDFSClient(String configLocation) throws Exception{
// 加载配置文件
ClientGlobal.init(configLocation);
// 创建 tracker客户端
TrackerClient trackerClient = new TrackerClient();
// 获取tracker连接
trackerServer = trackerClient.getConnection();
}
/**
* 获取文件扩展名
* @param fileName
* @return
*/
public static String getFileExtension(String fileName){
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* 上传文件
* @param data
* @param fileName
* @return
* @throws MyException
* @throws IOException
*/
public String uploadFile(byte[] data,String fileName) throws IOException, MyException{
// 查看storage客户端
StorageClient1 storageClient = new StorageClient1(trackerServer, null);
// 上传
return storageClient.upload_file1(data, getFileExtension(fileName), null);
}
}
fdfs_client.conf 配置文件放在项目根目录下
配置文件放在项目根目录下
connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.56.101:22122
controller类
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.taotao.manage.utils.FastDFSClient;
import com.taotao.manage.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
/****
* KindEditor文件上传
*/
@Controller
@RequestMapping("pic/upload")
public class PicUploadResultController {
private static final List<String> EXTENSIONS = Arrays.asList(".jpg", ".png", ".bmp", ".jpeg");
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public PicUploadResult upload(@RequestParam("uploadFile")MultipartFile multipartFile, HttpServletRequest request){
PicUploadResult result = new PicUploadResult();
result.setError(1);
try {
//获取绝对路径
String realPath = request.getSession().getServletContext().getRealPath("upload");
//获取文件名,设置中文乱码
String filename = multipartFile.getOriginalFilename();
//文件校验,后缀名,是否是图片
//截取文件名得到后缀
String extension = "."+StringUtils.substringAfterLast(filename, ".") ;
if (!EXTENSIONS.contains(extension)){
return result;
}
//是否是图片
BufferedImage bufferedImage = ImageIO.read(multipartFile.getInputStream());
if(bufferedImage==null){
return result;
}
/* 这是将文件保存到项目指定目录,太low了
//将文件保存到指定路径
multipartFile.transferTo(new File(realPath,filename));
//0代表成功,1代表失败
result.setError(0);
result.setUrl("http://manage.taotao.com/upload/"+filename);
*/
//使用Nginx保存
//创建FastDFS客户端
FastDFSClient fastDFSClient = new FastDFSClient("fdfs_client.conf");
//上床文件获取路径
String path = fastDFSClient.uploadFile(multipartFile.getBytes(), filename);
result.setError(0);
result.setUrl("http://image.taotao.com/"+path);
return result;
} catch (Exception e) {
e.printStackTrace();
return result;
}
}
}