教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 政务民生 >

hadoop网盘环境搭建,代码全部过程(8)

来源:网络收集 时间:2026-08-21
导读: } try { } returnb; //获得连接 ct = new ConnDB().getConn(); //创建statement sm = ct.createStatement(); sm.execute(\ b = true; values('\+id+\+user+\+emal+\+password+\); }finally{ this.close(); 数据访问

}

try { } returnb;

//获得连接

ct = new ConnDB().getConn(); //创建statement sm = ct.createStatement();

sm.execute(\

b = true;

values('\+id+\+user+\+emal+\+password+\);

}finally{

this.close();

数据访问层: HdfsDAO.java

publicclass HdfsDAO {

//HDFS访问地址

privatestaticfinal String HDFS = \;

publicHdfsDAO(Configuration conf) { this(HDFS, conf); }

publicHdfsDAO(String hdfs, Configuration conf) { this.hdfsPath = hdfs; this.conf = conf; }

//hdfs路径

private String hdfsPath; //Hadoop系统配置

private Configuration conf;

//启动函数

36 / 53

publicstaticvoid main(String[] args) throws IOException { JobConf conf = config();

HdfsDAO hdfs = new HdfsDAO(conf);

}

//加载Hadoop配置文件 publicstatic JobConf config(){

JobConf conf = newJobConf(HdfsDAO.class); conf.setJobName(\);

conf.addResource(\); conf.addResource(\); conf.addResource(\); returnconf; }

//在根目录下创建文件夹

publicvoidmkdirs(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); if(!fs.exists(path)) { fs.mkdirs(path);

System.out.println(\ + folder); } fs.close(); }

//某个文件夹的文件列表

publicFileStatus[] ls(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); FileStatus[] list = fs.listStatus(path); System.out.println(\ + folder);

System.out.println(\); if(list != null) for (FileStatus f :list) {

System.out.printf(\大小: %dK\\n\, f.getPath().getName(), (f.isDir()?\目

37 / 53

录\:\文件\), f.getLen()/1024); }

System.out.println(\); fs.close();

returnlist; }

publicvoidcopyFile(String local, String remote) throws IOException { FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); //remote---/用户/用户下的文件或文件夹

fs.copyFromLocalFile(new Path(local), new Path(remote));

System.out.println(\ + local + \ + remote); fs.close(); }

//删除文件或文件夹

publicvoidrmr(String folder) throws IOException { Path path = new Path(folder);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.deleteOnExit(path);

System.out.println(\ + folder); fs.close(); }

//下载文件到本地系统

publicvoiddownload(String remote, String local) throws IOException { Path path = new Path(remote);

FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf); fs.copyToLocalFile(path, new Path(local));

System.out.println(\ + remote + \ + local); fs.close(); } }

三、功能实现

38 / 53

文件上传功能: (1)选择要上传的文件

(2)上传完毕:

39 / 53

主要代码:

publicclassUploadServletextends HttpServlet { /**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(\); String filePath0 = new

String(request.getParameter(\).getBytes(\),\); System.out.println(\+filePath0+\); File file ;

intmaxFileSize = 50 * 1024 *1024; //50M intmaxMemSize = 50 * 1024 *1024; //50M ServletContext context = getServletContext();

String filePath = context.getInitParameter(\); System.out.println(\+filePath+\); // 验证上传内容了类型

String contentType = request.getContentType(); if ((contentType.indexOf(\) >= 0)) {

DiskFileItemFactory factory = newDiskFileItemFactory(); // 设置内存中存储文件的最大值 factory.setSizeThreshold(maxMemSize); // 本地存储的数据大于 maxMemSize. factory.setRepository(new File(\));

40 / 53

…… 此处隐藏:1263字,全部文档内容请下载后查看。喜欢就下载吧 ……
hadoop网盘环境搭建,代码全部过程(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/448558.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)