SpringBoot开发案例之打造私有云网盘的实现

网友投稿 401 2023-01-08


SpringBoot开发案例之打造私有云网盘的实现

前言

最近在做工作流的事情,正好有个需求,要添加一个附件上传的功能,曾找过不少上传插件,都不是特别满意。无意中发现一个很好用的开源web文件管理器插件 elfinder,功能比较完善,社区也很活跃,还方便二次开发。

环境搭建

软件

地址

SpringBoot

https://spring.io/projects/spring-boot/

elFinder

https://studio-42.github.io/elFinder/

项目截图

周末抽时间做了一个简单的案例,希望对大家有所帮助,下面是简单的项目截图。

项目功能

在线新建目录、文件、附件上传、下载、预览、在线打包,图片在线裁剪、编辑,实现列表试图、图标视图等等一些列功能。

项目配置

项目在第三方插件进行二次开发,基于 SpringBoot 注解配置实现。

application.properties 配置:

# 执行类,内部调用,实现前端相关功能

file-manager.command=com.itstyle.cloud.common.elfinder.command

file-manager.thumbnail.width=80

file-manager.volumes[0].Node=

file-manager.volumes[0].source=fileSystem

file-manager.volumes[0].alias=file

# 文件存放目录,可以自定义

file-manager.volumes[0].path=D:/cloudFile

file-manager.volumes[0]._default=true

file-manager.volumes[0].locale=

file-manager.volumes[0].constraint.locked=false

file-manager.volumes[0].constraint.readable=true

file-manager.volumes[0].constraint.writable=true

ElfinderConfiguration 读取配置:

@Component

@ConfigurationProperties(prefix="file-manager") //接收application.properties中的file-manager下面的属性

public class ElfinderConfiguration {

private Thumbnail thumbnail;

private String command;

private List volumes;

private Long maxUploadSize = -1L;

//省略部分代码

}

elfinderStorageFactory 初始化 基础Bean:

@Configuration

public class ElFinderConfig {

@Autowired

private ElfinderConfiguration elfinderConfiguration;

@Bean(name = "commandFactory")

public CommandFactory getCommandFactory() {

CommandFactory commandFactory = new CommandFactory();

commandFactory.setClassNamePattern(elfinderConfiguration.getCommand()+".%sCommand");

return commandFactory;

}

@Bean(name = "elfinderStorageFactory")

public ElfinderStorageFactory getElfinderStorageFactory() {

DefaultElfinderStorageFactory elfinderStorageFactory = new DefaultElfinderStorageFactory();

elfinderStorageFactory.setElfinderStorage(getElfinderStorage());

return elfinderStorageFactory;

}

@Bean(name = "elfinderStorage")

public ElfinderStorage getElfinderStorage() {

DefaultElfinderStorage defaultElfinderStorage = new DefaultElfinderStorage();

// creates thumbnail

DefaultThumbnailWidth defaultThumbnailWidth = new DefaultThumbnailWidth();

defaultThumbnailWidth.setThumbnailWidth(elfinderConfiguration.getThumbnail().getWidth().intValue());

// creates volumes, volumeIds, volumeLocale and volumeSecurities

Character defaultVolumeId = 'A';

List elfinderConfigurationVolumes = elfinderConfiguration.getVolumes();

List elfinderVolumes = new ArrayList<>(elfinderConfigurationVolumes.size());

Map elfinderVolumeIds = new HashMap<>(elfinderConfigurationVolumes.size());

Map elfinderVolumeLocales = new HashMap<>(elfinderConfigurationVolumes.size());

List elfinderVolumeSecurities = new ArrayList<>();

// creates volumes

for (Node elfinderConfigurationVolume : elfinderConfigurationVolumes) {

final String alias = elfinderConfigurationVolume.getAlias();

final String path = elfinderConfigurationVolume.getPath();

final String source = elfinderConfigurationVolume.getSource();

final String locale = elfinderConfigurationVolume.getLocale();

final boolean isLocked = elfinderConfigurationVolume.getConstraint().isLocked();

final boolean isReadable = elfinderConfigurationVolume.getConstraint().isReadable();

final boolean isWritable = elfinderConfigurationVolume.getConstraint().isWritable();

// creates new volume

Volume volume = VolumeSources.of(source).newInstance(alias, path);

elfinderVolumes.add(volume);

elfinderVolumeIds.put(volume, Character.toString(defaultVolumeId));

elfinderVolumeLocales.put(volume, LocaleUtils.toLocale(locale));

// creates security constraint

SecurityConstraint securityConstraint = new SecurityConstraint();

securityConstraint.setLocked(isLocked);

securityConstraint.setReadable(isReadable);

securityConstraint.setWritable(isWritable);

// creates volume pattern and volume security

final String volumePattern = Character.toString(defaultVolumeId) + ElFinderConstants.ELFINDER_VOLUME_SERCURITY_REGEX;

elfinderVolumeSecurities.add(new DefaultVolumeSecurity(volumePattern, securityConstraint));

// prepare next volumeId character

defaultVolumeId++;

}

defaultElfinderStorage.setThumbnailWidth(defaultThumbnailWidtbUGuWmmbefh);

defaultElfinderStorage.setVolumes(elfinderVolumes);

defaultElfinderStorage.setVolumeIds(elfinderVolumeIds);

defaultElfinderStorage.setVolumeLocales(elfinderVolumeLocales);

defaultElfinderStorage.setVolumeSecurities(elfinderVolumeSecurities);

return defaultElfinderStorage;

}

}

CloudDiskController 控制层实现:

@Controller

@RequestMapping("elfinder/connector")

public class CloudDiskController {

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

public static final String OPEN_STREAM = "openStream";

public static final String GET_PARAMETER = "getParameter";

@Resource(name = "commandFactory")

private ElfinderCommandFactory elfinderCommandFactory;

@Resource(name = "elfinderStorageFactory")

private ElfinderStorageFactory elfinderStorageFactory;

@RequestMapping

public void connector(HttpServletRequest request, final HttpServletResponse response) throws IOException {

try {

response.setCharacterEncoding("UTF-8");

request = processMultipartContent(request);

} catch (Exception e) {

throw new IOException(e.getMessage());

}

String cmd = request.getParameter(ElFinderConstants.ELFINDER_PARAMETER_COMMAND);

ElfinderCommand elfinderCommand = elfinderCommandFactory.get(cmd);

try {

final HttpServletRequest protectedRequest = request;

elfinderCommand.execute(new ElfinderContext() {

@Override

public ElfinderStorageFactory getVolumeSourceFactory() {

return elfinderStorageFactory;

}

@Override

public HttpServletRequest getRequest() {

return protectedRequest;

}

@Override

public HttpServletResponse getResponse() {

return response;

}

});

} catch (Exception e) {

logger.error("Unknown error", e);

}

}

//省略部分代码

}

最后,前端页面引入:

小结

总体来说个人使用还是非常不错的,当然对于一些成熟的网盘系统还是有一些差距。

源码:https://gitee.com/52itstyle/spring-boot-CloudDisk


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自动化接口测试注意事项(自动化接口测试注意事项包括)
下一篇:微服务网关一个还是多个(什么是微服务网关)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~