当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot在线代码修改器的问题及解决方法

SpringBoot在线代码修改器的问题及解决方法

2020年06月23日  | 移动技术网IT编程  | 我要评论

前言

项目上线之后,如果是后端报错,只能重新编译打包部署然后重启;如果仅仅是前端页面、样式、脚本修改,只需要替换到就可以了。

小公司的话可能比较自由,可以随意替换,但是有些公司权限设置的比较严格,需要提交申请交给运维去处理。

如果仅仅是一个前端问题,又很紧急,这时候提申请走流程势必会影响到用户的正常使用。

今天,撸主给大家推荐一款前端代码文件编辑器来解决以上问题。

案例

定义实体,用于前端文件树展示:

@data
public class sysfile {
 private integer fileid;
 private string name;
 private integer parentid;
 private string parentpath;
}

由于项目采用的是springboot框架,打成了war包部署,后端采用以下方式获取文件列表:

/**
 * 列表
 * @return
 */
@requestmapping(value = "list", method = requestmethod.post)
public result list() throws filenotfoundexception {
 string filepath = resourceutils.geturl("classpath:").getpath();
 list<sysfile> filelist = new arraylist<>();
 getallfilepaths(filepath,filelist,0,"");
 return result.ok(filelist);
}

递归获取某目录下的所有子目录以及子文件:

/**
 * 递归获取某目录下的所有子目录以及子文件
 * @param filepath
 * @param filepathlist
 * @return
 */
private static list<sysfile> getallfilepaths(string filepath, list<sysfile> filepathlist,
   integer level,string parentpath) {
 file[] files = new file(filepath).listfiles();
 if (files == null) {
 return filepathlist;
 }
 for (file file : files) {
 int num = filepathlist.size()+1;
 sysfile sysfile = new sysfile();
 sysfile.setname(file.getname());
 sysfile.setfileid(num);
 sysfile.setparentid(level);
 if (file.isdirectory()) {
 if(level==0){
 if(file.getname().equals("templates")||
  file.getname().equals("static")){
  filepathlist.add(sysfile);
  parentpath = systemconstant.sf_file_separator+file.getname();
  getallfilepaths(file.getabsolutepath(), filepathlist,num,parentpath);
  num++;
 }
 }else{
 filepathlist.add(sysfile);
 string subparentpath = parentpath+systemconstant.sf_file_separator+file.getname();
 getallfilepaths(file.getabsolutepath(), filepathlist,num,subparentpath);
 num++;
 }
 } else {
 if(level!=0){
 sysfile.setparentpath(parentpath+systemconstant.sf_file_separator+file.getname());
 filepathlist.add(sysfile);
 num++;
 }
 }
 }
 return filepathlist;
}

获取文件内容:

/**
 * 获取内容
 * @return
 */
@requestmapping(value = "getcontent", method = requestmethod.post)
public result getcontent(string filepath) throws filenotfoundexception {
 string path = resourceutils.geturl("classpath:").getpath();
 string content = fileutil.readutf8string(path+filepath);
 return result.ok(content);
}

修改保存:

/**
 * 保存内容
 * @return
 */
@requestmapping(value = "save", method = requestmethod.post)
public result save(string filepath, string content) throws filenotfoundexception {
 string path = resourceutils.geturl("classpath:").getpath();
 /**
 * 生产环境自行解除
 */
 if(active.equals("prod")){
 return result.error("演示环境禁止插插插!!!");
 }else{
 file file = new file(path+filepath);
 long lastmodified = file.lastmodified();
 fileutil.writeutf8string(content,path+filepath);
 file.setlastmodified(lastmodified);
 return result.ok();
 }
}

当然了,如果代码修改比较多,也可以对文件进行上传覆盖操作。

截图

小结

如果身边恰好没有工具连接远程服务,亦或是自己没有服务器的权限,这款在线修改器,撸主觉得还是很方便的。但一定要控制好权限,防止普通人员乱修改,还有一定要做好安全日志记录。

源码

https://gitee.com/52itstyle/sptools

到此这篇关于springboot在线代码修改器的文章就介绍到这了,更多相关springboot在线代码修改器内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网