FastDFS工具类-FastDFSClient package com.taotao.util;import org.csource.common.NameValuePair;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.StorageClient1;import org.csource.fastdfs.StorageServer;import org.csource.fas
package com.taotao.util;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* FastDFS工具类
*/
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf,Class c) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", c.getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* Title: uploadFile
* Description:
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* Title: uploadFile
* Description:
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
FastDFS-上传文件
package com.taotao.controller;
import com.taotao.common.util.JsonUtils;
import com.taotao.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
/**
* 图片上传Controller
*/
@Controller
public class PictureController {
@Value("${IMAGE_SERVER_URL}")
private String IMAGE_SERVER_URL;
/**
* 上传文件
*
* @param uploadFile 上传的文件
* @return
*/
@RequestMapping("/pic/upload")
@ResponseBody
public String picUpload(MultipartFile uploadFile) {
//接收上传的文件
//取扩展名
String originalFilename = uploadFile.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
//上传到图片服务器
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fastDFSClient.properties",this.getClass());
String url = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
//拼接完整的图片url ip+图片地址
url = IMAGE_SERVER_URL + url;
//相应上传图片的url
Map
result = new HashMap<>();
result.put("error", 0);
result.put("url", url);
//这里考虑到浏览器兼容性问题,不直接返回map,而是先将map转换成json字符串返回
String resultStr = JsonUtils.objectToJson(result);
return resultStr;
} catch (Exception e) {
e.printStackTrace();
Map
result = new HashMap<>(); result.put("error", 1); result.put("message", "图片上传失败"); //这里考虑到浏览器兼容性问题,不直接返回map,而是先将map转换成json字符串返回 String resultStr = JsonUtils.objectToJson(result); return resultStr; } } }
