1.fastdfs
1.为什么需要fastdfs
在集群环境下,统一管理图片,文件。
2.项目集成交互图
上传:(地址相对于fastdfs服务器)上传图片<—>commonservice<—>fastdfs–
—》地址存储到自己服务器
访问:fastdfsip+从自己的服务器获取相对地址
3.步骤
1.搭建fastdfs
2.写astdfs服务
1.写Util
public static String CONF_FILENAME = FastDfsApiOpr.class.getClassLoader()
.getResource("fdfs_client.conf").getFile();
/**
* 上传文件
* @param file
* @param extName
* @return
*/
public static String upload(byte[] file,String extName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair nvp [] = new NameValuePair[]{
new NameValuePair("age", "18"),
new NameValuePair("sex", "male")
};
String fileIds[] = storageClient.upload_file(file,extName,nvp);
System.out.println(fileIds.length);
System.out.println("组名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
return "/"+fileIds[0]+"/"+fileIds[1];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 上传文件
* @param extName
* @return
*/
public static String upload(String path,String extName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String fileIds[] = storageClient.upload_file(path, extName,null);
System.out.println(fileIds.length);
System.out.println("组名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
return "/"+fileIds[0]+"/"+fileIds[1];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 下载文件
* @param groupName
* @param fileName
* @return
*/
public static byte[] download(String groupName,String fileName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file(groupName, fileName);
return b;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// @Test
// public void testGetFileInfo(){
// try {
// ClientGlobal.init(conf_filename);
//
// TrackerClient tracker = new TrackerClient();
// TrackerServer trackerServer = tracker.getConnection();
// StorageServer storageServer = null;
//
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");
// System.out.println(fi.getSourceIpAddr());
// System.out.println(fi.getFileSize());
// System.out.println(fi.getCreateTimestamp());
// System.out.println(fi.getCrc32());
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// @Test
// public void testGetFileMate(){
// try {
// ClientGlobal.init(conf_filename);
//
// TrackerClient tracker = new TrackerClient();
// TrackerServer trackerServer = tracker.getConnection();
// StorageServer storageServer = null;
//
// StorageClient storageClient = new StorageClient(trackerServer,
// storageServer);
// NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");
// for(NameValuePair nvp : nvps){
// System.out.println(nvp.getName() + ":" + nvp.getValue());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
* 删除文件
* @param groupName
* @param fileName
*/
public static void delete(String groupName,String fileName){
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
int i = storageClient.delete_file(groupName,fileName);
System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("删除异常,"+e.getMessage());
}
}
2.写配置文件
tracker_server=122.51.119.246:22122
3.controller
//crud 上传 下载(查看) 删除 修改(先删除后添加)
/**
* 参数:上传文件
* 返回值:成功与否,还要返回地址
*/
@PostMapping
public AjaxResult upload(@RequestParam(required = true,value = "file") MultipartFile file){
try {
System.out.println(file.getOriginalFilename() + ":" + file.getSize());
String originalFilename = file.getOriginalFilename();
// xxx.jpg
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
System.out.println(extName);
String filePath = FastDfsApiOpr.upload(file.getBytes(), extName);
return AjaxResult.me().setResultObj(filePath); //把上传后的路径返回回去
} catch (IOException e) {
e.printStackTrace();
return AjaxResult.me().setSuccess(false).setMessage("上传失败!"+e.getMessage());
}
}
/**
* 参数:完整路径 /goup1/xxxxx/yyyy
* 返回值:成功与否,还要返回地址
*
*/
@DeleteMapping
public AjaxResult del(@RequestParam(required = true,value = "path") String path){
String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
String groupName = pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
System.out.println(groupName);
System.out.println(remotePath);
FastDfsApiOpr.delete(groupName, remotePath);
return AjaxResult.me();
}
4.入口类
@SpringBootApplication
@EnableEurekaClient
public class CommonService2090Application {
public static void main(String[] args) {
SpringApplication.run(CommonService2090Application.class);
}
}
5.配置到eureka
application.yml
spring:
application:
name: commonService
bootstrapt.yml
spring:
profiles:
active: dev
cloud:
config:
name: application-common #github上面名称
profile: ${spring.profiles.active} #环境 java -jar -D xxx jar
label: master #分支
discovery:
enabled: true #从eureka上面找配置服务
service-id: hrm-config-server #指定服务名
eureka:
client:
service-url:
defaultZone: http://localhost:1010/eureka #告诉服务提供者要把服务注册到哪儿 #单机环境
instance:
prefer-ip-address: true #显示客户端真实ip
最后修改前台代码