首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >大数据接私活200元,做个简易的HDFS浏览器(一)

大数据接私活200元,做个简易的HDFS浏览器(一)

作者头像
Maynor
发布2022-06-30 14:58:09
3770
发布2022-06-30 14:58:09
举报

大数据接私活200元,做个简易的HDFS浏览器(一)

需求

接到一个单子说是用制作一个简单的HDFS浏览器。

功能包括:基于HDFS的文件浏览、上传和下载。

请添加图片描述
请添加图片描述

需求分析

​ 用到的技术包括Java、HDFSAPI、tomcat的配置等

代码实现

项目架构:

请添加图片描述
请添加图片描述

Controller层代码:

@Controller
@RequestMapping("/hdfs")
public class hdfsController {
   
   
   
   private hdfsService hdfsService;   
   public hdfsService getHdfsService() {
      return hdfsService;
   }
    @Resource
   public void setHdfsService(hdfsService hdfsService) {
      this.hdfsService = hdfsService;
   }

   @RequestMapping("/delete")  
    public String toDelete(@RequestParam(value="path",required=false) String path)
       throws IOException{  
        hdfsService.delete(path);            
          return "success";       
}


    @RequestMapping(value="/ls",method=RequestMethod.GET)
       public ModelAndView home(@RequestParam(value="path",required=false) String path, HttpServletRequest request, HttpServletResponse response) 
          throws Exception {
          ModelAndView model = new ModelAndView();
           if (StringUtils.isEmpty(path)) {
               path = "/";
           } 
         List<hdfsBean> hdfsFiles =hdfsService.ls(path);
           model.addObject("file", hdfsFiles);
           model.setViewName("/ls");
           return model;
       }
    
    
    @RequestMapping("/download")  
       public String toDownload(@RequestParam(value="path",required=false) String path)
          throws IOException{  
          hdfsService.download(path);           
             return "success";       
   }
    
    
    @RequestMapping(value="/upload")
    public String upLoad(HttpServletRequest request, HttpServletResponse response) 
               throws IllegalStateException, IOException{
           //解析器解析request的上下文
           CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); 
           //先判断request中是否包涵multipart类型的数据,
           if(multipartResolver.isMultipart(request)) {
               //再将request中的数据转化成multipart类型的数据
               MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
               Iterator<String> iter = multiRequest.getFileNames();
               while(iter.hasNext()) {
                   MultipartFile file = multiRequest.getFile(iter.next());
                   if(file != null) {
                      String FileName = file.getOriginalFilename();
                      System.out.println(FileName);
                       CommonsMultipartFile cf= (CommonsMultipartFile)file; 
                       DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
                       File inputFile = fi.getStoreLocation();
                       hdfsService.createFile(inputFile, "hdfs://192.168.88.100:8020/upload/"+FileName);
                   }
               }
           }
           return "success";
       }

前端部分页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %> 
<%String ref = request.getHeader("REFERER");%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HDFS文件管理器</title>
</head>
<body>
<table>
   <tr>
      <td>文件名</td>
      <td>文件大小  </td>
      <td>拥有者 </td>
      <td>权限  </td>
      <td>时间  </td>
      <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作</td>
   </tr>
     <c:forEach var="file" items="${file}">
     <tr>
          <td><a href="${pageContext.request.contextPath}/hdfs/ls.do?path=${file.fileName}" style="color:#666666;text-decoration:none;">
               ${file.fileName}</a></td>
          <td> ${file.fileSize}</td>    
          <td> ${file.owner}</td>   
          <td> ${file.permission}</td>  
          <td> ${file.modificationTime}</td>    
          
          <td><a href="${pageContext.request.contextPath}/hdfs/delete.do?path=${file.fileName}" style="color:#666666;text-decoration:none;"
           class="button border-dot button-little" onclick="return confirm('确认删除?')" >
                            删除  </a>
          &nbsp;&nbsp;
          <a href="${pageContext.request.contextPath}/hdfs/download.do?path=${file.fileName}" style="color:#666666;text-decoration:none;">
          下载</a></td>
    </tr>
   </c:forEach>
 <input type="button" value="返回"   onclick="javascript:window.location='<%=ref%>'" >
</table>
</body>
</html>

效果演示

HDFS简易Web浏览器

HDFS简易Web浏览器_哔哩哔哩_bilibili

总结

比较轻松的需求,主要是之前做过类似的小项目直接拿来用即可。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-06-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 大数据接私活200元,做个简易的HDFS浏览器(一)
    • 需求
      • 需求分析
        • 代码实现
          • 效果演示
          • 总结
          相关产品与服务
          大数据
          全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档