fastdfs-client-java 文件上传

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

导入jar包:fastdfs_client_v1.20.jar

配置文件:fdfs.conf

#fastDFS配置信息
charset = UTF-8
tracker_server = 139.196.152.232:22122
http.tracker_http_port = 80
connect_timeout = 5
network_timeout = 30
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

源码:

接口

/**
 * 
 */
package com.bsh.common.service;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.FileInfo;

/**
 * <b>FastDFS文件存储服务接口</b>
 * 
 * 
 * 
 */
public interface FdfsFileService {
	/**
	 * 上传文件(文件路径方式)
	 * @param file_path 文件路径
	 * @param file_ext_name 文件后缀
	 * @param meta_list 文件附属相关信息 (例如:图片的120、author:XXX等)
	 * @return url 文件url
	 */
	public String upload_file(String file_path,String file_ext_name, NameValuePair[] meta_list) throws Exception;

	/**
	 * 上传文件(字节流方式)
	 * @param file_path 文件路径
	 * @param file_ext_name 文件后缀
	 * @param meta_list 文件附属相关信息 (例如:图片的120、author:XXX等)
	 * @return url 文件url
	 */
	public String upload_file(byte file_buff[],String file_ext_name, NameValuePair[] meta_list) throws Exception;

	/**
	 * 上传文件-可修改(文件路径方式)
	 * @param file_path 文件路径
	 * @param file_ext_name 文件后缀
	 * @param meta_list 文件附属相关信息 (例如:图片的120、author:XXX等)
	 * @return url 文件url
	 */
	public String upload_appender_file(String file_path,String file_ext_name, NameValuePair[] meta_list) throws Exception;

	/**
	 * 上传文件-可修改(字节流方式)
	 * @param file_path 文件路径
	 * @param file_ext_name 文件后缀
	 * @param meta_list 文件附属相关信息 (例如:图片的120、author:XXX等)
	 * @return url 文件url
	 */
	public String upload_appender_file(byte file_buff[],String file_ext_name, NameValuePair[] meta_list) throws Exception;

	/**
	 * 修改可修改文件(appender上传)
	 * @param group_name
	 * @param remote_filename
	 * @param file_buff
	 * @return 0  修改成功
	 */
	public int modify_appender_file(String group_name, String remote_filename, byte file_buff[]) throws Exception;
	
	
	/**
	 * 下载文件
	 * @param group_name 文件组
	 * @param remote_filename 文件路径(上传文件时返回的值)
	 * @return 文件字节流
	 */
	public byte[] download_file(String remote_filename) throws Exception;
	
	/**
	 * 文件删除
	 * @param group_name 文件组
	 * @param remote_filename 文件路径
	 * @return 0 删除成功
	 */
	public int delete_file(String remote_filename) throws Exception;
	
	/**
	 * 文件信息
	 * @return FileInfo 文件存储信息
	 */
	public FileInfo get_file_info(String remote_filename) throws Exception;

	/**
	 * 文件附属相关信息
	 * @return NameValuePair 数组
	 */
	public NameValuePair[] get_metadata(String remote_filename) throws Exception;
	
}

  

实现类

package com.bsh.common.service.impl;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;

import com.bsh.common.service.FdfsFileService;



/**
 * FastDFS文件存储服务实现
 * 
 * 
 */
public class FdfsFileServiceImpl implements FdfsFileService {

	private static FdfsFileServiceImpl service;
	private String conf_file_path = "fdfs.conf";
	private static final String GROUP = "allies";

	private FdfsFileServiceImpl(){}
	
	/**
	 * Spring初始化服务
	 */
	public void init() {
		service = this;
		service.conf_file_path = this.conf_file_path;
		try {
              //配置文件路径 String filePath = FdfsFileService.class.getResource("/").toURI().getPath() + service.conf_file_path; ClientGlobal.init(filePath); } catch (Exception e) { System.err.println("FastDFS初始化失败"); e.printStackTrace(); } } /** * 初始化服务 */ public static FdfsFileService getInstance(String conf_file_path) { if (service == null) { service = new FdfsFileServiceImpl(); } service.conf_file_path = conf_file_path; try { ClientGlobal.init(FdfsFileService.class.getResource("/").getPath() + service.conf_file_path); } catch (Exception e) { System.err.println("FastDFS初始化失败"); e.printStackTrace(); } return service; } /** * 获得客服端连接 * * @return * @throws Exception */ private StorageClient getClient() throws Exception { return new StorageClient(new TrackerClient().getConnection(), null); } @Override public String upload_file(String file_path, String file_ext_name, NameValuePair[] meta_list) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } String[] fileIds = null; try { fileIds = client.upload_file(file_path, file_ext_name, meta_list); } catch (Exception e) { throw new Exception("FastDFS文件上传失败", e); } return fileIds[1]; } @Override public String upload_file(byte file_buff[], String file_ext_name, NameValuePair[] meta_list) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } String[] fileIds = null; try { fileIds = client.upload_appender_file(file_buff, file_ext_name, meta_list); } catch (Exception e) { throw new Exception("FastDFS文件上传失败", e); } return fileIds[1]; } @Override public byte[] download_file(String remote_filename) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } return client.download_file(GROUP, remote_filename); } @Override public int delete_file(String remote_filename) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } return client.delete_file(GROUP, remote_filename); } @Override public FileInfo get_file_info(String remote_filename) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } return client.get_file_info(GROUP, remote_filename); } @Override public NameValuePair[] get_metadata(String remote_filename) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } return client.get_metadata(GROUP, remote_filename); } public void setConf_file_path(String conf_file_path) { this.conf_file_path = conf_file_path; } @Override public String upload_appender_file(String file_path, String file_ext_name, NameValuePair[] meta_list) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } String[] fileIds = null; try { fileIds = client.upload_appender_file(file_path, file_ext_name, meta_list); } catch (Exception e) { throw new Exception("FastDFS文件上传失败", e); } return fileIds[1]; } @Override public String upload_appender_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } String[] fileIds = null; try { fileIds = client.upload_appender_file(file_buff, file_ext_name, meta_list); } catch (Exception e) { throw new Exception("FastDFS文件上传失败", e); } return fileIds[1]; } @Override public int modify_appender_file(String group_name, String appdender_filename, byte[] file_buff) throws Exception { StorageClient client = null; try { client = service.getClient(); } catch (Exception e) { throw new Exception("FastDFS获取连接失败", e); } return client.modify_file(group_name, appdender_filename, 0, file_buff); } }

  

调用工具类

package com.bsh.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.bsh.common.service.FdfsFileService;
import cn.osworks.aos.core.asset.AOSPropertiesHandler;

/**
 * 文件上传工具
 *
 */
@Component
@Lazy(false)
public class UploadUtils {
	
	static FdfsFileService fdfsFileService;
	
	public static FdfsFileService getFdfsFileService() {
		return fdfsFileService;
	}
	
	@Autowired
	public void setFdfsFileService(FdfsFileService fdfs) {
		fdfsFileService = fdfs;
	}
	


	//dfs新方法
	public static String save(MultipartFile file) {
		String picUrl = "";
		String[] extNameStr=file.getOriginalFilename().split("\.");
		String extName=extNameStr[1];
		if (!file.isEmpty()) {
			try {
				picUrl = fdfsFileService.upload_file(file.getBytes(), extName, null);
                   //返回地址加上图片服务器域名 picUrl=AOSPropertiesHandler.getProperty("pic_server")+"/"+picUrl.split("/")[3]; } catch (Exception e) { e.printStackTrace(); } } else { return picUrl; } return picUrl; } }

  

原文地址:https://www.cnblogs.com/kms1989/p/6002802.html