FastDFS实现文件上传下载实战


       正好,淘淘商城讲这一块的时候,我又想起来当时老徐让我写过一个关于实现FastDFS实现文件上传下载的使用文档,当时结合我们的ITOO的视频系统和毕业论文系统,整理了一下,有根据网上查到的知识,总结了一点东西,下面分享一下


一、FastDFS简介:

        参见博客:http://blog.csdn.net/liweizhong193516/article/details/53234486


二、FastDFS使用流程介绍:        

       我们在itoo的dobbu+zk框架中使用fastdfs+nginx+mysql实现上传附件的功能,主要原理就是将附件上传到fastdfs得到一个文件的链接路径url,我们获取到这个url将他以字符串形式保存到我们的mysql中,下载的时候获取到这个url直接打开或者下载附件。(url内容参见博客:http://blog.csdn.net/liweizhong193516/article/details/52556526)

       FastDFS实现文件上传下载实战_第1张图片FastDFS实现文件上传下载实战_第2张图片

               

               

        默认的,我们就知道fastdfs后面还有一个nginx服务器,当我们进行存储文件的时候,我们要通过fastdfs给我们生成一个保存地址,也就是grounpID 和保存地址,然后将文件存在fastdfs服务器中,fastdfs中有许多的16进制命名文件夹,文件夹中套文件夹;当我们进行读取文件的时候,我们要通过获取mysql里面的相应文件的存储地址去nginx服务器中通过url路径文件名,从fastdfs文件中取出我们的文件进行展示。(所以下载图,有点问题,在这里解释一下)


三、实现流程:

1、配置依赖:因为我们使用的maven来管理工程,所以,我们需要去陪pom文件

  
	4.0.0   
	com.leech  
	fastdfs-demo  
	0.0.1-SNAPSHOT  
	jar   
	fastdfs-demo  
	http://maven.apache.org   
	    
		UTF-8  
	   
	
	    
		      
			junit      
			junit      
			4.11      
			test    
		         
		      
			org.csource      
			fastdfs-client-java      
			1.25    
		         
		        
			commons-io        
			commons-io        
			2.4    
			       
		
	
当然,更多时候我们利用maven的继承特性,直接配置在父工程中,然后继承下来就o了。

2、新建fdfs_client.conf文件,在我们的itoo中,我们建立在(src/main/resources底下)

connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80 
#没什么用
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.17.112:22122 
#tracker_server = 192.168.0.119:22122


3、实现文件上传:

       public void save(HttpServletRequest request,HttpServletResponse response){		
		String videoName=request.getParameter("videoName");
		String videoType=request.getParameter("videoType");
		String videoDesc=request.getParameter("videoDesc");
		String videoPath=request.getParameter("videoPath");
		String picturePath=request.getParameter("picturePath");

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date uploadTime=null;
		try {
			uploadTime = formatter.parse(formatter.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();
		
		String dataBaseName = "itoo_video";
	
		Video video=new Video();
		video.setUserName("why");
		video.setUserID("why");		
		video.setVideoName(videoName);
		video.setVideoPath(videoPath);
		video.setVideoType(videoType);
		video.setVideoDesc(videoDesc);	
		video.setDataBaseName(dataBaseName);
		video.setPicturePath(picturePath);
		video.setUploadTime(uploadTime);
		
		uploadFileService.save(video);		
		
        jackJsonUtil.beanToJson(response,video);
		
	}
	
	/*
	 * 上传文件
	 */
	@RequestMapping(value={"/upload"})
	@ResponseBody
	public void upload( MultipartFile file, HttpServletRequest request,HttpServletResponse response){		
		
		String ext_Name = file.getOriginalFilename().split("\\.")[1];
		String videoName=file.getOriginalFilename().split("\\.")[0];
		
        byte[] bytes = null;
		try {
			bytes = file.getBytes();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String videoPath=uploadFile(bytes,ext_Name);
		
		JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();
		
		Video video=new Video();
		video.setVideoPath(videoPath);
		video.setVideoName(videoName);		
		
		jackJsonUtil.beanToJson(response,video);
	} 
	
	public String uploadFile(byte[] byteFile, String ext_file) {
		// 拼接服务区的文件路径
		StringBuffer sbPath = new StringBuffer();
		sbPath.append("http://192.168.22.252");
		try {
			// 初始化文件资源
			ClientGlobal
					.init("C:\\Users\\alsr\\Desktop\\ITOO-5.0\\itoo-video-Test\\dmsd-itoo-video-parent\\dmsd-itoo-video-web\\src\\main\\resources\\fdfs_client.conf");

			// 链接FastDFS服务器,创建tracker和Stroage
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			StorageServer storageServer = null;
			StorageClient storageClient = new StorageClient(trackerServer,
					storageServer);
			//利用字节流上传文件
			String[] strings = storageClient.upload_file(byteFile, ext_file, null);

			for (String string : strings) {
				sbPath.append("/" + string);
				System.out.println(string);
			}
			// 全路径
			System.out.println(sbPath);
		} catch (IOException | MyException e) {
			e.printStackTrace();
		}
		return sbPath.toString();
	}
       只要我们能正常接收到一个json类型的字符串(url地址),就证明我们已经上传成功了,如果不信,可以直接用浏览器去验证一下,看看能不能得到文件展示。


4、FastDFS实现文件下载:

public void testDownload() {        
		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("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             
			System.out.println(b);             
			IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".conf"));        
			} 
		catch (Exception e) {             
			e.printStackTrace();         
		}     
	}       


5、FastDFS获取将上传文件信息:

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();         
			}     
		}       


6、FastDFS获取文件名称:

	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();         
				}     
		}    

7、FastDFS实现删除文件:

             public void testDelete(){         
			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("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");             
				System.out.println( i==0 ? "删除成功" : "删除失败:"+i);         
				} 
			catch (Exception e) {             
				e.printStackTrace();         
				}     
             }




你可能感兴趣的:(【FastDFS】)