springBoot整合FastDFS
1、maven配置
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
2、resource 中新增文件 fdfs.properties
# connect timeout in seconds # default value is 30s connect_timeout=30 # network timeout in seconds # default value is 30s network_timeout=60 # the base path to store log files base_path=/usr/local/fastdfs/client # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.0.44:22122 #tracker_server=192.168.0.5:22122 #standard log level as syslog, case insensitive, value list: ### emerg for emergency ### alert ### crit for critical ### error ### warn for warning ### notice ### info ### debug log_level=info # if use connection pool # default value is false # since V4.05 use_connection_pool = false # connections whose the idle time exceeds this time will be closed # unit: second # default value is 3600 # since V4.05 connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server # since V4.05 # default value is false load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # default value is false # since V4.05 use_storage_id = false # specify storage ids filename, can use relative or absolute path # same as tracker.conf # valid only when load_fdfs_parameters_from_tracker is false # since V4.05 storage_ids_filename = storage_ids.conf #HTTP settings http.tracker_server_port=8080 #use "#include" directive to include HTTP other settiongs ##include http.conf
3、application.yml配置
#fdfs 配置信息 tracker IP端口 httpUrl: http://192.168.0.44:8080/
#fdfs 配置文件名称 fdfsPropertiesName: fdfs.properties
4、代码
public class FileController { @Value("${httpUrl}") private String HTTP_URL; @Value("${fdfsPropertiesName}") private String CONF_FILENME; @ApiOperation("上传图片") @RequestMapping("/image/uploadImage") public String uploadImage(MultipartFile file){ StringBuffer imageUrl = new StringBuffer(); imageUrl.append(HTTP_URL); try { ClientGlobal.init(CONF_FILENME); //创建一个tracker客户端对象 TrackerClient trackerClient = new TrackerClient(); //使用trackerclient连接trackersever对象 TrackerServer trackerServer = trackerClient.getConnection(); //创建一个straged server对象 StorageServer storageServer = null; //创建一个storage client 对象 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //利用storageclient对象上传图片 //参数一为文件所在的路径(不要有中文) //参数二为文件的拓展名 //参数三为文件扩展信息 String originalFilename = file.getOriginalFilename(); System.out.println(originalFilename); String img=originalFilename.substring(originalFilename.lastIndexOf(".") + 1); System.out.println(img); String[] upload_file = storageClient.upload_file(file.getBytes(), img , null); //upload_file 存储着访问路径 for (String s : upload_file) { System.out.println(s); } imageUrl.append(upload_file[0] + "/" + upload_file[1]); System.out.println(imageUrl.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return imageUrl.toString(); } /** * 批量上传 * @param * @return */ @ApiOperation("上传图片") @RequestMapping("/uploadImage") public String uploadImage(MultipartFile[] file){ StringBuffer imageUrl = new StringBuffer(); try { ClientGlobal.init(CONF_FILENME); //创建一个tracker客户端对象 TrackerClient trackerClient = new TrackerClient(); //使用trackerclient连接trackersever对象 TrackerServer trackerServer = trackerClient.getConnection(); //创建一个straged server对象 StorageServer storageServer = null; //创建一个storage client 对象 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //利用storageclient对象上传图片 //参数一为文件所在的路径(不要有中文) //参数二为文件的拓展名 //参数三为文件扩展信息 for (MultipartFile multipartFile : file) { String originalFilename = multipartFile.getOriginalFilename(); System.out.println(originalFilename); String img=originalFilename.substring(originalFilename.lastIndexOf(".")+1); System.out.println(img); String[] upload_file = storageClient.upload_file(multipartFile.getBytes(),img , null); imageUrl.append(HTTP_URL); //upload_file 存储着访问路径 for (String s : upload_file) { System.out.println(s); } imageUrl.append(upload_file[0]+"/"+upload_file[1]+","); } System.out.println(imageUrl.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return imageUrl.toString(); } @ApiOperation("删除图片") @RequestMapping("/deleteFile") public void deleteFile(String fileName){ try { ClientGlobal.init(CONF_FILENME); TrackerClient trackerClient = new TrackerClient(); TrackerServer connection = trackerClient.getConnection(); StorageServer storageServer=null; StorageClient storageClient = new StorageClient(connection, storageServer); //截取文件分组 //String url = "http://192.168.0.44:8080/group1/M00/00/00/rBEQIl8ROTKAIm_JAAAM9WnKN_I424.png"; int group = fileName.indexOf("group"); //获取到文件名 String fileName2 = fileName.substring(group); String groupName = fileName2.substring(0, fileName2.indexOf("/")); String pathName = fileName2.substring(fileName2.indexOf("/")); int i = storageClient.delete_file(groupName, pathName); System.out.println(i == 0 ? "删除失败" : "删除成功" + i); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } }