解析后端返回html文件格式 获取html里的meta值

const parser = new DOMParser();
      const doc = parser.parseFromString('后端返回的html结构', 'text/html');
const grv_bearer_token=
 doc.querySelector('meta[name="grv_bearer_token"]')?.getAttribute('content');
       console.log(grv_bearer_token); 得到的值
      if (grv_bearer_token) {
        const decoded = window.atob(grv_bearer_token); atob解码base64格式编码
        const json = JSON.parse(decoded);
      
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的问题,需要涉及到前后端的交互,以及HDFS文件系统的操作。下面是简单的代码示例,供参考。 前端代码: ``` <!DOCTYPE html> <html> <head> <title>HDFS文件管理</title> <meta charset="UTF-8"> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <style> .file { display: inline-block; margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; text-align: center; cursor: pointer; } .file .name { margin-top: 5px; font-weight: bold; } .file .size { margin-top: 5px; font-size: 12px; color: #999; } .file .icon { font-size: 48px; color: #999; } .file .icon.icon-folder { color: #ffa500; } .file .icon.icon-file { color: #4169e1; } </style> </head> <body> <div> <button id="btn-prev" onclick="prev()">返回上一级</button> <button id="btn-upload" onclick="upload()">上传文件</button> </div> <div id="list"></div> <script> var path = "/"; $(function() { loadList(path); }); function loadList(path) { $.get("/list", {path: path}, function(result) { var files = result.files; var html = ""; for (var i = 0; i < files.length; i++) { var file = files[i]; var icon = file.type == "directory" ? "icon-folder" : "icon-file"; html += '<div class="file" onclick="clickFile(\'' + file.path + '\')">'; html += '<div class="icon ' + icon + '">&#x' + icon.charCodeAt(0).toString(16) + ';</div>'; html += '<div class="name">' + file.name + '</div>'; html += '<div class="size">' + file.size + '</div>'; html += '</div>'; } $("#list").html(html); }); } function clickFile(path) { if (path.endsWith("/")) { loadList(path); } else { download(path); } } function prev() { var index = path.lastIndexOf("/", path.length - 2); path = index >= 0 ? path.substring(0, index + 1) : "/"; loadList(path); } function upload() { var form = $('<form method="post" enctype="multipart/form-data"><input type="file" name="file"/></form>'); form.find('input').on('change', function() { var file = this.files[0]; var formData = new FormData(); formData.append('file', file); formData.append('path', path); $.ajax({ url: '/upload', type: 'POST', data: formData, processData: false, contentType: false, success: function(result) { loadList(path); } }); }).click(); } function download(path) { window.location.href = "/download?path=" + encodeURIComponent(path); } </script> </body> </html> ``` 后端代码: ``` @Controller public class HdfsController { private static final Logger logger = LoggerFactory.getLogger(HdfsController.class); private static final String HDFS_URI = "hdfs://localhost:9000"; private static final String HDFS_USER = "hadoop"; private FileSystem fs; @PostConstruct public void init() throws IOException, InterruptedException { Configuration conf = new Configuration(); conf.set("fs.defaultFS", HDFS_URI); conf.set("hadoop.user.name", HDFS_USER); fs = FileSystem.get(conf); } @GetMapping("/list") @ResponseBody public Map<String, Object> list(@RequestParam("path") String path) throws IOException { Path p = new Path(path); FileStatus[] files = fs.listStatus(p); List<Map<String, Object>> fileList = new ArrayList<>(); for (FileStatus file : files) { Map<String, Object> fileInfo = new HashMap<>(); fileInfo.put("name", file.getPath().getName()); fileInfo.put("path", file.getPath().toString()); fileInfo.put("type", file.isDirectory() ? "directory" : "file"); fileInfo.put("size", file.isDirectory() ? "-" : formatSize(file.getLen())); fileList.add(fileInfo); } Map<String, Object> result = new HashMap<>(); result.put("files", fileList); return result; } @PostMapping("/upload") @ResponseBody public Map<String, Object> upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws IOException { Path p = new Path(path, file.getOriginalFilename()); FSDataOutputStream out = fs.create(p); IOUtils.copy(file.getInputStream(), out); out.close(); Map<String, Object> result = new HashMap<>(); result.put("success", true); return result; } @GetMapping("/download") public void download(HttpServletResponse response, @RequestParam("path") String path) throws IOException { Path p = new Path(path); FSDataInputStream in = fs.open(p); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + p.getName() + "\""); IOUtils.copy(in, response.getOutputStream()); in.close(); response.flushBuffer(); } private String formatSize(long size) { String[] units = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; int i = 0; double s = size; while (s >= 1024 && i < units.length - 1) { s /= 1024; i++; } return String.format("%.2f %s", s, units[i]); } } ``` 需要注意的是,以上代码只是示例,实际使用时需要根据具体情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值