public class UploadUtil {
public static String uploadImage(MultipartFile multipartFile){
String imgUrl = Constant.trackerServerIP; //fastdfs服务器ip自行设置
//上传图片服务器
//配置fdfs的全局链接地址
String tracker = PmsUploadUtil.class.getResource("/tracker.conf").getPath();//获得配置文件的路径
try {
ClientGlobal.init(tracker);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
TrackerClient trackerClient = new TrackerClient();
//获得trackerServer的实例
TrackerServer trackerServer = null;
try {
trackerServer = trackerClient.getConnection();
} catch (IOException e) {
e.printStackTrace();
}
//通过tracker获得Storage链接客户端
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] bytes =null;
try {
bytes = multipartFile.getBytes(); //获得上传的二进制对象
} catch (IOException e) {
e.printStackTrace();
}
//获取文件后缀名
String originalFilename = multipartFile.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
//获取返回的文件信息数组
String [] uploadInfos = null;
try {
uploadInfos = storageClient.upload_file(bytes,extName,null);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
//获取图片服务器路径
for (String uploadInfo : uploadInfos) {
imgUrl += "/"+uploadInfo;
}
return imgUrl;
}
}
来源:CSDN
作者:张张张小哥儿
链接:https://blog.csdn.net/ZHC_AC/article/details/104010052