javascript
用 Spring Boot 纯手工打造私人云网盘!!!
前言
最近在做工作流的事情,正好有個(gè)需求,要添加一個(gè)附件上傳的功能,曾找過(guò)不少上傳插件,都不是特別滿意。無(wú)意中發(fā)現(xiàn)一個(gè)很好用的開(kāi)源web文件管理器插件 elfinder,功能比較完善,社區(qū)也很活躍,還方便二次開(kāi)發(fā)。
環(huán)境搭建
| SpringBoot | https://spring.io/projects/spring-boot/ |
| elFinder | https://studio-42.github.io/elFinder/ |
項(xiàng)目截圖
周末抽時(shí)間做了一個(gè)簡(jiǎn)單的案例,希望對(duì)大家有所幫助,下面是簡(jiǎn)單的項(xiàng)目截圖。
項(xiàng)目功能
在線新建目錄、文件、附件上傳、下載、預(yù)覽、在線打包,圖片在線裁剪、編輯,實(shí)現(xiàn)列表試圖、圖標(biāo)視圖等等一些列功能。
項(xiàng)目配置
項(xiàng)目基于 SpringBoot 注解配置實(shí)現(xiàn),在第三方插件進(jìn)行二次開(kāi)發(fā)。
application.properties 配置:
#?執(zhí)行類,內(nèi)部調(diào)用,實(shí)現(xiàn)前端相關(guān)功能 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=trueElfinderConfiguration 讀取配置:
@Component @ConfigurationProperties(prefix="file-manager")?//接收application.properties中的file-manager下面的屬性 public?class?ElfinderConfiguration?{private?Thumbnail?thumbnail;private?String?command;private?List<Node>?volumes;private?Long?maxUploadSize?=?-1L;//省略部分代碼}elfinderStorageFactory 初始化 基礎(chǔ)Bean:
@Configuration public?class?ElFinderConfig?{@Autowiredprivate?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?thumbnailDefaultThumbnailWidth?defaultThumbnailWidth?=?new?DefaultThumbnailWidth();defaultThumbnailWidth.setThumbnailWidth(elfinderConfiguration.getThumbnail().getWidth().intValue());//?creates?volumes,?volumeIds,?volumeLocale?and?volumeSecuritiesCharacter?defaultVolumeId?=?'A';List<Node>?elfinderConfigurationVolumes?=?elfinderConfiguration.getVolumes();List<Volume>?elfinderVolumes?=?new?ArrayList<>(elfinderConfigurationVolumes.size());Map<Volume,?String>?elfinderVolumeIds?=?new?HashMap<>(elfinderConfigurationVolumes.size());Map<Volume,?Locale>?elfinderVolumeLocales?=?new?HashMap<>(elfinderConfigurationVolumes.size());List<VolumeSecurity>?elfinderVolumeSecurities?=?new?ArrayList<>();//?creates?volumesfor?(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?volumeVolume?volume?=?VolumeSources.of(source).newInstance(alias,?path);elfinderVolumes.add(volume);elfinderVolumeIds.put(volume,?Character.toString(defaultVolumeId));elfinderVolumeLocales.put(volume,?LocaleUtils.toLocale(locale));//?creates?security?constraintSecurityConstraint?securityConstraint?=?new?SecurityConstraint();securityConstraint.setLocked(isLocked);securityConstraint.setReadable(isReadable);securityConstraint.setWritable(isWritable);//?creates?volume?pattern?and?volume?securityfinal?String?volumePattern?=?Character.toString(defaultVolumeId)?+?ElFinderConstants.ELFINDER_VOLUME_SERCURITY_REGEX;elfinderVolumeSecurities.add(new?DefaultVolumeSecurity(volumePattern,?securityConstraint));//?prepare?next?volumeId?characterdefaultVolumeId++;}defaultElfinderStorage.setThumbnailWidth(defaultThumbnailWidth);defaultElfinderStorage.setVolumes(elfinderVolumes);defaultElfinderStorage.setVolumeIds(elfinderVolumeIds);defaultElfinderStorage.setVolumeLocales(elfinderVolumeLocales);defaultElfinderStorage.setVolumeSecurities(elfinderVolumeSecurities);return?defaultElfinderStorage;} }CloudDiskController 控制層實(shí)現(xiàn):
@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;@RequestMappingpublic?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()?{@Overridepublic?ElfinderStorageFactory?getVolumeSourceFactory()?{return?elfinderStorageFactory;}@Overridepublic?HttpServletRequest?getRequest()?{return?protectedRequest;}@Overridepublic?HttpServletResponse?getResponse()?{return?response;}});}catch?(Exception?e)?{logger.error("Unknown?error",?e);}}//省略部分代碼 }最后,前端頁(yè)面引入:
// ... 省略,具體看源碼。微信文章限制長(zhǎng)度。小結(jié)
總體來(lái)說(shuō)個(gè)人使用還是非常不錯(cuò)的,當(dāng)然對(duì)于一些成熟的網(wǎng)盤系統(tǒng)還是有一些差距。
源碼:
https://gitee.com/52itstyle/spring-boot-CloudDisk
總結(jié)
以上是生活随笔為你收集整理的用 Spring Boot 纯手工打造私人云网盘!!!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MongoDB 4.2 正式发布,支持分
- 下一篇: 厉害了,教你用 Spring Boot