注:转载请署名
一、实体
package com.ebd.application.modules.fileManage.pojo;
public class FilesOrDirs {
private String id;
private String permission;
private String owner;
private String group;
private String type;
private String size;
private String modificationTime;
private String name;
private String dirs;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getModificationTime() {
return modificationTime;
}
public void setModificationTime(String modificationTime) {
this.modificationTime = modificationTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirs() {
return dirs;
}
public void setDirs(String dirs) {
this.dirs = dirs;
}
}
二、前端页面
String path = request.getContextPath();
String rootPath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ "/";
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
request.setAttribute("basePath", basePath);
request.setAttribute("rootPath", rootPath);
pageContext.setAttribute("newLineChar", "\n");
%>
提示:ctrl(cmd)+backspace 返回上一级
权限所属组别类型大小上次修改名称操作
$(function(){
//初始化列表及分页
submit();
//点击查询时触发(该功能使用绑定回车标签实现-也就是刷新)
// $("#search").click(function(){
// //当点击搜索的时候,起始搜索值为0-10,且当前页回到第一页
// startPage = 0;
// currentPage = 1;
// submit();
// })
});
//分页参数设置 这些全局变量关系到分页的功能
var startPage = 0;
var limitNum = 10;
var currentPage = 1;
var totalPage = 0;
//ajax请求后台数据
function submit(){
var path = $(".search_input").val()
$.ajax({
type:"post",
async:false,
url:"${basePath}/file/fileListByPage.htm",
data:{start:startPage, limit:limitNum,path:path},
success:function(data,status){
data=eval("("+data+")"); //转为对象
startPage = data.currentIndex;//当前页数(后台返回)
totalPage = data.totalPage;//总页数(后台返回)
toPage(data.dataList);
}
});
}
//看看参数有的是否为空(data:后台数据)
function toPage(data){
layui.use(['form', 'layer', 'layedit', 'laydate', 'laypage'], function() {
var form = layui.form(),
layer = layui.layer,
layedit = layui.layedit,
laydate = layui.laydate,
laypage = layui.laypage;
//调用分页
laypage({
cont: 'page'
,pages: totalPage
,curr: currentPage
,skip: true
,jump: function(obj, first){
currentPage = obj.curr;
startPage = (obj.curr-1)*limitNum;
getBackDatas(data); //渲染数据
if(!first){ //加此判断,避免初始时会无限刷新
submit(); //一定要把翻页的ajax请求放到这里,不然会请求两次。
}
form.render();
}
});
//全选
form.on('checkbox(allChoose)', function (data) {
var checkvals="";
var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
child.each(function (index, item) {
item.checked = data.elem.checked;
if(item.checked){
checkvals += ","+item.value;
}
});
//捕捉被选记录个数及id
$("#checkids").attr("valength",checkvals.split(",").length-1);
$("#checkids").val(checkvals);
form.render('checkbox');
});
//通过判断文章是否全部选中来确定全选按钮是否选中
form.on("checkbox(choose)", function (data) {
var checkvals="";
var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
if (childChecked.length == child.length) {
$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
} else {
$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
}
//遍历被选中的记录
for(var i=0;i
checkvals += ","+childChecked[i].value;
}
//捕捉被选记录个数及id
$("#checkids").attr("valength",childChecked.length);
$("#checkids").val(checkvals);
form.render('checkbox');
})
});
};
//遍历装载数据
function getBackDatas(data){
var htmlStr = "";
$.each(data,function(v,o){
htmlStr+="
";htmlStr+='
'+o.permission+'';htmlStr+='
'+o.owner+'';htmlStr+='
'+o.group+'';htmlStr+='
';if(o.type == 'd'){
htmlStr+='目录';
}else{
htmlStr+='文件';
}
htmlStr+='
';htmlStr+='
'+o.size+'';htmlStr+='
'+o.modificationTime+'';htmlStr+='
'+o.name+'';htmlStr+='
';if(o.type == 'f'){
htmlStr+='下载';
}else{
htmlStr+='上传';
htmlStr+='进入';
}
htmlStr+='删除';
htmlStr+='
';});
if(data.length>0){
$("#page").show();
$(".files_content").html(htmlStr);
}else{
$("#page").hide();
$(".files_content").html("
暂无数据");}
//清空checkids(checkbox记录)
$("#checkids").attr("valength",0);
$("#checkids").val("");
}
//前一个目录(暂时没用)
function data_prev(dirs,pathName){
$(".search_input").val(dirs.replace(pathName,''));
submit();
}
//后一个目录
function data_next(dirs){
$(".search_input").val(dirs);
submit();
}
function data_up(remoteDir){
layer.prompt({
value: '举栗 D:/wx/install.txt',
title: '请输入本地文件路径',
maxlength: 100
}, function(value, index){
var dirRegex = /^([A-Za-z]{1}:\/[\w\/]*)?\w+\.{1}[a-zA-Z]+$/;
if(!dirRegex.exec(value)){
layui.layer.alert('友情提示:请输入正确的目录注意:间隔符为 / 且目录不能有特殊字符', {
skin: 'layui-layer-molv'
,closeBtn: 0
});
}else{
$.ajax({
type : "POST",
async : false,
dataType : "JSON",
cache : false,
url : "${basePath}/file/fileup.htm",
data : {localFile:value, remoteDir:remoteDir},
success : function(data) {
if (data.flag) {
layer.msg(data.msg, {
icon:data.msgIcon,
time:data.msgTime
});
} else {
layer.msg(data.msg, {
icon:data.msgIcon,
time:data.msgTime
});
}
}
});
}
layer.close(index);
});
}
function data_down(remoteFile){
layer.prompt({
value: '举栗 D:/download',
title: '请输入本地目录',
maxlength: 100
}, function(value, index){
var dirRegex = /^([A-Za-z]{1}:\/[\w\/]*)?\w+[/|\w]$/;
if(!dirRegex.exec(value)){
layui.layer.alert('友情提示:请输入正确的目录注意:间隔符为 / 且目录不能有特殊字符', {
skin: 'layui-layer-molv'
,closeBtn: 0
});
}else{
$.ajax({
type : "POST",
async : false,
dataType : "JSON",
cache : false,
url : "${basePath}/file/filedown.htm",
data : {remoteFile:remoteFile, targetDir:value},
success : function(data) {
if (data.flag) {
layer.msg(data.msg, {
icon:data.msgIcon,
time:data.msgTime
});
} else {
layer.msg(data.msg, {
icon:data.msgIcon,
time:data.msgTime
});
}
}
});
}
layer.close(index);
});
}
function data_del(remoteFileOrDir){
layer.confirm('友情提示:你确定要删除该文件或文件夹吗', {skin: 'layui-layer-molv' }, function(index){
$.ajax({
type : "POST",
async : false,
dataType : "JSON",
cache : false,
url : "${basePath}/file/fileordirdel.htm",
data : {remoteFileOrDir:remoteFileOrDir},
success : function(data) {
if (data.flag) {
layer.msg(data.msg, {
icon:data.msgIcon,
time:data.msgTime
}, function(){
submit();
});
} else {
layer.msg(data.msg, {
icon:data.msgIcon,
});
}
}
});
layer.close(index);
});
}
//绑定搜索框回车事件(暂未使用)
// $('.search_input').bind('keypress', function (event) {
// if(event.keyCode == "13") {
// submit();
// }
// })
//监听按键事件
document.onkeydown = function(e) {
var keyCode = e.keyCode || e.which || e.charCode;
var ctrlKey = e.ctrlKey || e.metaKey;
if(keyCode == 8) {
var sval = $('.search_input').val();
if(sval!=''&&sval.substring(0,1)=='/'&&sval!='/'){
var prevdir;
if(sval.substring(sval.length-1)!='/'){
prevdir = sval.substring(0,sval.lastIndexOf("/"));
$(".search_input").val(prevdir);
}
if(prevdir==''){
$('.search_input').val('/');
}
//重置开始页面数
startPage=0;
submit();
}
}
//e.preventDefault();
return true;
}
三、控制器
package com.ebd.application.modules.fileManage.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ebd.application.common.Base.Page;
import com.ebd.application.common.support.RetObj;
import com.ebd.application.modules.fileManage.pojo.FilesOrDirs;
import com.ebd.application.modules.fileManage.service.FileService;
@Controller
@Scope("prototype")
@RequestMapping("/file")
public class FileController {
// 日志记录器
public final Logger log = Logger.getLogger(this.getClass());
@Autowired
private FileService fileService;
@RequestMapping("filemanage")
public String fileManage(HttpServletRequest request, String currPage) {
return "modules/file/filemanage";
}
@ResponseBody
@RequestMapping("fileListByPage")
public Page listByPage(Integer start, Integer limit, String path) {
Map conditions = new HashMap();
conditions.put("path", StringUtils.isNotBlank(path) ? path : "/");
Page page = new Page(start,limit,conditions);
return fileService.findByPage(page);
}
@ResponseBody
@RequestMapping("fileup")
public RetObj fileUp(String localFile, String remoteDir) {
return fileService.fileUp(localFile,remoteDir);
}
@ResponseBody
@RequestMapping("filedown")
public RetObj fileDown(String remoteFile, String targetDir) {
return fileService.fileDown(remoteFile,targetDir);
}
@ResponseBody
@RequestMapping("fileordirdel")
public RetObj fileOrDirDel(String remoteFileOrDir) {
return fileService.fileOrDirDel(remoteFileOrDir);
}
}
四、FileDao类
package com.ebd.application.modules.fileManage.dao;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.mapred.JobConf;
import com.ebd.application.common.Base.Constants;
import com.ebd.application.common.Base.Page;
import com.ebd.application.common.support.RetObj;
import com.ebd.application.common.utils.CreateFileUtil;
import com.ebd.application.common.utils.DateUtils;
import com.ebd.application.common.utils.Identities;
import com.ebd.application.modules.fileManage.pojo.FilesOrDirs;
public class FileDao {
//hdfs路径
private static String hdfsPath;
//Hadoop系统配置
private static Configuration conf;
//fdfs对象
private static FileDao hdfsObj;
private static List filesList = null;
public FileDao(Configuration conf) {
this(Constants.HDFS, conf);
}
public FileDao(String hdfs, Configuration conf) {
this.hdfsPath = hdfs;
this.conf = conf;
}
public static FileDao getHdfsObj() {
if(hdfsObj == null){
return new FileDao(config());
}
return hdfsObj;
}
public static int getCount(Page page) {
int count = 0;
try {
count = count(page);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
public static List findByPage(Page page) {
List filesList = null;
try {
filesList = getHdfsObj().ls(page);
} catch (Exception e) {
e.printStackTrace();
}
return filesList;
}
public static JobConf config(){
JobConf conf = new JobConf(FileDao.class);
conf.setJobName("HdfsDAO");
conf.addResource("hadoop/core-site.xml");
conf.addResource("hadoop/hdfs-site.xml");
conf.addResource("hadoop/mapred-site.xml");
return conf;
}
public static int count(Page page){
int count = 0;
try {
Path path = new Path(Constants.HDFS+page.getCondition().get("path"));
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FileStatus[] fileArray = fs.listStatus(path);
if(fileArray != null){
filesList = new ArrayList();
for (FileStatus f : fileArray) {count++;}
}
fs.close();
} catch (Exception e) {
return count;
}
return count;
}
public List ls(Page page){
int count = 1;
try {
Path path = new Path(Constants.HDFS+page.getCondition().get("path"));
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
FileStatus[] fileArray = fs.listStatus(path);
if(fileArray != null){
filesList = new ArrayList();
if(fileArray.length>page.getStart()){
for(int i = page.getStart(); i
count++;
if(count
FilesOrDirs fd = new FilesOrDirs();
fd.setId(Identities.uuid());
fd.setName(fileArray[i].getPath().getName());
fd.setGroup(fileArray[i].getGroup());
fd.setModificationTime(DateUtils.timestamptoString(fileArray[i].getModificationTime()));
fd.setOwner(fileArray[i].getOwner());
fd.setPermission(fileArray[i].getPermission().toString());
fd.setSize(String.format("%d", fileArray[i].getLen()));
fd.setType(fileArray[i].isDirectory()?Constants.IS_DIR:Constants.IS_FILE);
fd.setDirs(fileArray[i].getPath().toString().substring(Constants.HDFS.length()));
filesList.add(fd);
}
}
}
}
fs.close();
} catch (IOException e) {
return filesList;
}
return filesList;
}
public static RetObj fileUp(String localFile, String remoteDir) {
try {
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.copyFromLocalFile(new Path(localFile), new Path(remoteDir));
fs.close();
} catch (Exception e) {
return RetObj.getRetObj(false,Constants.MSG_UPLOAD_FAILED,2,1000);
}
return RetObj.getRetObj(true,Constants.MSG_UPLOAD_SUCCESS,1,1000);
}
public static RetObj fileDown(String remoteFile, String targetDir) {
try {
FileSystem fs = FileSystem.get(URI.create(remoteFile),conf);
FSDataInputStream fsdi = fs.open(new Path(remoteFile));
if(CreateFileUtil.createDir(targetDir)){
OutputStream output = new FileOutputStream(targetDir+remoteFile.substring(remoteFile.lastIndexOf("/")));
IOUtils.copyBytes(fsdi,output,4096,true);
}
fs.close();
} catch (Exception e) {
return RetObj.getRetObj(false,Constants.MSG_DOWNLOAD_FAILED,2,1000);
}
return RetObj.getRetObj(true,Constants.MSG_DOWNLOAD_SUCCESS,1,1000);
}
public static RetObj fileOrDirDel(String remoteFileOrDir) {
try {
Path path = new Path(remoteFileOrDir);
FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
fs.deleteOnExit(path);
fs.close();
} catch (Exception e) {
return RetObj.getRetObj(false,Constants.MSG_CRUD_FAILED,2,1000);
}
return RetObj.getRetObj(true,Constants.MSG_CRUD_SUCCESS,1,1000);
}
}
Page类
package com.ebd.application.common.Base;
import java.util.List;
import java.util.Map;
public class Page {
/** 总记录数 */
private int total;
/** 分页结果 */
private List dataList;
/** 开始页码 */
private int start;
/** 每页多少 */
private int limit;
/** 查询条件 */
private Map condition;
private int currentPage; //当前页
private int currentIndex; //当前记录起始索引
private int totalPage; //总页数
public Page(int start, int limit) {
this.start = start;
this.limit = limit;
}
public Page(int start, int limit, Map condition) {
this.start = start;
this.limit = limit;
this.condition = condition;
}
public int getCurrentPage() {
if(currentPage<=0) currentPage = 1;
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentIndex() {
this.currentIndex = (getCurrentPage()-1)*getLimit();
if(currentIndex<0) currentIndex = 0;
return currentIndex;
}
public void setCurrentIndex(int currentIndex) {
this.currentIndex = currentIndex;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
if(total%limit==0)
totalPage = total/limit;
else
totalPage = total/limit+1;
this.total = total;
}
public List getDataList() {
return dataList;
}
public void setDataList(List dataList) {
this.dataList = dataList;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public Map getCondition() {
return condition;
}
public void setCondition(Map condition) {
this.condition = condition;
}
}
效果图