<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="org.apache.hadoop.fs.FileStatus"%>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎使用-bobo网盘title>
<link rel="stylesheet"
href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="icon"
href="https://cdn.jsdelivr.net/gh/wangyang-o/imgcdn/img/20200512002714.png"
type="image/x-icon">
<link
href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"
rel="stylesheet">
head>
<body class="container">
<%
FileStatus[] list = (FileStatus[]) request.getAttribute("documentList");
String name = (String) session.getAttribute("username");
String thisPath;
if (request.getAttribute("thisPath") == null) {
thisPath = name;
} else {
thisPath = (String) request.getAttribute("thisPath");
}
String result;//用户当前目录
if (list.length != 0) {
/*不知道怎么获取当前目录的路径,所以找到他一个子目录的父目录,前面一串路径都是一样的
比如hdfs://localhost:9000/user/wangyang/user/wy/dir1/
你点击了dir2,你就只需要得到hdfs://localhost:9000/user/wangyang/user/wy/+你点击的路径dir2
*/
result = list[0].getPath().getParent().toString().substring(40);
} else {
result = "";
}
%>
<nav class="navbar navbar-expand-lg navbar-light bg-light mt-5">
<a class="navbar-brand" href="#">bobo<i
class="fa fa-modx fa-spin m-1">i>网盘
a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<a class="nav-link col-3">当前路径:<%=thisPath%>a>
<form id="frm" class="form-inline my-2 my-lg-0"
action="MkdirServlet?thisPath=<%=thisPath%>" method="post">
<input class="form-control mr-sm-2 col-5" type="text"
placeholder="输入文件夹名称" name="mkdir">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">创建button>
form>
<form action="UploadServlet?thisPath=<%=thisPath%>" method="post"
enctype="multipart/form-data">
<div class="row">
<div class="col-9">
<input type="file" class="filestyle" data-badgeName="badge-danger"
data-badge="true" data-placeholder="未选择" data-text="上传文件"
name="myfile">
div>
<div class="col-3 ml-0 pl-0">
<button class="btn btn-secondary my-2 my-sm-0" type="submit">
<i class="fa fa-upload" aria-hidden="true">i>
button>
div>
div>
form>
nav>
<table class="table table-hover " style="text-align: center">
<thead class="thead-dark">
<tr>
<th scope="col">序号th>
<th scope="col">文件名th>
<th scope="col">属性th>
<th scope="col">大小th>
<th scope="col">可执行操作th>
tr>
thead>
<tbody>
<%
if (list != null) {
for (int i = 0; i < list.length; i++) {
%>
<tr>
<%
out.print("<th scope='row'>" + (i + 1) + "th>");
if (list[i].isDir())//DocumentServlet
{
out.print("<td><strong>" + list[i].getPath().getName() + "strong>td>");
} else {
out.print("<td>" + list[i].getPath().getName() + "td>");
}
%>
<td><%=(list[i].isDir() ? "<i class='fa fa-folder-open mr-1' aria-hidden='true'>i>目录"
: "<i class='fa fa-file-text-o mr-1' aria-hidden='true'>i>文件")%>td>
<td><%=(list[i].getLen())%>td>
<td>
<%
String cut=(String)request.getAttribute("cut");
if (list[i].isDir())//DocumentServlet
{
out.print("<a role='button' class='btn btn-outline-success' href='ShowChildDirServlet?filePath=" + result + "/"
+ list[i].getPath().getName() + "&cut=" + cut + "'><i class='fa fa-sign-in mr-1'>i>进入a>");
} else {
out.print("<a role='button' class='btn btn-outline-primary' href='DownloadServlet?result=" + result + "&fileName="
+ list[i].getPath().getName() + "'><i class='fa fa-download' aria-hidden='true'>i>下载a>");
}
%> <a role="button" class="btn btn-outline-danger"
href="DeleteServlet?thisPath=<%=thisPath%>&fileName=<%=list[i].getPath().getName()%>"><i
class="fa fa-trash-o" aria-hidden="true">i>a> <%
System.out.println("html:" + request.getAttribute("cut"));
if (cut == null||cut.equals("null")) {
out.print("<a role='button' class='btn btn-outline-secondary' href='MoveServlet?thisPath=" + thisPath + "&fileName="
+ list[i].getPath().getName() + "'><i class='fa fa-scissors'>i> a>");
} else if (!cut.equals("null")&&cut != null && list[i].isDir()) {
out.print("<a role='button' class='btn btn-outline-secondary' href='MoveServlet?pastePath=" + thisPath
+ "&pasteName=" + list[i].getPath().getName() + "&cut=" + request.getAttribute("cut") + "'>粘贴a>");
}
%>
td>
<%
}
}
if (list.length == 0) {
out.print("<tr><td colspan='5' style='text-align:center'>没有任何文件哦td><tr>");
}
%>
tbody>
table>
<div class="row">
<div class="col-3 offset-9">
<div class="row">
<a role="button" class="btn btn-outline-secondary col-6 mr-1"
href="javascript:history.go(-1);location.reload()"><i
class="fa fa-arrow-left" aria-hidden="true">i>返回上一级a> <a
class="btn btn-danger col-4" href="LogoutServlet" role="button">注销登录a>
div>
div>
div>
body>
<footer>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js">script>
<script
src="https://cdn.bootcdn.net/ajax/libs/bootstrap-filestyle/2.1.0/bootstrap-filestyle.js">script>
<script
src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js">script>
<script
src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js">script>
<script type="text/javascript" color="100,50,100" opacity='1'
zIndex="-2" count="99"
src="//cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js">script>
footer>
html>
上一篇文章其实已经贴了。再贴一次吧
package com.wy.pojo;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsDao {
static Configuration conf = new Configuration();
static String hdfsPath = "hdfs://localhost:9000/user/wangyang/user";
public static void init() {
try {
conf.set("fs.defaultFS", "hdfs://localhost:9000/user/wangyang/user");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
} catch (Exception e) {
System.out.println("你似乎没有运行hadoop哦!!");
e.printStackTrace();
}
}
// 根据用户和文件名判断文件是否存在
public static boolean FileIfExist(String user, String filename) throws IOException {
init();
String fileName = "/user/wangyang/user/" + user + "/" + filename;// 你的文件路径,没有就显示不存在
FileSystem fs = FileSystem.get(conf);
if (fs.exists(new Path(fileName))) {
System.out.println("文件存在");
return true;
} else {
System.out.println("文件不存在");
return false;
}
}
// 为每个用户创建一个目录
public static void mkPersonalDir(String username) throws IOException {
init();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
String upremote = "/user/wangyang/user/" + username;
Path a = new Path(upremote);
fs.mkdirs(a);
System.out.println("创建个人目录成功");
fs.close();
}
// 创建一个子目录
public static void mkChildDir(String filePath) throws IOException {
init();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
String upremote = "/user/wangyang/user/" + filePath;
Path a = new Path(upremote);
fs.mkdirs(a);
System.out.println("创建目录成功");
fs.close();
}
// 根据路径删除文件
public static void DeleteFile(String deletePath) throws IOException {
init();
// Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
String filePath = "/user/wangyang/user/" + deletePath;
fs.deleteOnExit(new Path(filePath));
fs.close();
}
// 遍历用户目录文件与目录
public static FileStatus[] ShowFiles(String username) throws IOException {
init();
String filePath = "/user/wangyang/user/" + username;
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FileStatus[] list = fs.listStatus(new Path(filePath));
fs.close();
return list;
}
// 遍历用户目录下的子文件夹
public static FileStatus[] ShowDirFiles(String filePath) throws IOException {
init();
String filePath1 = "/user/wangyang/user/" + filePath + "/";
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FileStatus[] list = fs.listStatus(new Path(filePath1));
// if (list != null) {
// for (FileStatus f : list) {
// System.out.printf("name %s,folder:%s,size:%d\n", f.getPath(), f.isDir(), f.getLen());
// }
// }
fs.close();
return list;
}
// 获取文件的输入流对象
public static InputStream down(String cloudPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取对象
init();
String filePath1 = "/user/wangyang/user/" + cloudPath + "/";
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FSDataInputStream in = fs.open(new Path(filePath1));
return in;
}
public static void upload(String fileName,InputStream in) throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{
init();
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FSDataOutputStream out = fs.create(new Path("/user/wangyang/user/" + fileName + "/"));
// FileInputStream in = new FileInputStream("H:/新建.txt");
IOUtils.copy(in, out);
fs.close();
}
// 移动或者重命名:path1原文件路径,path2粘贴路径
public static void ReName(String path1,String path2) throws IOException {
// 1获取对象
init();
String filePath1 = "/user/wangyang/user/" + path1 + "/";
String filePath2 = "/user/wangyang/user/" + path2 + "/";
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
boolean re = fs.rename(new Path(filePath1), new Path(filePath2));
System.out.println("rename:"+re);
}
}
DeleteServlet.java
package com.wy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.hadoop.fs.FileStatus;
import com.wy.pojo.HdfsDao;
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String fileName = request.getParameter("fileName");
//创建在当前路径
String a=(String) request.getParameter("thisPath");
String filePath1=a+"/"+ fileName;
System.out.println("删除文件:"+filePath1);
HdfsDao.DeleteFile(filePath1);
HttpSession session = request.getSession();
String username=(String)session.getAttribute("username");
if(a.equals(username)) {
FileStatus[] documentList = HdfsDao.ShowFiles(a);
request.setAttribute("documentList", documentList);
System.out.println("得到list数据"+documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else {
response.sendRedirect(request.getHeader("Referer"));
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
this.doGet(request, response);
}
}
DownloadServlet.java
package com.wy.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wy.pojo.HdfsDao;
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String result = request.getParameter("result");
String fileName = request.getParameter("fileName");
String filePath=result+"/"+fileName;
try {
InputStream in=HdfsDao.down(filePath);
byte[] b = new byte[in.available()];
in.read(b);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition","attachment; filename="+fileName+"");
//获取响应报文输出流对象
ServletOutputStream out =response.getOutputStream();
//输出
out.write(b);
out.flush();
out.close();
} catch (IOException | InterruptedException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
UploadServlet.java
package com.wy.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import org.apache.hadoop.fs.FileStatus;
import com.wy.pojo.HdfsDao;
@WebServlet("/UploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Part part = request.getPart("myfile");
// 文件名
String disposition = part.getHeader("Content-Disposition");
String fileName =disposition.substring(disposition.lastIndexOf("=")+2,disposition.length()-1);
// 路径
String a=(String) request.getParameter("thisPath");
String upPath=a+"/"+ fileName;
InputStream in = part.getInputStream();
System.out.println(disposition.substring(disposition.lastIndexOf("=")+2,disposition.length()-1));
try {
HdfsDao.upload(upPath, in);
System.out.println("上传成功");
HttpSession session = request.getSession();
String username=(String)session.getAttribute("username");
if(a.equals(username)) {
FileStatus[] documentList = HdfsDao.ShowFiles(a);
request.setAttribute("documentList", documentList);
System.out.println("得到list数据"+documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else {
response.sendRedirect(request.getHeader("Referer"));
}
} catch (IllegalArgumentException | IOException | InterruptedException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
MkDirServlet.java
package com.wy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.hadoop.fs.FileStatus;
import com.wy.pojo.HdfsDao;
@WebServlet("/MkdirServlet")
public class MkDirServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String fileName = request.getParameter("mkdir");
//创建在当前路径
String a=(String) request.getParameter("thisPath");
String filePath1=a+"/"+ fileName;
System.out.println("创建在当前路径:"+a);
HdfsDao.mkChildDir(filePath1);
HttpSession session = request.getSession();
String username=(String)session.getAttribute("username");
if(a.equals(username)) {
FileStatus[] documentList = HdfsDao.ShowFiles(a);
request.setAttribute("documentList", documentList);
System.out.println("得到list数据"+documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else {
response.sendRedirect(request.getHeader("Referer"));
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.wy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.hadoop.fs.FileStatus;
import com.wy.pojo.HdfsDao;
import com.wy.pojo.User;
import com.wy.pojo.UserDao;
@WebServlet("/MoveServlet")
public class MoveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String thisPath = request.getParameter("thisPath");
String fileName = request.getParameter("fileName");
String filePath1 = thisPath + "/" + fileName;
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
String pastePath = request.getParameter("pastePath");
String pasteName = request.getParameter("pasteName");
String filePath2 = pastePath + "/" + pasteName;
if (pastePath != null) {
String cut = request.getParameter("cut");
HdfsDao.ReName(cut, filePath2);
request.setAttribute("cut", "null");
if (pastePath.equals(username)) {
FileStatus[] documentList = HdfsDao.ShowFiles(pastePath);
request.setAttribute("documentList", documentList);
System.out.println("得到list数据" + documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
} else {
System.out.println("从定向了1");
response.sendRedirect(request.getHeader("Referer"));
}
} else {
System.out.println("准备剪切文件:" + filePath1);
request.setAttribute("cut", filePath1);
System.out.println("我是username:" + username + "=====" + "我是thisPath:" + thisPath);
System.out.println("===============================================================");
FileStatus[] documentList = HdfsDao.ShowFiles(username);
request.setAttribute("documentList", documentList);
System.out.println("得到list数据" + documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
package com.wy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.hadoop.fs.FileStatus;
import com.wy.pojo.HdfsDao;
@WebServlet("/ShowChildDirServlet")
public class ShowChildDirServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String filePath = request.getParameter("filePath");
request.setAttribute("thisPath",filePath);
// //用于移动,传递原始文件路径给子目录
String cut = request.getParameter("cut");
System.out.println("看看cut:"+cut);
request.setAttribute("cut", cut);
FileStatus[] documentList = HdfsDao.ShowDirFiles(filePath);
request.setAttribute("documentList", documentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}
由于博主也是菜鸡,所以数据的传递都是直接跟在链接后面传的,没有考虑安全性(毕竟当时只是为了完成期末作业嘛,事也比较多),代码的可复用性也没有去管,写了不少重复的函数(可以搞成一起的),所以大佬们嘴下留情,当然,有疑问的小伙伴欢迎下方提问,另外记得在博主的github和gitee上点个小星星,需要克隆的话,见第一篇文章最后部分,推荐gitee克隆吧,下载快很多!
最后,希望一些小伙伴们,不只是clone,自己理解做出来的才是有意义的,不要应付期末作业哦!