使用SpringBoot怎么实现私有云网盘
这期内容当中小编将会给大家带来有关使用SpringBoot怎么实现私有云网盘,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为红山企业提供专业的做网站、网站制作,红山网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
项目配置
项目在第三方插件进行二次开发,基于 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 Listvolumes; 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'; ListelfinderConfigurationVolumes = 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(defaultThumbnailWidth); 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); } } //省略部分代码 }
最后,前端页面引入:
上述就是小编为大家分享的使用SpringBoot怎么实现私有云网盘了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
分享标题:使用SpringBoot怎么实现私有云网盘
URL链接:http://myzitong.com/article/ghoocs.html