hadoop文件系统的管理----》可以做网盘的呦
2014-05-30 16:09
561 查看
今天给大家介绍一下通过hadoop的hdfs和Struts搭建一个网盘的基本框框
*************************************************************************************************************************
hdfs的基本操作
(1)获得给定路径下的文件列表 : throws FileNotFoundException, IOException {
String dst = "hdfs://localhost:9000/" + filename;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FileStatus fileList[] = fs.listStatus(new Path(dst));
int size = fileList.length;
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].getLen() == 0) {
//可以通过判断文件的大小来确定是否是文件还是文件夹
} else {
}
}
fs.close();
return value;(2)上传文件到hdfs上
String dst = "hdfs://localhost:9000/" + path;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FSDataInputStream hdfsInStream = fs.open(new Path(dst));
return hdfsInStream;(4)删除给定路径的文件
String d = "localhost:9000"+path;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dfspath), conf);
fs.deleteOnExit(new Path(dst));
fs.close();
*******************************************************************************************************************************************
struts部分
(2-1)上传实现代码
private List<File> image;
private List<String> imageContentType;
private List<String> imageFileName;
@Override
public String execute() throws Exception {
String dst = "hdfs://localhost:9000/user/qq1.txt";
String path = ServletActionContext.getServletContext().getRealPath(
"/images");
System.out.println(path);
System.out.println("保存路径为" + path);
if (image.size() > 0) {
/************************/
BufferedInputStream bis = null;
/***********************/
for (int i = 0; i < image.size(); i++) {
System.out.println("datas的个数" + image.size());
String filename = imageFileName.get(i);
FileInputStream fis = new FileInputStream(image.get(i));
bis = new BufferedInputStream(fis);
// FileUtils.copyFile(image.get(i), saveFile);
hdfsUtil.uploadToHdfs(bis, filename);
}
}
// for (int i = 0; i < image.size(); i++)
// {
// File data=image.get(i);
// String fileName=imageFileName.get(i);
// fileName=path+"\\"+fileName;
// data.renameTo(new File(fileName));
// }
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
(2-2)下载模块代码
private String fileName;
private String flag;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() throws FileNotFoundException,
IOException {
return hdfsUtil.readFromHdfs((String) (ActionContext.getContext()
.getSession().get("path"))+flag);
}
public String execute() {
return "success";
}
*************************************************************************************************************************
hdfs的基本操作
(1)获得给定路径下的文件列表 : throws FileNotFoundException, IOException {
String dst = "hdfs://localhost:9000/" + filename;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FileStatus fileList[] = fs.listStatus(new Path(dst));
int size = fileList.length;
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].getLen() == 0) {
//可以通过判断文件的大小来确定是否是文件还是文件夹
} else {
}
}
fs.close();
return value;(2)上传文件到hdfs上
String localSrc = "/home/red-zhu/chen.txt"; String dst = "hdfs://localhost:9000/user/" + name; System.out.println(dst); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dst), conf); OutputStream out = fs.create(new Path(dst), new Progressable() { public void progress() { System.out.print("."); } }); IOUtils.copyBytes(in, out, 4096, true);(3)下载文件到本地
String dst = "hdfs://localhost:9000/" + path;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FSDataInputStream hdfsInStream = fs.open(new Path(dst));
return hdfsInStream;(4)删除给定路径的文件
String d = "localhost:9000"+path;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dfspath), conf);
fs.deleteOnExit(new Path(dst));
fs.close();
*******************************************************************************************************************************************
struts部分
(2-1)上传实现代码
private List<File> image;
private List<String> imageContentType;
private List<String> imageFileName;
@Override
public String execute() throws Exception {
String dst = "hdfs://localhost:9000/user/qq1.txt";
String path = ServletActionContext.getServletContext().getRealPath(
"/images");
System.out.println(path);
System.out.println("保存路径为" + path);
if (image.size() > 0) {
/************************/
BufferedInputStream bis = null;
/***********************/
for (int i = 0; i < image.size(); i++) {
System.out.println("datas的个数" + image.size());
String filename = imageFileName.get(i);
FileInputStream fis = new FileInputStream(image.get(i));
bis = new BufferedInputStream(fis);
// FileUtils.copyFile(image.get(i), saveFile);
hdfsUtil.uploadToHdfs(bis, filename);
}
}
// for (int i = 0; i < image.size(); i++)
// {
// File data=image.get(i);
// String fileName=imageFileName.get(i);
// fileName=path+"\\"+fileName;
// data.renameTo(new File(fileName));
// }
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
(2-2)下载模块代码
private String fileName;
private String flag;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() throws FileNotFoundException,
IOException {
return hdfsUtil.readFromHdfs((String) (ActionContext.getContext()
.getSession().get("path"))+flag);
}
public String execute() {
return "success";
}
相关文章推荐
- 通用权限管理系统组件 (GPM - General Permissions Manager) 不需要任何配置文件,程序都可以正常运行
- 通用权限管理系统组件 (GPM - General Permissions Manager) 不需要任何配置文件,程序都可以正常运行
- 企业级Hadoop 2.x入门系列之四HDFS文件系统和YARN资源管理框架简介
- Hadoop HDFS概念学习系列之分布式文件管理系统(二十五)
- 基于JFinal+AngularJs+Hadoop的WEB文件管理系统
- 利用eclipse管理Hadoop集群文件系统
- vs2005视频教程 之 文件管理系统(二)视频教程[视频]
- [原]找热衷公益的可以开发信息管理系统的朋友
- 最近刚为公司完成的一款监视的小工具软件!用DELPHI 7 写的,可以从后台监控系统﹑键盘﹑鼠标﹑屏幕以及文件与目录!有源码!
- Windows XP一个很无敌的命令replace--系统运行中也可以替换系统文件
- vs2005视频教程 之 文件管理系统(三)视频教程[视频]
- 在线文件管理系统 下载地址
- 文件管理系统
- 教学管理系统-链接文件的发布问题
- 针式个人知识库管理系统V2.6.1多文件格式支持版本
- 粗谈LINUX下的文件系统管理
- FAT磁盘文件管理系统的组成
- HP UNIX 分区管理及文件系统
- 深入理解和管理Linux文件系统
- 明日网全功能超级文件上传管理系统 V1.0 源码发布