java6 -- fastFdfs在java高并发下中的应用(图片上传),上线测试无误
@Component
public class ImageToFdfsFileStorageClientUtil extends DefaultFastFileStorageClient {
public StorePath uploadFile(FastFile fastFile, int storePath) {
Validate.notNull(fastFile.getInputStream(), "上传文件流不能为空", new Object[0]);
Validate.notBlank(fastFile.getFileExtName(), "文件扩展名不能为空", new Object[0]);
StorageNode client = this.getStorageNode(fastFile.getGroupName());
return this.uploadFileAndMetaData(storePath, client, fastFile.getInputStream(), fastFile.getFileSize(), fastFile.getFileExtName(), fastFile.getMetaDataSet());
}
private StorageNode getStorageNode(String groupName) {
return null == groupName ? this.trackerClient.getStoreStorage() : this.trackerClient.getStoreStorage(groupName);
}
public StorePath uploadFileAndMetaData(int storePath, StorageNode client, InputStream inputStream, long fileSize, String fileExtName, Set<MetaData> metaDataSet) {
client.setStoreIndex((byte) (storePath - 256));
StorageUploadFileCommand command = new StorageUploadFileCommand(client.getStoreIndex(), inputStream, fileExtName, fileSize, false);
StorePath path = (StorePath)this.connectionManager.executeFdfsCmd(client.getInetSocketAddress(), command);
if (this.hasMetaData(metaDataSet)) {
StorageSetMetadataCommand setMDCommand = new StorageSetMetadataCommand(path.getGroup(), path.getPath(), metaDataSet, StorageMetadataSetType.STORAGE_SET_METADATA_FLAG_OVERWRITE);
this.connectionManager.executeFdfsCmd(client.getInetSocketAddress(), setMDCommand);
}
return path;
}
private boolean hasMetaData(Set<MetaData> metaDataSet) {
return null != metaDataSet && !metaDataSet.isEmpty();
}
}