當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
用 Spring Boot 纯手工打造私人云网盘!!!
生活随笔
收集整理的這篇文章主要介紹了
用 Spring Boot 纯手工打造私人云网盘!!!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
最近在做工作流的事情,正好有個需求,要添加一個附件上傳的功能,曾找過不少上傳插件,都不是特別滿意。無意中發現一個很好用的開源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=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 初始化 基礎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 控制層實現:
@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);}}//省略部分代碼 }最后,前端頁面引入:
// ... 省略,具體看源碼。微信文章限制長度。小結
總體來說個人使用還是非常不錯的,當然對于一些成熟的網盤系統還是有一些差距。
源碼:
https://gitee.com/52itstyle/spring-boot-CloudDisk
總結
以上是生活随笔為你收集整理的用 Spring Boot 纯手工打造私人云网盘!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB 4.2 正式发布,支持分
- 下一篇: 一个30岁男人转型码农的平凡之路