FastDFS是通过StorageClient来执行上传操作的
通过看源码我们知道,FastDFS有两个StorageClient工具类。
StorageClient的上传方法upload_file(…)返回的是字符串数组String[],
如[group1,M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg]
StorageClient1的上传方法upload_file(…)返回的是字符串数组String,
如group1/M00/00/00/wKgAb1dBK2iANrayAA1rIuRd3Es112.jpg,也就是已经帮我们拼接好了
所以使用StorageClient1的上传方法更方便,不用我们自己拼接了。
FastDFSClient
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
//使用StorageClient1进行上传
private StorageClient1 storageClient1 = null;
public FastDFSClient(String conf) throws Exception {
//获取classpath路径下配置文件”fdfs_client.conf”的路径
//conf直接写相对于classpath的位置,不需要写classpath:
String configPath = this.getClass().getClassLoader().getResource(conf).getFile();
System.out.println(configPath);
ClientGlobal.init(configPath);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
}
public String uploadFile(byte[] file_buff, String file_ext_name) throws Exception {
String result = storageClient1.upload_file1(file_buff, file_ext_name, null);
return result;
}
public String uploadFile(String local_filename, String file_ext_name) throws Exception {
String result = storageClient1.upload_file1(local_filename, file_ext_name, null);
return result;
}
}
测试
public class FastDFSTest {
public static void main(String[] args) throws Exception {
//不要带classpath
FastDFSClient client = new FastDFSClient(“properties/fdfs_client.conf”);
String result = client.uploadFile(“C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg”, “jpg”);
System.out.println(result);
}
}
在项目中,图片上传的实现
Service层
@Service
public class PictureServiceImpl implements PictureService {
//将需要拼接的ip地址写在配置文件中,交给spring管理
@Value(“${IAMGE_SERVER_BASE_UEL}”)
private String IAMGE_SERVER_BASE_UEL;
@Override
public PictureResult uploadPic(MultipartFile picFile) {
PictureResult result = new PictureResult();
//判断图片是否为空
if (picFile.isEmpty()) {
result.setError(1);
result.setMessage(“图片为空”);
return result;
}
//上传到图片服务器
try {
//获取图片扩展名
String originalFilename = picFile.getOriginalFilename();
//取扩展名,不要”.”
String extName = originalFilename.substring(originalFilename.lastIndexOf(“.”) + 1);
FastDFSClient client = new FastDFSClient(“properties/fdfs_client.conf”);
String url = client.uploadFile(picFile.getBytes(), extName);
//拼接图片服务器的ip的地址(写配置文件中)
url = IAMGE_SERVER_BASE_UEL + url;
//把url响应给客户端
result.setError(0);
result.setUrl(url);
} catch (Exception e) {
e.printStackTrace();
result.setError(1);
result.setMessage(“图片上传失败”);
}
return result;
}
}
表现层
@Controller
public class PictureController {
@Autowired
PictureService pictureService;
@RequestMapping(“/pic/upload”)
@ResponseBody
public PictureResult uploadFile(MultipartFile uploadFile){
PictureResult result = pictureService.uploadPic(uploadFile);
return result;
}
}
经测试发现,chrome可以正常上传,但是firefox不能,说明兼容性有问题
这就要求我们将返回的数据转换成一个字符串文本,浏览器对字符串的兼容性最好、
封装一个工具类
JsonUtils
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*/
public static T jsonToPojo(String jsonData, Class beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*/
public static List jsonToList(String jsonData, Class beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
修改后的controller
@Controller
public class PictureController {
@Autowired
PictureService pictureService;
@RequestMapping(“/pic/upload”)
@ResponseBody
public String uploadFile(MultipartFile uploadFile){
PictureResult result = pictureService.uploadPic(uploadFile);
//需要把java对象转换成json数据
String json = JsonUtils.objectToJson(result);
return json;
}
}