package cn.autoform.web.controller.resumable.util; import jxl.read.biff.BiffException; import lombok.extern.slf4j.Slf4j; import org.csource.common.MyException; import org.csource.fastdfs.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.core.env.StandardEnvironment; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Objects; import java.util.Properties; import java.util.Random; /** * * @ClassName: FastDFSClient * @Description: fdfs工具类 * @author: sunqz * @date: 2017-6-3 下午3:17:46 */ @Service @Slf4j public class FastDFSClient implements EnvironmentAware { private StorageClient1 storageClient1; private Environment environment; @Value("${fast.group_name}") private String groupName; /** * 只加载一次. */ @PostConstruct public void init() { try { ClientGlobal.initByProperties(new Properties(){ @Override public String getProperty(String key) { return environment.getProperty(key); } @Override public String getProperty(String key, String defaultValue) { return environment.getProperty(key, defaultValue); } }); TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group); TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) { log.error("getConnection return null"); } StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); if (storageServer == null) { log.error("getStoreStorage return null"); } storageClient1 = new StorageClient1(trackerServer, storageServer); } catch (Exception e) { log.error(e.getMessage(), e); } } /** * * @Title: uploadFile * @Description: 上传文件 * @return * @return: String 文件id */ public String uploadFile(byte[] bytes, String fileName) { try { log.debug("上传文件byte大小:{}, 文件名:{}", bytes.length, fileName); String fileId = storageClient1.upload_file1(groupName, bytes, fileName.substring(fileName.lastIndexOf('.')+1), null); log.debug("上传后地址:{}", fileId); return fileId; } catch (IOException|MyException e) { log.error(e.getMessage(), e); return ""; } } public InputStream downloadFile(String fileId) { try { return new ByteArrayInputStream( storageClient1.download_file1(fileId)); } catch (Exception e) { log.error(e.getMessage(), e); return null; } } @Override public void setEnvironment(Environment environment) { this.environment = environment; } public static void main(String... args) throws IOException, BiffException { FastDFSClient f = new FastDFSClient(); f.setEnvironment(new StandardEnvironment() { @Override public String getProperty(String key) { if(Objects.equals(key, "fastdfs.tracker_servers")){ return "10.4.32.37:22122"; } return null; } }); f.init(); byte[] b = new byte[4993]; for (int i = 0; i < 4993; i++) { b[i] = (byte) new Random().nextInt(100); } String url = f.uploadFile(b, "xlsx"); InputStream is = f.downloadFile(url); // Workbook rwb = Workbook.getWorkbook(is); System.out.println(is); } }