FastDFSClientWrapper.java 4.11 KB
package com.glsx.publicservice.util.fastdfs;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.glsx.common.utils.PropertiesUtil;
import com.glsx.common.utils.StringUtils;


/**
 * 
 * @Title FastDFSClientWrapper.java
 * @Package com.qurong.credit.sp.utils.fastdfs
 * @Description TODO fastdfs文件上传
 * @author zhanghj
 * @date 2018年7月27日 下午2:52:05
 * @version
 */
@Component
public class FastDFSClientWrapper {

	private static Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);

	private static FastFileStorageClient storageClient;
	
//	private static DefaultTrackerClient trackserClient;

//	private static final String GENERAL_NGINX_PORT = "80";
	private static final String FDFS_TRACKER_NGINX_DOMAIN = "fdfs_tracker_nginx_domain";

	public static final String PROTOCOL = "http://";
	public static final String SEPARATOR = "/";
	

	/**
	 * 上传文件
	 * 
	 * @param file
	 *            文件对象
	 * @return 文件访问地址
	 * @throws IOException
	 */
	public static String uploadFile(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return getResAccessUrl(storePath);
	}

	/**
	 * 将一段字符串生成一个文件上传
	 * 
	 * @param content
	 *            文件内容
	 * @param fileExtension
	 * @return
	 */
	public static String uploadFile(String content, String fileExtension) {
		byte[] buff = content.getBytes(Charset.forName("UTF-8"));
		ByteArrayInputStream stream = new ByteArrayInputStream(buff);
		StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
		return getResAccessUrl(storePath);
	}
	
	/**
	 * 将byte数组生成一个文件上传
	 * 
	 * @param bytes 文件内容字节数组
	 * @param fileExtension
	 * @return
	 */
	public static String uploadFile(byte[] bytes, String fileExtension) {
		ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
		StorePath storePath = storageClient.uploadFile(stream, bytes.length, fileExtension, null);
		return getResAccessUrl(storePath);
	}

	// 封装图片完整URL地址
	private static String getResAccessUrl(StorePath storePath) {
		/*String fdfsTrackerNginxPort = PropertiesUtil.getProperty("fdfs_tracker_nginx_port");
		String fileAbsolutePath = "";
		if (GENERAL_NGINX_PORT.equals(fdfsTrackerNginxPort)) {
			fileAbsolutePath = PROTOCOL + trackserClient.getStoreStorage().getIp() + SEPARATOR
					+ storePath.getFullPath();

		} else {
			fileAbsolutePath = PROTOCOL + trackserClient.getStoreStorage().getIp() + ":" + fdfsTrackerNginxPort
					+ SEPARATOR + storePath.getFullPath();
		}*/
		String domain = PropertiesUtil.getProperty(FDFS_TRACKER_NGINX_DOMAIN);
		return domain + SEPARATOR + storePath.getFullPath();
	}

	/**
	 * 删除文件
	 * 
	 * @param fileUrl
	 *            文件访问地址
	 * @return
	 */
	public static void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		try {
			StorePath storePath = StorePath.praseFromUrl(fileUrl);
			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (FdfsUnsupportStorePathException e) {
			logger.warn(e.getMessage());
		}
	}

	@Autowired
	public void setStorageClient(FastFileStorageClient storageClient) {
		FastDFSClientWrapper.storageClient = storageClient;
	}
	
	/*@Autowired
	public void setTrackserClient(DefaultTrackerClient trackserClient) {
		FastDFSClientWrapper.trackserClient = trackserClient;
	}*/
}