实现图片上传与下载 - 采用分布式文件系统 - 可查看https://blog.csdn.net/qq_38225558/article/details/86511320
一、后端实现:
中
1.导入依赖:
<dependency><groupId>com.zhengqing</groupId><artifactId>basic_util</artifactId><version>1.0-SNAPSHOT</version>
</dependency>
<!--fastdfs依赖-->
<dependency><groupId>org.csource.fastdfs</groupId><artifactId>fastdfs</artifactId><version>1.2</version>
</dependency>
2.配置文件:
3.工具类:
public class FastDfsApiOpr {public static String CONF_FILENAME = FastDfsApiOpr.class.getClassLoader().getResource("fast_client.conf").getFile();/*** 上传文件*/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;}}/*** 上传文件*/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;}}/*** 下载文件*/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;} }/*** 删除文件*/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());} }
}
3.编写controller代码:
/*** 上传和下载暂时只给外部页面之间调用 -》 不写feign*/
@RestController
public class FastDfsController {@RequestMapping(value = "/upload",method = RequestMethod.POST)public AjaxResult upload(@RequestParam(value = "file",required = true) MultipartFile file){try {System.out.println(file.getOriginalFilename() + ":" + file.getSize());String originalFilename = file.getOriginalFilename();// xxx.jpgString 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@RequestMapping(value = "/del",method = RequestMethod.DELETE)public AjaxResult upload(@RequestParam(value = "filePath",required = true)String filePath){String pathTmp = filePath.substring(1); // goup1/xxxxx/yyyyString groupName = pathTmp.substring(0, pathTmp.indexOf("/")); //goup1String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// xxxxx/yyyySystem.out.println(groupName);System.out.println(remotePath);FastDfsApiOpr.delete(groupName, remotePath);return AjaxResult.me();}
}
4.启动测试
postman测试:
上传文件
检验是否成功:http://172.16.14.211/group1/M00/00/02/rBAO01w_ck-AAG02AAAdIsUcl40226.jpg
删除文件:
再刷新浏览器检查是否成功删除
二、前端实现: 代码部分略...
源码和文档:https://pan.baidu.com/s/1a6GNBSmP2H2y_VuZ9weySw