上一篇文章其实已经贴了。再贴一次吧
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); } }由于博主也是菜鸡,所以数据的传递都是直接跟在链接后面传的,没有考虑安全性(毕竟当时只是为了完成期末作业嘛,事也比较多),代码的可复用性也没有去管,写了不少重复的函数(可以搞成一起的),所以大佬们嘴下留情,当然,有疑问的小伙伴欢迎下方提问,另外记得在博主的github和gitee上点个小星星,需要克隆的话,见第一篇文章最后部分,推荐gitee克隆吧,下载快很多! 最后,希望一些小伙伴们,不只是clone,自己理解做出来的才是有意义的,不要应付期末作业哦!